Create Content Type Using Object Model

Procedure

Please use the following procedure to create the content type.

Step 1

reate a Console Application and add a reference to Microsoft.SharePoint.dll. Also ensure the Prefer 32-bit project property in the Build tab is unchecked.

build application

Step 2

Use the following code for the console application.

  1. using (SPSite site = new SPSite("http://hpvm"))  
  2. {  
  3.     using (SPWeb web = site.OpenWeb())  
  4.     {  
  5.         // Delete content type if already existss  
  6.         if (web.ContentTypes["CodeCT"] != null)  
  7.             web.ContentTypes["CodeCT"].Delete();  
  8.   
  9.         // Create content type inheritin from 'Item'  
  10.         SPContentType contentType = new SPContentType(web.ContentTypes["Item"], web.ContentTypes, "CodeCT");  
  11.         web.ContentTypes.Add(contentType);  
  12.         contentType.Group = "Custom Content Types";  
  13.         contentType.Description = "Content Type created through Code";  
  14.   
  15.         // Create Site Columns  
  16.         string fieldName = web.Fields.Add("Column 1", SPFieldType.Text, false);  
  17.         SPField field = web.Fields.GetFieldByInternalName(fieldName);  
  18.         field.Update();  
  19.   
  20.         fieldName = web.Fields.Add("Column 2", SPFieldType.Number, false);  
  21.         field = web.Fields.GetFieldByInternalName(fieldName);  
  22.         field.Update();  
  23.   
  24.         fieldName = web.Fields.Add("Column 3", SPFieldType.DateTime, false);  
  25.         field = web.Fields.GetFieldByInternalName(fieldName);  
  26.         field.Update();  
  27.   
  28.         // Add Field References to Our Site Columns  
  29.         contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField("Column 1")));  
  30.         contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField("Column 2")));  
  31.         contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField("Column 3")));  
  32.   
  33.         // Add few fields from OOB Site Columns  
  34.         contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField("_Status")));  
  35.   
  36.         // Update  
  37.         contentType.Update();  
  38.     }  
  39.   
  40.     Console.WriteLine("Content Type created successfully.");  
  41.      
  42. }  

 

Step 3

The code is executed below:

  1. Delete any content type with name CodeCT.
  2. Create new content type.
  3. Add custom field definitions to the web.
  4. Add custom field references to the content type.
  5. Add OOB column _Status to the content type.
  6. Updates content type.

Note: For custom columns, we are actually creating a Site Column through code (Fields.Add) and referring it to the content type (FieldLinks.Add).

Step 4

Execute the code.

Execute code

Step 5

You can verify that the new content type was created from Site Settings > Site content types.

CodeCT in sharepoint

Step 6

Click on it and ensure that all the columns are visible.

columns in sharepoint

Step 7

You can use the SPFieldType enum for finding more types.

/spFileType

References

How to: Programmatically Create Content Types.

Summary

In this article we explored how to create a content type using server object model code. The source code is attached for reference.

Up Next
    Ebook Download
    View all
    Learn
    View all