27 February, 2007

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);

https://11011.net/software/vspaste


Tags: ,