See how Microsoft Consulting Services can help you
Contact Microsoft Services
Contact The SharePoint Guys
Apply for a job at Microsoft Consulting Services
MCS Solution Dev
The Deployment Guys
MSDN UK Newsletter
MSDN UK Team Blog
TechNet UK Newsletter
TechNet UK Blog
I recently spent longer than I should have done trying to create what I thought was a fairly simple feature; one that adds a content type to a site collection and also adds a physical file as the document template. This is a common scenario when developing content types to be used in document libraries. The content type bit was easy, I had done that many times before, but adding the document template did take a while to figure out.
It was with a lot of help from my good friend and SharePoint development guru George Bonney that we managed to get it working so I thought I’d share our findings. George has also blogged about in a couple of related articles here and here.
In order to achieve this, you basically need two features:
· Content Type feature: This will create the content type and any site columns that needs to go with it.
· Modules feature: This will upload the document template and associate it with the content type.
As with most features, both of my features contain a feature.xml file and a manifest.xml file. I won’t go into detail about what these files do, if you are unsure, it might be worth having a look at these MSDN articles Feature.xml Files and Element Manifest (feature).
This is very simple, you can either do this via Visual Studio Extensions for WSS 1.1 and simply go to Add > New Item > Content Type, then fill in the gaps; or you can build the feature from scratch. For clarity, I’ll include steps to build your own.
These steps will basically create a simple Content Type called ‘MyContentType’ which will be in its own group, inherit from the default ‘Document’ content type.
1. Create a folder called MyContentType in C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\FEATURES on your SharePoint server
2. Create a new file called feature.xml
3. Create a Registry Format GUID for your feature; you can do this using GUIDGen.exe or http://createguid.com/
4. Add the following code to your feature.xml file. Make sure you replace the ‘<Content Type Feature GUID here>’ the actual GUID you generated in step 3 (do not include the curly braces in the GUID).
<?xml version="1.0" encoding="utf-8"?>
Id="<Content Type Feature GUID here>"
<ElementManifest Location="Manifest.xml" />
Now we just need to add the content type itself. There are several things to note regarding the ContentType element.
Firstly, the ID specifies the parentage of the content type. There is a whole MSDN article which outlines how Content Type IDs work here. In summary, the ID should be a combination of a new GUID (again, use GUIDGen.exe) and the ID of the parent content type. In this case we want to use the default Document content type as the parent; therefore we need to prefix our new GUID with 0x0101 which is the ID of the document content type.
5. Create a new file called Manifest.xml in the same folder as feature.xml and add the following code in to your Manifest.xml file. Be sure to replace the ContentType ID with ‘0x0101’ plus you own unique GUID, minus the dash symbols.
<ContentType Name="MyContentType" ID="0x0101FE67098470024e0b9272DF90F64061F6" Group="MyContentTypes" Version="0">
<DocumentTemplate TargetName="/_cts/MyContentType/MyDocumentTemplate.dotx" />
You will note that there is a DocumentTemplate element; this is the key line that maps the content type to the actual document template file. The TargetName property points to url of the document template file once it has been uploaded and uses the url of _cts/<name of the content type>/<name of file>. This Url is a hidden part of SharePoint which you can only see in either SharePoint Designer or by opening the site collection via a Network Place. Inside the _cts folder there is a folder that represents each content type in the site collection. These folders are called Resource Folders and are used to store resources for the corresponding content type.
The content type feature is now complete.
Now that the content type feature is complete, we can create the modules feature. This feature will actually upload the doucment template to eth right place in SharePoint (_cts/<name of the content type>)
1. Create a folder called MyModule in C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\FEATURES on your SharePoint server
4. Add the following code to your feature.xml file. Make sure you replace the ID for the Feature element with the GUID you generated in step 3. Also replace the FeatureId property in the ActivationDependency element with the Feature ID of the Content Type feature GUID (created in step 3 of the Creating the Content Type Feature section)
Id="C81844B4-06DB-478c-B01E-2761B0AEFD69" Title="MyModule" Scope="Site" Version="126.96.36.199" Hidden="FALSE" xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifest Location="Manifest.xml" />
<ElementFile Location="MyDocumentTemplate.dotx" />
<ActivationDependency FeatureId="<Content Type Feature GUID here>" />
There are several key things to note with the above code. Firstly you will note that there are two element manifests, the first point points to Manifest.xml which we will discuss in a moment, the second points to the *.docx file that will be the document template (it does not have to be *.docx, it can be any file type).
Secondly, you will notice that there is an activation dependency. This means that this feature (MyModule) will not activate unless the corresponding feature is already activated. In this case, the activation dependency is the MyContentType feature we created earlier. All you need to do is ensure that the FeatureId property is set to the ID of the MyContentType feature (from feature.xml). This is required because unless the content type exists the MyModule feature has nowhere to upload the document to.
5. Create a new file called Manifest.xml in the same folder as feature.xml and add the following code to Manifest.xml
<Module Name="MyModule" Url="_cts/MyContentType" RootWebOnly="TRUE">
<File Url="MyDocumentTemplate.dotx" Type="Ghostable" />
The Url property in the Module element points to the Resource Folder that the document template file needs to be uploaded to. The File element includes a Path that points to the actual file that you wish to upload (i.e. its location on the local disk on the SharePoint server).
8. Finally you need to actually add the MyDocumentTemplate.dotx file to the feature folder which will be C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\FEATURES\MyModule (just create any old file for this, so long as it has the same name).
In an ideal world these two features would be wrapped up into a SharePoint solution package; however in the interest of clarity, we’ll skip this steps and simply install and activate the features on their own.
1. On the SharePoint server open a command box and navigation to C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\bin
2. Enter and execute the following two commands (one for each feature):
Stsadm.exe –o installfeature –name “MyContentType”
Stsadm.exe –o installfeature –name “MyModule”
3. Now enter and execute the following two commands, replacing http://<your web application name> with the url of your web application:
Stsadm.exe –o activatefeature –name “MyContentType” –url “http://<your web application name>”
Stsadm.exe –o activatefeature –name “MyModule” –url “http://<your web application name>”
Your features are now activated on the site collection, your next step is to associate the content type with a document library and start creating documents.
4. Navigate to the document library you want to use your new content type and go to Settings > Library Settings > Advanced Settings. Enable ‘Allow Management of Content Types’.
5. Now add the content type by clicking ‘Add from existing site content types’ in the Content Types section of Settings > Library Settings for your library.
6. You should now see MyContentType in your libraries’ new menu
This article was published by
Martin Kearn Senior Consultant Microsoft Consulting Services UK Martin.Kearn@Microsoft.com
Click here for my bio page