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. |
For more information, see Using Features in SharePoint Foundation.
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.
For more information, see Using Features in SharePoint Foundation.
The value that you use for the ID attribute has a very specific format. For more information, see Content Type IDs. |
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. |
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="http://schemas.microsoft.com/sharepoint/"> <!-- 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.
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
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.