Notes about the Sitecore ID class

A description of the Sitecore.Data.ID class and its methods and quirks along with a few code snippets.

Introduction

Namespace: Sitecore.Data

Assembly: Sitecore.Kernel.dll

The ID class in Sitecore is used to identify all types of item in Sitecore i.e. content items, templates, media items etc. It is a wrapper around .NET's own System.Guidstruct and has a property called Guidwhich returns a System.Guid. Internally Sitecore stores IDs in it's SQL Server database using the uniqueidentifiertype, but often stores IDs in string format e.g. when storing a link to an item within a droplink field.

The string format for IDs in Sitecore is equivalent to System.Guid.ToString("B").ToUpperInvariant()which is 32 upper case digits separated by hyphens and enclosed in braces. E.g. {00000000-0000-0000-0000-000000000000}

ID.Parse Static Method (ID)

Sitecore IDs can be parsed from string or object and will accept the following:

Sitecore.Data.ID class example usage
        ID.Parse("00000000000000000000000000000000"); //OK
ID.Parse("00000000-0000-0000-0000-000000000000"); //OK
ID.Parse("(00000000-0000-0000-0000-000000000000)"); //OK
ID.Parse("{00000000-0000-0000-0000-000000000000}"); //OK
ID.Parse("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}"); //OK
ID.Parse(""); //Throws FormatException
ID.Parse(" "); //Throws FormatException
ID.Parse("bob"); //Throws FormatException
    

ID.TryParse Static Method (Bool)

If the input source is not guaranteed to be a valid GUID format, then it is good practice to use ID.TryParse, which returns true if the input was valid and populates the result output parameter.

Sitecore.Data.ID.TryParse examples
        ID value;
ID.TryParse("00000000000000000000000000000000", out value); // false
ID.TryParse("00000000-0000-0000-0000-000000000000", out value); // true
ID.TryParse("(00000000-0000-0000-0000-000000000000)", out value); // false
ID.TryParse("{00000000-0000-0000-0000-000000000000}", out value); // true
ID.TryParse("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}", out value); // false
    

Note the subtle inconsistency with the ID.Parse method.

ID.IsID Static Method (Bool)

Returns true if the input string is a valid ID

Sitecore.Data.ID.IsID example usage
        ID.IsID("00000000000000000000000000000000"); // false
ID.IsID("00000000-0000-0000-0000-000000000000"); // true
ID.IsID("(00000000-0000-0000-0000-000000000000)"); // false
ID.IsID("{00000000-0000-0000-0000-000000000000}"); // true
ID.IsID("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}"); // false
    

ID.Encode Static Method (String)

Returns a string representation of the ID with no hyphens and no enclosing braces, format "N".

Sitecore.Data.ID.Encode example
        var id = new ID();
ID.Encode(id); // returns in the format "00000000000000000000000000000000"
    

ID.Decode Static Method (ID)

Returns an ID if the input string is in a valid format, otherwise throws FormatException.

Sitecore.Data.ID.Decode example
        ID.Decode("00000000000000000000000000000000"); // returns ID
ID.Decode("00000000-0000-0000-0000-000000000000"); // returns ID
ID.Decode("(00000000-0000-0000-0000-000000000000)"); // Throws FormatException
ID.Decode("{00000000-0000-0000-0000-000000000000}"); // returns ID
ID.Decode("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}"); // throws FormatException
    

ID.ParseArray Static Method (ID[])

This takes a list of pipe-separated IDs and returns an array of IDs.

Sitecore.Data.ID.ParseArray examples
        ID.ParseArray("00000000000000000000000000000000|00000000000000000000000000000000"); // returns empty array
ID.ParseArray("00000000-0000-0000-0000-000000000000|00000000-0000-0000-0000-000000000000"); // returns array of two elements
ID.ParseArray("(00000000-0000-0000-0000-000000000000)|(00000000-0000-0000-0000-000000000000)"); // returns empty array
ID.ParseArray("{00000000-0000-0000-0000-000000000000}|{00000000-0000-0000-0000-000000000000}"); // returns array of two elements
ID.ParseArray("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}|{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}"); // returns empty array
ID.ParseArray("|{00000000-0000-0000-0000-000000000000}||{00000000-0000-0000-0000-000000000000}|"); // returns array of two elements
ID.ParseArray("non|sense"); // returns empty array
ID.ParseArray((string)null, true); // returns null
ID.ParseArray((string)null, false); // returns empty array
    

ID.ArrayToString Static Method (String)

Converts an array of IDs into a pipe-separated string. Override method allows alternative separator.

Sitecore.Data.ID.ArrayToString examples
        var ids = new ID[] { new ID(), new ID() };
ID.ArrayToString(ids); //returns "{00000000-0000-0000-0000-000000000000}|{00000000-0000-0000-0000-000000000000}" (actual GUID values will differ!)
ID.ArrayToString(ids, ','); //returns "{00000000-0000-0000-0000-000000000000},{00000000-0000-0000-0000-000000000000}" (actual GUID values will differ!)
    

ShortID

This is a wrapper around the ID class which serves to read and write the ID in the 32 digit format (no hyphens and no enclosing braces or parentheses). This is useful when passing an ID via a query string. ID.ToShortID() returns a new instance of ShortID with the same internal GUID value. Likewise ShortID.ToID() returns a new ID with the same internal GUID value.

TemplateID

This class is essentially obsolete and should not be used.

GUIDs (in .NET) are case-insensitive

GUIDs in string form are a series of hexadecimal numbers and are therefore case-insensitive.

Be warned though, if you are talking to other non-.net systems this may not be the case!

Guid.ToString Format Types

The following format options can be used with System.Guid:

System.Guid.ToString examples
        var guid = new Guid();
guid.ToString("N"); //Returns "00000000000000000000000000000000"
guid.ToString("D"); //Returns "00000000-0000-0000-0000-000000000000"
guid.ToString("P"); //Returns "(00000000-0000-0000-0000-000000000000)"
guid.ToString("B"); //Returns "{00000000-0000-0000-0000-000000000000}"
guid.ToString("X"); //Returns "{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}"
    

Other random facts

  • You can paste an item ID into the search box of the Content Editor to find an item (only works in regular Sitecore ID format).

Download

I have created a Gist on github with the code snippets here which you can run in LINQPad

Tagged: CMS Sitecore

comments powered by Disqus