Wednesday, May 20, 2009

How to check whether a List is a Document Library?

Many times, we face a situation where we iterate through list collection. Now if we need to check whether a list is a SPDocumentLibrary or not, we can check it in very simple manner:

Suppose we need to display list of document libraries present in a site in a dropdown control named 'lstTargetLibrary', we will iterate through list collection and check for List type:


SPWeb site = SPContext.Current.Web;
foreach (SPList list in site.Lists) {
if (list is SPDocumentLibrary && !list.Hidden) {
SPDocumentLibrary docLib = (SPDocumentLibrary)list;

lstTargetLibrary.Items.Add(
new ListItem(docLib.Title, docLib.ID.ToString()));
}
}

No comments: