kamagra how much to take

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:

  1. // Get the associated folder object for the item that is triggering the event
  2. SPFolder curFolder = GetCurFolder(web, curItem);
  3.  
  4. // Get the GUID of the document library that contains the folders being created
  5. Guid guid = curFolder.ContainingDocumentLibrary;
  6.  
  7. /// <summary>
  8. /// Get the current folder object from the SPListItem
  9. /// </summary>
  10. ///
  11. <param name="web">The SPWeb object</param>
  12. ///
  13. <param name="curItem">The SPListItem object</param>
  14. /// <returns>SPFolder object</returns>
  15. private static SPFolder GetCurFolder(SPWeb web, SPListItem curItem)
  16. {
  17. if (web == null)
  18. throw new ArgumentNullException("web");
  19. if (curItem == null)
  20. throw new ArgumentNullException("curItem");
  21.  
  22. return web.GetFolder(curItem.Folder.Url);
  23. }

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

  1. string guidString = guid.ToString();
  2.  
  3. // Run a test to see before the SPList object is used whether it is a valid GUID
  4. if (!IsValidGuid(guidString))
  5. {
  6. Handler.WriteLine(TraceLevel.Error, string.Format("An Invalid GUID Was Enountered When Hydrating The SPList Object"));
  7. }
  8.  
  9. // Create an SPList object, and call the current list from the SPWeb by passing in the GUID harvested previously
  10. SPList curList = web.Lists[guid];
  11.  
  12. /// <summary>
  13. /// Check whether a GUID presented is valid
  14. /// </summary>
  15. ///
  16. <param name="guid"></param>
  17. /// <returns></returns>
  18. public static bool IsValidGuid(string guid)
  19. {
  20. if (guid == null)
  21. throw new ArgumentNullException("guid");
  22.  
  23. try
  24. {
  25. new Guid(guid);
  26. return true;
  27. }
  28. catch
  29. {
  30. return false;
  31. }
  32. }

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

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>