Creating a SharePoint Content Type using a Feature


Following on from this post in which I created Site Columns using a feature, in this post we will create a Content Type based on these site columns.

For an introduction to Content Types see the MSDN site.

Building on the solution in the previous post, we will create a Content Type called “My Site Content Type” using the site columns created there.

Add a new XML file called mysitecontenttype.xml to the MySiteTypes folder as shown below.

sct01

Open the new XML file, delete it’s default content and drop in the following Content Type XML.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
	<ContentType ID="0x0100928100FAB05A4148BD6F4C8E6A716B40"
					 Name="My Site Content Type"
					 Description="My Site Content Type"
					 Group="My Group">
		<FieldRefs>
			<FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Required="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE" />
			<FieldRef ID="{918BA6B8-60BC-417e-AEE5-F9E1A0A59144}" Name="MyReference" Required="TRUE" />
			<FieldRef ID="{1FDC83BE-D596-45e2-9DD3-AD3338798784}" Name="MyType" Required="TRUE" />
			<FieldRef ID="{AB31BEFA-A178-4a1e-B324-B73E9C15D5A8}" Name="MyDetails" Required="TRUE" />
		</FieldRefs>
		<XmlDocuments>
			<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
				<FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
					<Display>ListForm</Display>
					<Edit>ListForm</Edit>
					<New>ListForm</New>
				</FormTemplates>
			</XmlDocument>
		</XmlDocuments>
	</ContentType>
</Elements>

You can create this XML in a few ways;

  1. Create the Content Type using the SharePoint UI and export it, using for example Andrew Connells STSADM commands.
  2. Extract the Content Type XML using SharePoint Manager.
  3. Create it manually.

If you use the first approach, ensure that the XmlDocument element has the NamespaceURI attribute shown, if you don’t, you may experience the same problem I had when I failed to do this, discussed here.

If you use SharePoint Manager, you will need to change the Field elements to FieldRef elements.

In either case you must ensure that the ID attributes have unique values. If you exported the content type from SharePoint to package it into a feature then you should certainly change the the content type ID attribute value.

Having created the Content Type, we now need to modify the Feature manifest file, feature.xml, to tell it about the Content Type. Open the feature.xml file and add an ElementManifest element, as shown below.

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="adc873b4-fe40-4169-8304-c9a1dd697c2b"
          Title="MySiteTypes"
          Description="Description for MySiteTypes"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Site"
			 ActivateOnDefault="FALSE"
			 ImageUrl="GenericFeature.gif"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="sitecolumns.xml"/>
    <ElementManifest Location="mycontenttype.xml"/>
  </ElementManifests>
</Feature>

Use the WSPBuilder Build and Deploy commands to build and deploy to SharePoint.

After the solution has been deployed, browse to your SharePoint Site Collection and go to Site Settings – Site Collection Features and activate the MySiteTypes feature.

sc05

Navigate to Site Settings – Site Content Types under the Galleries section and your new Content Type is ready for use.

sct02

So lets try it out.

  1. Create a new Custom List
  2. Navigate to the List Settings and choose Advanced Settings under General Settings.
  3. On the Content Types section, choose Yes for Allow Management of Content Types.
  4. Click OK.

sct03

  1. Now choose Add from existing content types

sct04

  1. Click Add
  2. Click OK

You’ll notice that the List Settings now includes the Content Types site columns in its Columns section.

sct05

Modify the default view to include those columns. Now when you add a new list item you will see the option to add a new “My Site Content Type”.

sct06

The form used to edit the Content Types columns is generated by SharePoint at runtime, but you can customize these or provide your own.

sct07

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

5 thoughts on “Creating a SharePoint Content Type using a Feature

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s