Manipulating Content Types Using the Object Model

Is really simple! Here is a small sample where I extend the content type ”Document” by adding the field ”Office”. Not really useful but shows how it’s done.

 

SPSite site = new SPSite("http://moss.litwareinc.com");
SPWeb web = site.OpenWeb("/SiteDirectory/t2");

//Create Content Type (extending Document content type)
SPContentType documentContentType = web.AvailableContentTypes["Document"];
SPContentType newContentType = new SPContentType(documentContentType, 
web.ContentTypes, "Niklas Document"); web.ContentTypes.Add(newContentType); Console.WriteLine(newContentType.Id.ToString()); //Add FieldLink SPField fieldOffice = web.AvailableFields["Office"]; SPFieldLink fieldLink = new SPFieldLink(fieldOffice); newContentType.FieldLinks.Add(fieldLink); newContentType.Update(false);

2 thoughts on “Manipulating Content Types Using the Object Model

  1. Uhm, I understand that it’s all about SharePoint but what does the code snippet actually achieve? 🙂

  2. Hum, maybe I should write a post on what content types are…
    In short what the code archives is that it adds a new content type that inherits from the already existing content type ”Document” and adds on a field called office. Not very useful maybe but it shows how to get in touch with content types through the object model.

Comments are closed.