SharePointAds TextOnly

Monday 3 May 2010

Populate all Sharepoint Document Library names which has my custom Content types

Today I populated all document library names which has my custom content type in drop down list control in web part.
(So before start further, I am assuming that you have created any document library with custom content type in visual studio 2010.)
Lets starts with creating simple web part template in VS 2010 and on user control page (in .ascx) add ASP.net dropdown control with name 'ddlCategory'

<asp:DropDownList ID="ddlCategory" runat="server" > </asp:DropDownList>


and on usercontrol.ascx.cs page, add following function code on 'page_load'


public void PopulateDocNames()
{
ArrayList listWithMyCT = new ArrayList();
using (SPSite objSite = new SPSite("http://YOURSITE:PORT/"))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
foreach (SPList list in objWeb.Lists)
{
if (list is SPDocumentLibrary && list.ContentTypes["Custom_Content_Type"]!= null)
{
listWithMyCT.Add(list.Title);
}
}
}
}
ddlCategory.DataSource = listWithMyCT;
ddlCategory.DataBind();
}

Note: Change YOURSITE:PORT and Custom_Content_Type with your site URL with port and your custom content type name.



No comments:

Post a Comment