Create Custom Content Types in SharePoint 2010

SharePoint 2010

SharePoint QuickStart Banner

Getting Started with Web Development in SharePoint 2010:  Learn how to create a custom content type in SharePoint 2010.

Last modified: January 12, 2011

Applies to: SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2010

In this article
Create a SharePoint 2010 Project
Create a Content Type
Verify that the Project Works Correctly
Next Steps

Published:  April 2010

Provided by:   Frank Rice, Microsoft Corporation

In this exercise, you create a custom content type. Then you add two fields to the content type: a new text field and a field that already exists in Web site. To complete this task, you must do the following:

In this task, you create an empty SharePoint 2010 project in Microsoft Visual Studio 2010.

To create the SharePoint project

  1. To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.

  2. On the File menu, point to New, and then click Project.

  3. In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.

  4. Select Empty SharePoint Project from the project items.

  5. In the Name box, type CreateContentType and then click OK.

  6. In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (such as http://localhost/SampleWebSite).

  7. For the trust level, select Deploy as a farm solution and then click Finish.

In this task, you create the content type as a feature and add an event receiver.

To create a content type

  1. Right-click the Features folder in Solution Explorer and then click Add Feature.

  2. Right-click Feature1 and then click Add Event Receiver. Visual Studio adds a feature event receiver to Feature1.

  3. Right-click Feature1.EventReceiver.cs and then click View Code.

  4. Uncomment the FeatureActivated method in the Feature1EventReceiver class.

  5. Insert the following code in the FeatureActivated method.

    using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
        SPContentType newAnnouncement = spWeb
            .FirstOrDefault(c => c.Name == "New Announcements");
        if (newAnnouncement != null)
        SPField newField = spWeb.Fields
            .FirstOrDefault(f => f.StaticName == "Team Project");
        if (newField != null)
        SPContentType myContentType = 
            new SPContentType(spWeb.ContentTypes["Announcement"], 
                spWeb.ContentTypes, "New Announcements");
        myContentType.Group = "Custom Content Types";
        spWeb.Fields.Add("Team Project", SPFieldType.Text, true);
        SPFieldLink projFeldLink = new SPFieldLink(spWeb.Fields["Team Project"]);
        SPFieldLink companyFieldLink = new SPFieldLink(spWeb.Fields["Company"]);

    The FeatureActivated method is run when Feature1 is started. This code does the following:

    • Deletes the content type New Announcements and the field Team Project, if they exist.

    • Creates a parent content type Announcement based on the New Announcements content type.

    • Creates a text field, which is titled Team Project, and then adds it to the content type.

    • Adds an existing field, which is titled Company, to the content type.

  6. Uncomment the FeatureDeactivating method.

  7. Insert the following code in the FeatureDeactivating method.

    using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
        SPContentType myContentType = spWeb.ContentTypes["New Announcements"];
        spWeb.Fields["Team Project"].Delete();

    The FeatureDeactivating method is run when Feature1 is deactivated. This code does the following:

    • Deletes the content type New Announcements.

    • Deletes the text field Team Project.

  8. In Solution Explorer, right-click CreateContentType and then click Deploy.

In this task, you verify the presence of the content type and the two fields.

To test the project

  1. Start Internet Explorer and browse to the Web site that you specified previously.

  2. At the upper-left section of the screen, click Site Actions, and then click Site Settings.

  3. Under Galleries, click Site Columns.

  4. In the Show Group options, click Custom Columns.

    You should see the new field Team Project.

    Figure 1. Team Project field

    Team Project field
  5. Click Site Actions and then click Site Settings.

  6. Under Galleries, click Site content types.

  7. From the Show Group options, select Custom Content Types.

    You should see the new content type New Announcements.

    Figure 2. New Announcements content type

    New Announcements content type