Creating Content Types for SharePoint 2010 in Visual Studio 2010

SharePoint Visual How To

Summary:  Learn how to create a Microsoft SharePoint 2010 content type by using Microsoft Visual Studio 2010.

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

Provided by:  Ben Hedges, Point8020

Overview

A content type is a reusable collection of settings that can be defined and applied to data in a Microsoft SharePoint list or library. Microsoft Visual Studio 2010 provides project types that support the creation and deployment of content types.

Code It

This SharePoint Visual How To walks you through the following high-level steps to show the process of creating and deploying a content type to a SharePoint 2010 site:

  1. Creating a new Content Type project in Visual Studio 2010.

  2. Editing the content type details.

  3. Adding a new field to the XML markup.

  4. Including a reference to the new field from within the content type element.

You will create a content type definition named Official Documents that is based on the Document content type. Then, you will add a choice column for Review Status, and deploy and use this content type.

To create a SharePoint 2010 content type application solution in Visual Studio 2010

  1. Start Visual Studio 2010.

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

  3. In the Installed Templates section, expand either Visual Basic or C#, expand SharePoint, and then click 2010.

  4. In the template pane, click Content Type.

  5. In the Name box, type OfficialDocuments.

  6. Leave other fields with their default values, and then click OK.

  7. In the What local site do you want to use for debugging? box, select your site.

  8. Select the Deploy as a farm solution box. Then, click Next.

  9. In the Choose Content Type Settings dialog box, in the Which base content type should this content type inherit from? list, select Document.

  10. Click Finish.

To edit the content type details in the Elements.xml file

  1. In Solution Explorer, expand ContentType1 and then open Elements.xml.

  2. Edit the Name attribute in the <ContentType> tag and type Official Documents.

  3. Edit the Description attribute in the <ContentType> tag and type Official Document Content Type.

    The markup should look similar to the following.

    Note

    The ContentType ID is a GUID and your ID will differ.

    <ContentType ID="0x01010055a134438a8544eaa18477c8f21894d0"
      Name="Official Documents"
      Group="Custom Content Types"
      Description="Official Document Content Type"
      Inherits="TRUE"
      Version="0">

To add a field to the Elements.xml file

  1. Above the <ContentType> tag, add the following markup. Notice that this includes a new field that will be identified with a new GUID. For this exercise, you can either use the GUID in the following code or create your own GUID.

    <Field ID="{30C3D21A-A7C9-410E-A896-82875475F697}" Name="ReviewStatus" DisplayName="Review Status" Type ="Choice" >
      <CHOICES>
        <CHOICE>Review Required</CHOICE>
        <CHOICE>Review in Progress</CHOICE>
        <CHOICE>Review Completed</CHOICE>
      </CHOICES>  
    </Field>
  2. Between the begin and end <FieldRefs> tags in the <ContentType> tag, add the following <FieldRef> tag. Ensure that the GUID matches that of the <Field> in the previous step.

    <FieldRef ID="{30C3D21A-A7C9-410E-A896-82875475F697}" 
      Name="ReviewStatus" DisplayName="Review Status"  />

To deploy the project

  1. In Solution Explorer, right-click the project, and then click Deploy.

  2. In SharePoint, in Quick Launch, click Shared Documents.

  3. On the Server ribbon, in the Library Tools tab group, click the Library tab.

  4. On the ribbon, in the Settings section, click Library Settings.

  5. In the General Settings section, click Advanced Settings.

  6. In the Content Types section, in the Allow Management of Content Types option, click Yes.

  7. At the bottom of the form, click OK.

  8. On the List Information page, in the Content Types section, click Add from existing site content types.

  9. In the Select site content types from drop-down list, click Custom Content Types.

  10. In the Available Site Content Types list, click Official Documents, and then click Add. Then, click OK.

  11. On the List Information page, in the Content Types section, click Change new button order and default content type.

  12. Clear the Visible check box for the Document content type, and then click OK.

To use the new content type

  1. In Quick Launch, click Shared Documents.

  2. In the list of items, click Add document.

  3. Browse to a document, click Open to upload it, and then click OK.

  4. In the dialog box, note the Review Status drop-down list that is associated with the Official Document content type.

  5. Save the document.

Read It

This Visual How To walks you through the following:

  • A content type is created by using a wizard in Visual Studio 2010.

  • The new content type has an ID that is formed by concatenating the parent content type identifier and a GUID that is generated by Visual Studio.

  • The <Field> tag is used to define a new field that will appear on a content type.

  • The ID of the Field should be set to a new GUID that will be unique for this field.

  • The Field Type is set to Choice, and then the choice options are added.

  • The ContentType element is then edited, so that it includes a <FieldRef> tag that specifies the new Field that has been defined by reference to the ID (GUID) of that new field. This ensures the field is included on this content type.

See It

Watch the video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/4d0c5966-9324-45f9-a6c0-ae3a2f9b5519]

Length: 00:04:02

Click to grab code

Grab the code

Explore It

About the Author

Community Contributor  Ben Hedges is Senior Vice President, Research and Development at Point8020. Ben is particularly interested in modeling Talent Management, Compliance, and Learning and Development solutions on SharePoint. Ben has a wealth of experience fulfilling customer requirements by using Microsoft technologies.