SharePoint 2013 - SPLessons

Programmatically Creating a Content Typ...

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Programmatically Creating a Content Type in SharePoint

Description :

Hello Every one, Today we are going to see Programmatically Creating a Content Type in SharePoint. Content Types can be created through OOB, Programmatically and Using Power shell.

What is Content Type :

Content types are a core organizational feature of SharePoint 2010. They are designed to give users the ability to organize SharePoint content in a centralized and meaningful manner. Site columns (metadata) can be encapsulated within a content type to allow for reusable structure or independently added to sites and lists. At its most basic level, a content type is a collection of settings which can be applied to content. They are reusable since content types are independent of sites and lists. A content type includes site columns to define the desired information. Example: The Task content type includes site columns for Task Status, Start Date and Due Date where the Schedule content type includes site columns for Location, Start Time and End Time. Using these content types as a template when a new task or event list needs to be created is much more efficient than rebuilding separate lists from site columns.

creation of Content types can be done in 2 ways through programmatically :

1. Using Declarative XML
2. Through code (Feature Activation Event Receiver)

In current post we are going to look on
Declarative XML

Step1 :

Open Visual Studio in Admin Mode.

Step2 :

Create an Empty Share Point Project

Step3 :

Add a New Item by choosing Template of Type “Empty Element” and name it as “ContentTypes” in our case.(you can give as per your choice) When you open ContentTypes.XML file it will look as follows,

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
//We have to define our content types over here...
</Elements>

In our example , i took Item(0x01) as Parent Content Type.


<!-- Parent Content Type Item:(0x01) -->
<ContentType ID="0x0100da3896467d2d4af085f58a0fa1c22703"
             Inherits ="TRUE"
             Group ="MyCTGrp"
             Name ="CT1"
             Overwrite ="TRUE"
             Description ="This content type is used for the....">
      <FieldRefs >
          <FieldRef ID ="{860deaeb-80cd-4a22-ab0d-31e49b67ac90}"
                    Name ="SiteCol1"
                    DisplayName ="SiteCol1"
                    Required ="TRUE" />

          <FieldRef ID ="{b287cd46-13b5-4a56-8a18-c0247804aa96}"
                    Name ="SiteCol2"
                    DisplayName ="SiteCol2"
                    Required ="TRUE" />

         <FieldRef ID ="{5e86344f-8a18-4e72-9117-bcee35b72430}"
                   Name ="SiteCol3"
                   DisplayName ="SiteCol3"
                   Required ="FALSE" />
     </FieldRefs>
  </ContentType>

<!-- Parent Content Type Video:(0x0101009.....) -->
<ContentType   ID="0x0101009148F5A04DDD49CBA7127AADA5FB792B00291D173ECE694D56B19D111489C4369D0002944d4fe73b47c4b4fc617611fa4d52"  Inherits ="TRUE"
Group ="MyCTGrp"
Name ="CT2"
Overwrite ="TRUE"
Description ="This content type is used for the video">
    <FieldRefs >
      <FieldRef ID ="{de2f4df0-b742-40ee-86d7-2fcd05ecbd8e}"
                Name ="SiteCol4"
                DisplayName ="SiteCol4"
                Required="FALSE" />
    </FieldRefs>
  </ContentType>

If you want to create a content type and want to make a document as a Parent. The logic is simple for that we have to give Parent Content Type ID+00+GUID

New Content Type ID= Parent Content type ID +00+GUID

Once your declaration is completed just build and deploy the solution
Navigate to your site to see the Content types.

Happy Sharing 🙂