Monday, October 5, 2009

Programmatically Get Content Types

Title: You might want to get all the content types on a given site

Code:

static void GetListTypeID()
{
using (SPSite oSite = new SPSite(@"http://localhost/"))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPContentTypeCollection ctypecoll = oWeb.ContentTypes;
foreach (SPContentType type in ctypecoll)
{
Console.WriteLine("ID: " + type.Id + " ; Name: " + type.Name);
}
}
}
}

2 comments:

Sandeep K Nahta said...

Also its worth mentioning the difference b/w
web.ContentTypes and web.AvailableContentTypes

You might get access denied using the 1st one

Srini Sistla said...

here you go :)
SPWeb.AvailableContentTypes Vs SPWeb.ContentTypes