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:
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.
3 Comments »
RSS feed for comments on this post. TrackBack URL





















Articles & Research
SharePoint Architecture
Personal/Off-Topic
Latest Free SharePoint Software
SecureCenter For SharePoint
SharePoint Security Assurance Program™
Free Online SharePoint Security Tools
Online SharePoint Security Health Assessment
Article Or Research Filed Under 
Isn’t GetWeb() returning a reference to a disposed object?
Comment by Viktor Dolezel — November 19, 2008 @ 8:49 am
The expression in the return statement is evaluated before the object is disposed.
Comment by Adam Buenz — November 19, 2008 @ 9:14 am
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;
}
Comment by Keith Dahlby — January 18, 2009 @ 4:06 pm