Sitecore CMS and everything related RSS 2.0
 Wednesday, January 14, 2009

Sitecore has a number of ways to reference an item. Most common are ID and path, and most methods will accept either System.String or Sitecore.Data.ID parameter.

Item item = Context.Database.GetItem("/sitecore/content/somepath");
Item item = Context.Database.GetITem(ID.Parse("{11111111-1111-1111-1111111111111111"});

Our internal convention is that the ID can be used instead of path. You can supply the id as a string to methods that expect a path:

Item item = Context.Database.GetItem("{11111111-1111-1111-1111111111111111"}");

However you have to remember that neither path nor ID identify a piece of content in a unique way. Because Sitecore supports versions and multiple languages, a single item can have 3 versions in English and 2 in Danish. If you only use path or ID when retrieving items, you will get the latest version in the current (Sitecore.Context.Language) language.

Sometimes you need to be specific, so the GetItem() methods have overloads allowing to specify language and version:

Item item = Context.Database.GetItem("/some/path", Language.Parse("en"), Version.Parse(2));

Items can also come from different databases. Notice that I have been using the context database in the above examples, which depends on the current site and Sitecore configuration.

To identify an item, or rather a version, in a unique way, Sitecore has an ItemUri class. ItemUri is an ID or path, language, version and database name bundled together, as well as a number of convenience methods. This is how it looks like:

// get the item URI
ItemUri uri = Context.Item.Uri;

// string representation
string uriString = uri.ToString() -> "sitecore://master/{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}?lang=en&ver=1"

// parsing a string
uri = ItemUri.Parse(uriString);

// get an item by URI. Static Database.GetItem method can be used because URI includes database name.
Item item = Database.GetItem(uri);

There is a reason why paths or IDs remain most common ways to reference an item - usually it is a good thing that code adapts to current context and configuration. For some less typical Sitecore tasks, URIs can be handy.

Wednesday, January 14, 2009 1:07:50 PM (FLE Standard Time, UTC+02:00)  #    Comments [2]
Sitecore
Wednesday, January 14, 2009 10:23:40 PM (FLE Standard Time, UTC+02:00)
Great post on best practices!
Wednesday, January 14, 2009 11:04:34 PM (FLE Standard Time, UTC+02:00)
I always wondered about the differences. Thanks for explaining.
Matt Hovany
Comments are closed.
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Blogroll
 Alex de Groot
Few words about SiteCore from Holland
 Alexander Shyba
Sitecore Support
 Anders Dreyer
Anders Dreyer on Sitecore Development
 Jakob Christensen
Sitecore Core Development
 Lars Fløe Nielsen
Lars's ramblings about development and business processes
 Ole Thrane
Sitecore API
 Runi Thomsen
Runi Thomsen Sitecore Toughts
 The Sitecore Experience
The Sitecore Experience
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010
Alexey Rusakov
Sign In
Statistics
Total Posts: 211
This Year: 0
This Month: 0
This Week: 0
Comments: 0
Themes
Pick a theme:
All Content © 2010, Alexey Rusakov
DasBlog theme 'Business' created by Christoph De Baene (delarou)