Registering Content Types in a Site Definition Configuration
  • Share this blog:

To specify a content type to include in a custom site definition configuration, you first create the content type as a separate Feature, and then you reference that Feature in some Collaborative Application Markup Language (CAML) markup to the site definition configuration in the Onet.xml file of the site definition. Then, when a user creates a new site of that type, the content type is included by default in the site’s content type collection.

Modifying the Onet.xml file for any of the built-in site types of SharePoint Foundation is not supported, so you can use the following procedure only on custom site types. For more information about creating custom site definition configurations, see How to: Create a Custom Site Definition and Configuration.


To specify a content type in a site definition configuration

  1. Create the content type as a separate Feature.

For more information, see Using Features in SharePoint Foundation.

  1. Reference that Feature in theConfiguration element (in the Onet.xml file) that defines your custom site type:
    1. Open the Onet.xml file in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates\site_type\XML, wheresite_type is name of the custom site definition.
    2. In theConfiguration element, add a reference to the content type Feature in either the SiteFeatures orWebFeatures  You identify the Feature by its GUID. The following is an example.

XML: <SiteFeatures> <Feature ID="00BFEA71-1C5E-4A24-B310-BA51C3EB7A57" /> <Feature ID="695B6570-ACDC-4A8E-8545-26EA7FC1D162" /> </SiteFeatures> <WebFeatures> <Feature ID="00BFEA71-4EA5-48D4-A4AD-7EA5C011ABE5" /> <Feature ID="00BFEA71-E717-4E80-DEAF-D0C71B360101" /> </WebFeatures> For more information about the difference between these two elements, see SiteFeatures Element (Site)and WebFeatures Element (Site). Adding Content Types to an Existing Site You can add content types to a site by using either declarative XML or the SharePoint Foundation object model. Declarative XML uses the Content Type Definition Schema to define content types. Content type definitions are declared in the element manifest file of a Feature, and the content types are added to a site when the Feature is activated. When you use the SharePoint Foundation object model, you must subclass the SPFeatureReceiver class. Place the code that creates your content types and adds them to the site in the FeatureActivated method. Typically, declarative XML is easier to write. However, declarative XML generally offers less flexibility than the object model, which has access to the capabilities of the entire Microsoft .NET Framework and can be debugged at run time. Both approaches are supported by templates that are provided with SharePoint development tools in Microsoft Visual Studio 2010.

To add a content type by using declarative XML

  1. Create a Feature.

For more information, see Using Features in SharePoint Foundation.

  1. Add an element manifest file to the Feature.
  2. Add anElements element as the root XML element in the element manifest.
  3. Add aContentType element as the child of the Elements
The value that you use for the ID attribute has a very specific format. For more information, see Content Type IDs.
  1. Add the column references that you want. For more information, seeHow to: Reference a Column in a Content Type.
  2. Add aDocumentTemplate definition as desired.
The file that is referenced by the DocumentTemplate element must already exist on the site or must be included with the Feature. For more information, see How to: Provision a File.
  1. Complete the content type definition as desired. For more information, seeContent Type Definitions.

Example 1

The following example is the element manifest for a Feature. When the Feature is activated, the element manifest is used to create a site column and add it to the site column collection on the current site. Then the manifest is used to create a site content type and add it to the content type collection on the current site.   XML: <?xml version="1.0" encoding="utf-8"?> <Elements xmlns=""> <!-- Create a site column. --> <Field ID="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}" Name="Amount" DisplayName="Amount" Type="Currency" Decimals="2" Min="0" Required="FALSE" Group="Financial Columns" /> <!-- Create a site content type that uses the column. --> <!-- Parent ContentType: Document (0x0101) --> <ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e" Name="Financial Document" Group="Financial Content Types" Description="Base financial content type" Version="0"> <FieldRefs> <FieldRef ID="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}" Name="Amount" DisplayName="Amount" Required="FALSE"/> </FieldRefs> </ContentType> </Elements>

Learn the core features of SharePoint and become master with our expertise tutorials.

To add a content type by using server code

  1. Create a feature receiver class that inherits from theSPFeatureReceiver
  2. In theFeatureActivated method, use the ContentTypes property to access the content type collection for the site. This property returns an SPContentTypeCollection object.
  1. Create anSPContentType  For more information, see Creating Content Types.

Example 2

The following example shows the implementation of the FeatureActivated method in a subclass of theSPFeatureReceiver class. When the Feature is activated, code in the FeatureActivated method creates a site column and adds it to the site column collection on the current site. Then the code creates a content type that inherits from the Document content type and adds it to the content type collection on the current site.   public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite siteCollection = (SPSite)properties.Feature.Parent; SPWeb site = siteCollection.OpenWeb(); /* Create a site column. */ string amountFieldName = site.Fields.Add("Amount", SPFieldType.Currency, false); SPFieldCurrency amountField = (SPFieldCurrency)site.Fields.GetFieldByInternalName(amountFieldName); amountField.Group = "Financial Columns"; amountField.DisplayFormat = SPNumberFormatTypes.TwoDecimals; amountField.MinimumValue = 0; amountField.Update(); /* Create a site content type that uses the column. */ // Get a content type to be the parent of a new Financial Document content type. SPContentType documentCType = site.AvailableContentTypes[SPBuiltInContentTypeId.Document]; // Create the Financial Document content type. SPContentType financialDocumentCType = new SPContentType(documentCType, site.ContentTypes, "Financial Document"); site.ContentTypes.Add(financialDocumentCType); // Note: A content type is not initialized until after it is added. financialDocumentCType = site.ContentTypes[financialDocumentCType.Id]; financialDocumentCType.Group = "Financial Content Types"; // Add the Amount column. Child content types inherit the column. SPFieldLink amountFieldRef = new SPFieldLink(amountField); financialDocumentCType.FieldLinks.Add(amountFieldRef); // Commit changes. financialDocumentCType.Update(); site.Dispose(); siteCollection.Dispose(); }


You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses


About Author
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated

Get stories of change makers and innovators from the startup ecosystem in your inbox