Create ContentType programatically

by Håvard Hebnes 14. July 2009 21:49

Out of the box SharePoint has a few content types that you can use. If you would like to create your own that inherits from one of these you could do it like this:

// first we reference the content type we want to inherit from
SPContentType pageContentType = web.AvailableContentTypes[ContentTypeId.Page];

// create our new content type
var customContentType = new SPContentType(pageContentType, web.ContentTypes, "Name of content type");
newsContentType.Group = "Custom content type group";

// add content type to the site
web.ContentTypes.Add(customContentType);
web.Update();

To add a custom sitecolumn to our new content type:

// first we need to find our site column
SPField siteColumn = web.AvailableFields.GetFieldByInternalName("InternalSiteColumnName");

// Add column to our new content type
customContentType.FieldLinks.Add(new SPFieldLink(siteColumn));

// Update content type
customContentType.Update();

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen