Get Item URI

Get Item URI

ItemURI

  • The ItemURI contains the ID or path, language, version and database name bundled together
  • This is useful because if you do not specify the database, version, and language , you get the latest numeric version in the current (Sitecore.Context.Language) language from the context database when you only provide the GUID:
    • Create an object of type ItemUri for the currently requested item
    • The string representation will look like this: sitecore://master/{11111111-1111-1111-1111-111111111111}?lang=en&ver=2"

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Data.ItemUri

ItemURI C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

Sitecore.Data.ItemUri uri = item.Uri;
if (uri == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "URI String", "<b>" + uri.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "URI Database", "<b>" + uri.DatabaseName + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "URI ID", "<b>" + uri.ItemID.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "URI Path", "<b>" + uri.Path + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "URI Version", "<b>" + uri.Version.ToString() + "</b>");

output.RenderEndTag(); //Div


ItemURI C# Web Control Output

URI String: sitecore://web/{EAE2E014-198F-43D3-B694-7FA739E2A3C6}?lang=en&ver=1
URI Database: web
URI ID: {EAE2E014-198F-43D3-B694-7FA739E2A3C6}
URI Path: {EAE2E014-198F-43D3-B694-7FA739E2A3C6}
URI Version: 1