Thursday, March 28, 2013

Package and Delpoy Content Types as Feature Based Solutions in SharePoint 2010

Task: Create a feature based solution package to pack and deploy the content type from DEV environment to STAGING environment in SharePoint 2010.

Although SharePoint 2010 supports content type hub, where content types of one site collection can be used by other site collections/web application/even Farms, we needed an isolation and wanted to pack & deploy content types between different environments.

Solution: Create a Visual Studio project to pack the content type as a feature based solution package (wsp). Lets pack the Content Type "Crescent Travel Request" with Few Columns & Form Template. Here is the step by step guide on Creating Visual Studio Solution for Content Types in SharePoint 2010: 

Create a Visual Studio Project To Package Content Type as a Feature based solution:
1. Create a New Visual Studio 2010 Project of "Empty SharePoint Project" Type, Give it a name, say: "Crescent.TravelRequest.CotentType"

Creating Visual Studio Solution for Content Types in SharePoint 2010
Make it as a Farm Solution and Specify the site for debugging. Click Finish.
Build a deployable Content Type for SharePoint 2010

2. Go to Server Explorer from Visual Studio 2010 (View >> Server Explorer), Expand the nodes and Navigate to your desired content type, In our case it is: Crescent Travel Request. Right click and choose "Import Content Type"
How to Deploy Content Types in SharePoint 2010
This will create a feature "Feature1" and a Content Type element with the selected content type's schema XML. Rename the Feature1 to "Travel Request".
sharepoint 2010 content type deployment
Crescent Content Type's Elements.xml
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ContentType ID="0x01010100C977E928AAE65D498957261048306247" Name="Crescent Travel Request" Group="Crescent Content Types" Inherits="true" Hidden="false" ReadOnly="false" Sealed="false">
    <FieldRefs>
      <FieldRef ID="002acd66-abf3-4cca-9a92-2fe8d93a3de2" Name="Traveller_x0020_Name" DisplayName="Traveller Name" />
      <FieldRef ID="48d47953-dd68-4a51-8990-768438b908e8" Name="Project_x0020_Manager" DisplayName="Project Manager" />
      <FieldRef ID="c600e139-1c03-4524-8ef9-72d8462e3992" Name="Travel_x0020_Date" DisplayName="Travel Date" />
      <FieldRef ID="e9d07d8f-9066-47ae-9d0e-7249386cde5b" Name="Travel_x0020_Coordinator" DisplayName="Travel Coordinator" />
      <FieldRef ID="839c026b-15b4-4d81-8c1a-bb7e53be7120" Name="Origin_x0020_Country" DisplayName="Origin Country" />
      <FieldRef ID="737c35ae-0b8a-494d-baf1-dfcf4271ad53" Name="Origin_x0020_City" DisplayName="Origin City" />
      <FieldRef ID="2489cb17-cace-48bc-939e-438d458d90b0" Name="Destination_x0020_Country" DisplayName="Destination Country" />
      <FieldRef ID="4820f860-1512-44ec-bdb0-2c0994d0c6c9" Name="Destination_x0020_City" DisplayName="Destination City" />
      <FieldRef ID="2ae77016-bd02-4bd4-95b3-250df489ebde" Name="Travel_x0020_Desk_x0020_Comments" DisplayName="Travel Desk Comments" />
      <FieldRef ID="2b41e4b5-2bce-4a6d-9167-0d3b38b89917" Name="Approval_x0020_Status" DisplayName="Approval Status" />
    </FieldRefs>
    <DocumentTemplate TargetName="TravelRequest.xsn" xmlns="http://schemas.microsoft.com/sharepoint/" />
    <XmlDocuments xmlns="http://schemas.microsoft.com/sharepoint/">
      <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
        <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
          <Display>DocumentLibraryForm</Display>
          <Edit>DocumentLibraryForm</Edit>
          <New>DocumentLibraryForm</New>
        </FormTemplates>
      </XmlDocument>
    </XmlDocuments>
  </ContentType>
  
</Elements>

3. Now, We got the content type with the references to Fields and Document Template. Since the columns (or fields) referenced in our content type are not default OOTB site columns, We must package the site columns referenced in the content type. So, Right click the Project from server explorer, Choose Add New Item, and then select "Empty Element"
sharepoint 2010 content type deploy
Like the content type, Fields also have schema XML. There are many ways to get Field's XML like using SharePoint Manager, Using PowerShell, etc. Lets use the OOTB way again: Server Explorer.
Deploying Content Types as a Feature
Copy the Field Schema from the Elements.xml of the field object, To the Element.xml file under "Columns" Object, and then delete the newly created Field object from Solution Explorer.

