Should You Test SharePoint GUID’s?

I am really not sure. I have had instances where using things like the ContainingDocumentLibrary GUID return was all 0’s, which isn’t very valid now is it. Didn’t make a lot of sense to me, but apparently a q quick poll around the team makes me think that it isn’t entirely uncommon. So usually now, just for my own sanity, I test whether it is a valid GUID. I am wondering if other people do it. Or am I taking crazy pills? Like for example say you have any event handler where you are calling that on your item, sorta like this:

[csharp]

// Get the associated folder object for the item that is triggering the event
SPFolder curFolder = GetCurFolder(web, curItem);

// Get the GUID of the document library that contains the folders being created
Guid guid = curFolder.ContainingDocumentLibrary;

///

/// Get the current folder object from the SPListItem
///

/// The SPWeb object /// The SPListItem object /// SPFolder object
private static SPFolder GetCurFolder(SPWeb web, SPListItem curItem)
{
if (web == null)
throw new ArgumentNullException(“web”);
if (curItem == null)
throw new ArgumentNullException(“curItem”);

return web.GetFolder(curItem.Folder.Url);
}

[/csharp]

That return would obviously be a GUID. Do you test it for validity?

[csharp]

string guidString = guid.ToString();

// Run a test to see before the SPList object is used whether it is a valid GUID
if (!IsValidGuid(guidString))
{
Handler.WriteLine(TraceLevel.Error, string.Format(“An Invalid GUID Was Enountered When Hydrating The SPList Object”));
}

// Create an SPList object, and call the current list from the SPWeb by passing in the GUID harvested previously
SPList curList = web.Lists[guid];

///

/// Check whether a GUID presented is valid
///

/// ///
public static bool IsValidGuid(string guid)
{
if (guid == null)
throw new ArgumentNullException(“guid”);

try
{
new Guid(guid);
return true;
}
catch
{
return false;
}
}

[/csharp]

Seems redundant, since the GUID should always be valid, but it makes me feel all cozy. :)

Share

Always Test Whether Your GUID’s Are Valid!

It is a best practice to not blindly use GUID’s, you should always test whether they are valid or not. This proves helpful in several SharePoint helper methods when you are doing custom SharePoint development, and is something that should be implemented in your corporate standard shared libraries.
Considering that testing for the GUID is a relatively simple process, it is worth taking the time to make sure that you implement a try/catch around it, and pass in the GUID as a string parameter. In order to set up the test of whether a GUID is valid or not, you are going to set up static flagging method (a Boolean) that will take a GUID string as a parameter.
[csharp]
public static bool isMyGuidValid(string guid)
[/csharp]
Then, you must setup a try/catch block that in the try block will intialize a new instnace of the System.Guid class and pass in the string GUID. If it returns true, great!, the GUID is valid!
[csharp]
try
{
new Guid(guid);
return true;
}
[/csharp]
When the catch block is executed, that means the GUID is not valid, so simply return false.
[csharp]
catch
{
return false;
}
[/csharp]
Yeah, it is a pretty simple convention, but you would be surprised how often untested GUID’s are used within WebParts and other SharePoint development pieces. It is a better code practice to test though, especially when the condition that you are building is so easy to construct!

Share