Maybe this should be in a “how to break Sitecore” section!
Question: Which of these Guid
formats is valid?
1 | var ex1 = new Guid("00000000000000000000000000000000"); |
Answer: The first 5 are perfectly valid Guid
formats, unless you need to index those values with Sitecore!
ShortID.Encode is picky about formats!
When adding a Guid
to an index Sitecore uses the ShortID
data type instead of the full Guid, this ShortID
formats the Guid
without hyphens or braces. This is great for indexes, it makes the value easily searchable by both Lucene and SOLR. Its also pretty useful in other area’s. You might use it to add your Item ID to a query string or link so that you don’t have to Url encode the values etc…
So pretty useful!
BUT, the method to encode a Guid
to a ShortID
is not very forgiving. Here it is:
1 | /// <summary>Converts a string to a ShortID.</summary> |
So you can see the problem!
- First it takes a string, not a Guid object. Yes there is an overload that takes a Guid and formats it as a string, but you are not forced to use that.
- Second, apart from checking if the value is null, no other validation is done. This code would pass all validation:Right away, the code assumes that the string is the correct length and that all the parts of the
1
var myShortId = ShortID.Encode("Will you be my friend?");
Guid
are in the correct place. Any deviation to that results in an untrapped exception. This means that in the above list, examples 1, 2 and 5 would all cause an exception in your code.
The big problem here is that if you programatically edit a link or list field, you must make sure that you format the Guid
correctly, otherwise you will end up with some nasty errors in the crawling log file. Fortunately there is a helper method for this:
1 | // Sitecore utility method |
TL/DR;
So if you are going to use ShortID.Encode
- make sure you pass in the right format or you are going to get some unexpected results!