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.
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;
- Create the Content Type using the SharePoint UI and export it, using for example Andrew Connells STSADM commands.
- Extract the Content Type XML using SharePoint Manager.
- 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.
Navigate to Site Settings – Site Content Types under the Galleries section and your new Content Type is ready for use.
So lets try it out.
- Create a new Custom List
- Navigate to the List Settings and choose Advanced Settings under General Settings.
- On the Content Types section, choose Yes for Allow Management of Content Types.
- Click OK.
- Now choose Add from existing content types
- Click Add
- Click OK
You’ll notice that the List Settings now includes the Content Types site columns in its Columns section.
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”.
The form used to edit the Content Types columns is generated by SharePoint at runtime, but you can customize these or provide your own.
tnx phil its really helpful.God bless u…
I’ve created a small tool to generate a feature with multiple contenttypes and document templates.
Please take a look at http://spfg.codeplex.com and give your comments.
Stefh, great, thanks for sharing