Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Create Content Type using Object Model

Posted by Paul on October 30, 2014


In this article we can try creating a Content Type using Server Object Model code.

Steps

Please follow the steps below to create the content type.

Create a Console Application & Add Reference to Microsoft.SharePoint.dll. Also make sure the project properties > Build tab > Prefer 32-bit is unchecked.

image

Use the following code for the console application.

using (SPSite site = new SPSite(“http://hpvm”))

{

using (SPWeb web = site.OpenWeb())

{

// Delete content type if already existss

if (web.ContentTypes[“CodeCT”] != null)

web.ContentTypes[“CodeCT”].Delete();

// Create content type inheritin from ‘Item’

SPContentType contentType = new SPContentType(web.ContentTypes[“Item”], web.ContentTypes, “CodeCT”);

web.ContentTypes.Add(contentType);

contentType.Group = “Custom Content Types”;

contentType.Description = “Content Type created through Code”;

// Create Site Columns

string fieldName = web.Fields.Add(“Column 1”, SPFieldType.Text, false);

SPField field = web.Fields.GetFieldByInternalName(fieldName);

field.Update();

fieldName = web.Fields.Add(“Column 2”, SPFieldType.Number, false);

field = web.Fields.GetFieldByInternalName(fieldName);

field.Update();

fieldName = web.Fields.Add(“Column 3”, SPFieldType.DateTime, false);

field = web.Fields.GetFieldByInternalName(fieldName);

field.Update();

// Add Field References to Our Site Columns

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Column 1”)));

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Column 2”)));

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Column 3”)));

// Add few fields from OOB Site Columns

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“_Status”)));

// Update

contentType.Update();

}

}

Console.WriteLine(“Content Type created successfully.”);

}

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

image

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

Execute the code.

image

You can verify the new content type being created or not from Site Settings > Site content types.

image

Click on it & ensure the all columns are visible.

image

You can use the SPFieldType enum for finding more types.

References

http://msdn.microsoft.com/en-us/library/ff798370.aspx

Summary

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s