One common need when working with CRM is to convert GUIDs to String. Say, you want to build up some fetchxml. No problem, .ToString(), right?
Well, Guids can actually be converted to strings in many different ways – even just the hexadecimal string can be with brackets, without brackets, uppercase, lowercase, with or without hyphens, etc.
To keep your code as reliable and readable as possible, you’ll want to do this consistently – CRM uses uppercase hexadecimal, with brackets. Guid has a ToString that takes a format string that you can pass it several different options. “B” is closest to what we want, but not quite right, because it returns lowercase. So you should convert to uppercase… but you’ll want to be careful, because converting to uppercase behavior can vary depending on the locale of the box running the code. Usually your code will run on a server and it won’t matter, but what if you are writing a plugin which is running offline on an outlook client? I don’t actually know if there is a locale where the hexadecimal characters A-F don’t convert right, but I know that in Turkey the uppercase of i is not I, so I don’t want to push my luck that no culture does something similar with A-F (update: Writing culture-safe managed code says Turkish and Azeri are the only languages with single character case differences, but this points out there may be multiple character case differences, such as Ff in welsh, and then there are some languages that don’t have some letters, such as Cryllic has no C for example…). So, to be as safe as possible, specify the ‘InvariantCulture’ for any string comparisons or conversions that should deal with strings in a program-readable consistent manner.
Finally, to make this easily accessible to all of your code, you can add a utility method directly to the Guid class as an Extension Method.
public static class CRMUtilExtensionmethods
public static string ToStringForCRM(this Guid guid)
return guid.ToString("B", CultureInfo.InvariantCulture).ToUpper(CultureInfo.InvariantCulture);
Now whenever I want to turn a guid into a string, I just have to have
at the top of the class, and my extension method is available directly on the Guid itself (handily also in the autocomplete dropdown):
Guid taskGuid = crmservice.Create(t);
string strTaskGuid = taskGuid.ToStringForCRM();