Casting To SPDocumentLibrary

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:

[csharp]

public static SPWeb GetWeb(string url)
{
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
return web;
}
}
}

public static SPDocumentLibrary CastDocumentLibrary(string url, string libraryName)
{
return GetWeb(url).Lists[libraryName] as SPDocumentLibrary;
}

[/csharp]

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

Share