Friday, March 25, 2011

Adding content type programatically

Scenario:
You want to add Content Type programatically

Solution:
SharePoint Object Model

Code:
  1. using (SPSite site = new SPSite("http://localhost")) {   
  2.   
  3. using (SPWeb web = site.OpenWeb()) {   
  4.   
  5. SPContentType baseType = web.AvailableContentTypes["Document"];  
  6.   
  7.  SPContentType proposal = new SPContentType( baseType, web.ContentTypes, "Project Proposal");   
  8.   
  9. proposal.FieldLinks.Add(new SPFieldLink(web.AvailableFields["Author"]));   
  10.   
  11. web.ContentTypes.Add(proposal);   
  12.   
  13. }  
  14. }  

No comments:

Post a Comment