Content Types in SharePoint 2010 Via OM

Introduction

 

Today, in this article let's play around with one of the interesting and most useful concepts in SharePoint 2010.
 

Question: What is a content type?

 

In simple terms "Content types are reusable sets of columns which can be used across multiple lists or libraries. It is called central component to manage metadata".

 

I think we are now good to go and implement this wonderful concept.

 

Step 1: Open SharePoint 2010 Central Administration and navigate to a specific site.

 

 

Step 2: In Visual Studio 2010, create a "Content Type" application:

 

Ouput1.jpg
 

 

Step 3: Select "Deploy as a farm solution" and click the "Next  >" button:

 

Ouput2.jpg
 

 

Step 4: The base content type needs to be inherited from "Item":

 

Ouput3.jpg

 

Step 5: The complete code of elements.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
       <Field ID="{168076D8-500B-4572-B2FA-9CC9C4664F4C}" Type="Text" Name="FirstName" DisplayName="Firs tName" Required="TRUE" />
       <Field ID="{A97BDC8C-8723-455E-818C-56CE8D63BE19}" Type="Text" Name="LastName" DisplayName="Last Name" Required="TRUE" />
       <Field ID="{5744d18c-305e-4632-8bd1-09d134f4830d}" Type="Number" Name="Age" DisplayName="Age" Required="TRUE"></Field>
       <!-- Parent ContentType: Item (0x01) -->
       <
ContentType ID="0x01009592d006ac51409fa1483777c1efb4ff"Name="ContentApplication"Group="Custom Content Types"Description="My Content Type"Inherits="TRUE"Version="0">
              <FieldRefs>
                     <
FieldRef ID="{168076D8-500B-4572-B2FA-9CC9C4664F4C}" Name="FirstName" Required="TRUE" />
                     <FieldRef ID="{A97BDC8C-8723-455E-818C-56CE8D63BE19}" Name="LastName" Required="TRUE" />
                     <FieldRef ID="{5744d18c-305e-4632-8bd1-09d134f4830d}" Name="Age3" Required="TRUE" />
              </FieldRefs>
       </
ContentType>
</
Elements>

 

Step 6: Navigate to the site and select "Custom List".

 

Ouput4.jpg
 

 

Step 7: In the "Advanced Settings" for the list select the radio button to allow content management:

 

Ouput5.jpg
 

 

Step 8: Now let's try to add the custom content type that we have created:

 

Ouput6.png
 

 

Step 9: Select custom content type as the default:

 

Ouput7.png
 

 

Step 10: Modify the view of the custom list:

 

Ouput8.png
 

 

Step 11: Adding new item to the list:

 

Ouput9.png
 

 

Step 12: With the item added to the list, it looks like this:

 

Ouput10.jpg
 

 I hope this article is useful for you.


MVC Corporation
MVC Corporation is consulting and IT services based company.