Create Content Type Using Schema

Activities Summary

The following are our activities:

  1. Create Project in Visual Studio
  2. Define Content Type Fields
  3. Deploy Solution

Procedure

Create a new project in Visual Studio. Choose Farm Solution. Then choose Add New Item > Content Type from the Office/SharePoint category of templates.

content type

Inherit from Item as parent.

choose content type setting

Close the designer view of the content type and use Solution Explorer to open the XML view.

elements

You will get the following view.

xmal code

Replace the content with the following.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  3.   <Field ID="{00D74D75-B775-4222-967D-D26973B8C5DB}" Name="CustomField" DisplayName="Custom Field" Type="Text"/>  
  4.   <Field ID="{C3CD561C-D714-42DE-8112-C830B4108CEF}" Name="CustomChoiceField" DisplayName="Custom Choice Field" Type="Choice">  
  5.     <CHOICES>  
  6.       <CHOICE>Choice 1</CHOICE>  
  7.       <CHOICE>Choice 2</CHOICE>  
  8.       <CHOICE>Choice 3</CHOICE>  
  9.     </CHOICES>  
  10.   </Field>  
  11.   <!-- Parent ContentType: Item (0x01) -->  
  12.   <ContentType ID="0x0100CB3ADB9F2D014E3F920C47C0B14D25BD" Name="SchemaCT" Group="Custom Content Types" Description="My Content Type" Inherits="TRUE" Version="0">  
  13.     <FieldRefs>  
  14.       <FieldRef ID="{00D74D75-B775-4222-967D-D26973B8C5DB}" Name="Custom Field" DisplayName="Custom Field"/>  
  15.       <FieldRef ID="{C3CD561C-D714-42DE-8112-C830B4108CEF}" Name="Custom Choice Field" DisplayName="Custom Choice Field"/>  
  16.       <FieldRef ID="{1DAB9B48-2D1A-47b3-878C-8E84F0D211BA}" DisplayName="$Resources:core,Status;" Name="_Status"/>  
  17.     </FieldRefs>  
  18.   </ContentType>  
  19. </Elements>  
Let us try to understand what each XML block does: 
  • The Elements section specifies the XML namespace URL. We can include Field definitions here. Fields are first defined and later referenced.

  • The first field is of type Text. We need to specify the GUID for each Field.

  • The second field is of type Choice. The Choice values are specified in the child tag.

  • The section Content Type specifies information on the content type like Name, Description, Group, Version and so on.

  • The FieldRefs section is important since it actually specifies the fields used in the content type. Please note that we are referring to the fields here that we defined previously.

  • Each Field Reference should include the ID, Name and Display Name of it. We can also refer to existing Site Columns here using the ID. We are using 2 custom fields and 1 existing site column here.

Now save the changes and Deploy the project. Once the deployment succeeds, open SharePoint to view the new content type in the Site Settings > Site content types page.

schemaCT

You can see the columns being shown up there.

Deploy the project

This concludes our content type creation using XML Schema. If you wanted to create various field types, please see the field types in the references section.

References

SPFieldType enumeration.

Summary

In this article we have explored how to create a content type using XML Schema.