Repeat the above step for all fields referenced in the content type.
Create a Content Type using Features
Finally, the Elements.xml file under "Columns" will look like:
Columns Object's Elements.xml File:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field Type="Text" DisplayName="Traveller Name" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="Crescent Site Columns" ID="{002acd66-abf3-4cca-9a92-2fe8d93a3de2}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Traveller_x0020_Name" Name="Traveller_x0020_Name" Customization="" />
  <Field Type="User" DisplayName="Project Manager" List="UserInfo" Required="FALSE" EnforceUniqueValues="FALSE" ShowField="ImnName" UserSelectionMode="PeopleOnly" UserSelectionScope="0" Group="Crescent Site Columns" ID="{48d47953-dd68-4a51-8990-768438b908e8}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Project_x0020_Manager" Name="Project_x0020_Manager" Customization="" />
  <Field Type="DateTime" DisplayName="Travel Date" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="DateOnly" Group="Crescent Site Columns" ID="{c600e139-1c03-4524-8ef9-72d8462e3992}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Travel_x0020_Date" Name="Travel_x0020_Date" Customization="" />
  <Field Type="User" DisplayName="Travel Coordinator" List="UserInfo" Required="FALSE" EnforceUniqueValues="FALSE" ShowField="ImnName" UserSelectionMode="PeopleOnly" UserSelectionScope="0" Group="Crescent Site Columns" ID="{e9d07d8f-9066-47ae-9d0e-7249386cde5b}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Travel_x0020_Coordinator" Name="Travel_x0020_Coordinator" Customization="" />
  <Field Type="Text" DisplayName="Origin Country" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="Crescent Site Columns" ID="{839c026b-15b4-4d81-8c1a-bb7e53be7120}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Origin_x0020_Country" Name="Origin_x0020_Country" Customization="" />
  <Field Type="Text" DisplayName="Origin City" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="Crescent Site Columns" ID="{737c35ae-0b8a-494d-baf1-dfcf4271ad53}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Origin_x0020_City" Name="Origin_x0020_City" Customization="" />
  <Field Type="Text" DisplayName="Destination Country" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="Crescent Site Columns" ID="{2489cb17-cace-48bc-939e-438d458d90b0}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Destination_x0020_Country" Name="Destination_x0020_Country" Customization="" />
  <Field Type="Text" DisplayName="Destination City" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="Crescent Site Columns" ID="{4820f860-1512-44ec-bdb0-2c0994d0c6c9}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Destination_x0020_City" Name="Destination_x0020_City" Customization="" />
  <Field Type="Note" DisplayName="Travel Desk Comments" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" NumLines="6" RichText="TRUE" RichTextMode="FullHtml" IsolateStyles="TRUE" Sortable="FALSE" Group="Crescent Site Columns" ID="{2ae77016-bd02-4bd4-95b3-250df489ebde}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Travel_x0020_Desk_x0020_Comments" Name="Travel_x0020_Desk_x0020_Comments" Customization="" />
  <Field Type="Text" DisplayName="Approval Status" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="Crescent Site Columns" ID="{2b41e4b5-2bce-4a6d-9167-0d3b38b89917}" SourceID="{2a4152d0-7cf4-4014-997f-0fce5e803436}" StaticName="Approval_x0020_Status" Name="Approval_x0020_Status" Customization="" />
</Elements> 

4. The next thing we need to do is: Pack the Document template associated with out content type. Add New Item to the project, Choose "Module" and name it. I've named it as "FormTemplate"
Custom Content Type using Visual Studio
Now from Solution Explorer, Right click the Form Template, Add >> Exising Item >> Specify the Form Template to Add.
Provision Cotent Type with document template in Visual Studio
Update the Elements.xml file under "FormTemplate" as below:
FormTemplate Module's Elements.xml file:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="FormTemplate">
    <File Path="FormTemplate\TravelRequest.xsn" Url="_cts/Crescent Travel Request/TravelRequest.xsn" Type="Ghostable" />
</Module>
</Elements>

5. Done, We have the Content Type, Columns and the Document templates referenced by the content type. Now the project structure should look like:
sharepoint 2010 content type feature
Go to the feature designer, Include above three objects to the feature.
sharepoint 2010 content type wsp
Build, Pack and Deploy the Project! That's all!! We are done packaging and deploying content types with document template using Visual Studio 2010.

While its also possible to have a single Elements.xml file for all these artifacts, I've separated them into three for the sake of simplicity!

In this demo, I've selected "Blank SharePoint Project", But you can choose "Content Type" Project and copy paste the Content Type & Fields Schema XML)
Export Import Content Types using Visual Studio

Export Import Content Types with PowerShell:
Here is my another post on Exporting and Importing Content Types using PowerShell



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...