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:
-
// 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;
-
-
/// <summary>
-
/// Get the current folder object from the SPListItem
-
/// </summary>
-
///
-
<param name="web">The SPWeb object</param>
-
///
-
<param name="curItem">The SPListItem object</param>
-
/// <returns>SPFolder object</returns>
-
private static SPFolder GetCurFolder(SPWeb web, SPListItem curItem)
-
{
-
if (web == null)
-
if (curItem == null)
-
-
return web.GetFolder(curItem.Folder.Url);
-
}
That return would obviously be a GUID. Do you test it for validity?
-
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];
-
-
/// <summary>
-
/// Check whether a GUID presented is valid
-
/// </summary>
-
///
-
<param name="guid"></param>
-
/// <returns></returns>
-
public static bool IsValidGuid(string guid)
-
{
-
if (guid == null)
-
-
try
-
{
-
return true;
-
}
-
catch
-
{
-
return false;
-
}
-
}
Seems redundant, since the GUID should always be valid, but it makes me feel all cozy. :)
Related posts:
- Always Test Whether Your GUID’s Are Valid!
- AspnetPathsDTO Partial Class
- AspnetPersonalizationperuserDTO Partial Class
- Removing Duplicate SPList Objects From Typed Collections
- AspnetUsers Partial Class
2 Comments »
RSS feed for comments on this post. TrackBack URL























Articles & Research
SharePoint Architecture
Research Methodology
Personal/Off-Topic
Article Or Research Filed Under 

Why not just check for an empty guid (the one with all zero’s) instead of bothering with going from Guid to String and back again?
if (guidValue == Guid.Empty)
{
//— do something else when you don’t get a ‘real’ guid value
}
Comment by BigJimInDC — August 14, 2007 @ 5:18 pm
[...] Should You Test SharePoint GUID’s? [...]
Pingback by Links (8/14/2007 « Steve Pietrek’s SharePoint Stuff) — August 14, 2007 @ 6:01 pm