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#:
-
public static SPWeb GetWeb(string url)
-
{
-
{
-
using (SPWeb web = site.OpenWeb())
-
{
-
return web;
-
}
-
}
-
}
-
-
public static SPDocumentLibrary CastDocumentLibrary(string url, string libraryName)
-
{
-
return GetWeb(url).Lists[libraryName] as SPDocumentLibrary;
-
}
Obviously, the casting can take on many forms, but the end result is still an SPDocumentLibrary object.











November 19th, 2008 at 8:49 am
Isn't GetWeb() returning a reference to a disposed object?
November 19th, 2008 at 9:14 am
The expression in the return statement is evaluated before the object is disposed.
January 18th, 2009 at 4:06 pm
I believe Viktor is correct. GetWeb disassembled looks like this:
public static SPWeb GetWeb(string url)
{
SPWeb temp;
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
temp = web;
}
}
return temp;
}