Posted by Adam Buenz

A friend just asked me the way to cast a SPList To a SPDocumentLibrary so that he could access the document library specific attributes for document library objects. This is one way of doing it:

C#:
  1. public static SPWeb GetWeb(string url)
  2. {
  3. using (SPSite site = new SPSite(url))
  4. {
  5. using (SPWeb web = site.OpenWeb())
  6. {
  7. return web;
  8. }
  9. }
  10. }
  11.  
  12. public static SPDocumentLibrary CastDocumentLibrary(string url, string libraryName)
  13. {
  14. return GetWeb(url).Lists[libraryName] as SPDocumentLibrary;
  15. }

Obviously, the casting can take on many forms, but the end result is still an SPDocumentLibrary object.

Leave a Reply