Using Short IDs

August 30, 2011
Tags: Sitecore

If you've ever worked with Sitecore for any length of time you've worked with an item ID. It's in the form: {7294ECFC-0C37-44AD-B728-ABA259B18889}. There are times when you need to pass the ID around through querystring values or other visible locations but don't want to deal with all the troubles of encoding/decoding the entity characters or just because it's damn ugly. You can, however, convert the ID to just it's alpha-numeric character sequence known in Sitecore as a ShortID. When you convert the previous ID to a ShortID and print the value, the result string will look like this: 7294ECFC0C3744ADB728ABA259B18889. It's still a long value but useful in other ways. The following code sample converts a ShortID string value to an item and then back to a ShortID string.

//get item from short idstring shortID = "7294ECFC0C3744ADB728ABA259B18889";			if (!ShortID.IsShortID(shortID)){	ID id = ShortID.DecodeID(shortID);	if (!id.IsNull){		Item someItem = Sitecore.Context.Database.GetItem(id);		//turn the id back into a short id string		string sid = id.ToShortID().ToString();	}}

I know this was a short article but there wasn't much out there in terms of information on the use of ShortID. I really just ended up poking around intellisense to figure it out so I thought although it might throw it out there to add a little value to the cruft.