LibrarySites.Banner

Accessing Items in the Sitecore ASP.NET CMS

This blog post describes provides some introductory information about accessing items in the Sitecore ASP.NET CMS by database, ID, path, key, name, language, version, and URI. I don't expect that this post contains any new information for experienced Sitecore developers.

You can use a variety of techniques to access items in the Sitecore ASP.NET CMS. When possible, use GUIDs rather than paths or especiall names. For example, use the Droplink field type, which stores the GUID of the selected item, rather than the Droplist field type, which stores only its name. Use paths and names only for reporting, such as when presenting an item in the user interface or exporting data.

Each item exists in a database, and you can either use use the context database:

Sitecore.Data.Database context =
  Sitecore.Context.Database;

Or you can explicitly reference a named database, such as the Master database:

Sitecore.Data.Database master =
  Sitecore.Configuration.Factory.GetDatabase("master");

For more information about the configuration factory, see my previous blog post The Sitecore ASP.NET CMS Configuration Factory.

You can retrieve an item from a database using the Globally Uniqiue ID of the item, its path, or using its key or other criteria relative to another item, where the key is the lowercase of the item name.

For example, to retrieve an item from a database using its path:

Sitecore.Data.Items.Item item =
  context.GetItem("/sitecore/content");

To retrieve the same item using its Sitecore.Data.ID:

Sitecore.Data.Items.Item item = context.GetItem(
  Sitecore.ItemIDs.ContentRoot);
 
To retrieve a child with a specific key:

Sitecore.Data.Items.Item home = item.Children["home"];

As this example demonsrates, you should avoid creating two child items with the same key under a single parent item, even with different capitalization.

Each item can consist of a number of languages, and each language can contain a number of versions. If you do not specify a language or version, Sitecore uses the appropriate version in the context language, which can depend on workflow and publishing status.

Sitecore provides URIs for each version of each item in each language in each database using the sitecore protocol. The Sitecore.Data.ItemUri class represents a full item designation including database, ID, language and version. This class is especially helpful when storing this information as text. For example, Sitecore uses this representation in the __Source field in the Advanced section of the standard template used to support cloning.

You can access alternate languages and versions of items:

foreach (Sitecore.Globalization.Language language
  in home.Languages)
{
  Sitecore.Data.Items.Item langItem =
    home.Database.GetItem(home.ID, language);

  foreach (Sitecore.Data.Items.Item version
    in langItem.Versions.GetVersions())
  {
    string uri = home.Uri.ToString();
  }
}

If needed, you can access a specific version of a specific language:

Sitecore.Data.Items.Item version = context.GetItem(
  home.ID,
  Sitecore.Data.Managers.LanguageManager.GetLanguage("en"),
  new Sitecore.Data.Version(1));

You can also retrieve items by URI:

Sitecore.Data.ItemUri homeUri = new Sitecore.Data.ItemUri(
  "sitecore://master/{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}?lang=en&ver=1");
Sitecore.Data.Items.Item home = Sitecore.Data.Database.GetItem(uri);