Wednesday, November 26, 2008

Adding content type programatically

Scenario:
You want to add Content Type programatically

Solution:
SharePoint Object Model

Code:

using (SPSite site = new SPSite("http://localhost")) { 

using (SPWeb web = site.OpenWeb()) {

SPContentType baseType = web.AvailableContentTypes["Document"];

SPContentType proposal = new SPContentType( baseType, web.ContentTypes, "Project Proposal");

proposal.FieldLinks.Add(new SPFieldLink(web.AvailableFields["Author"]));

web.ContentTypes.Add(proposal);

}
}
Article:

0 comments: