In SharePoint, the content type is one of the most important components to any project that you may work on. Every artifact that exists in a SharePoint site has a content type; whether you specify one or not. Content Types provide artifacts with a variety of functional elements beyond meta-data. They also provide a platform on which you can attach a workflow, set policy, and provide custom forms for Office applications. In short, the content type is a piece of functionality that you associate with an artifact.
When viewed as a piece of functionality, it is easy to draw parallels between a content type and an object in object oriented programming. This would be an accurate way of looking at content types in SharePoint. If this is our point of view, then it makes sense to design and deploy content types as discreet, reusable units in our SharePoint installations. In SharePoint, this means using features to encapsulate functionality, and features to deploy units.
In this article, we will look at creating a content type in Visual Studio and we will encapsulate it in a feature. In the next article, we will wrap this feature in a SharePoint solution. And later, I will show you how to deploy the cotent type into your sites and lists. This article is focused on the step by step instructions for creating a content type and a feature. It provides no additional discussion about content types and features. So if you are like me and you want to understand the concepts behind the code, then you should spend some time reading the SDK articles on content types and features.
1. Create a Visual Studio Class Library project
Open Visual Studio and create a new project using the Class Library template. Once open, you may delete Class1.cs
2. Create a 12 hive in your project
Add folders to you project file, starting with a folder named ’12’. Then create a replica of the 12 hive’s features folder. Finally, add a feature folder that matches the name of your content type. In this example, I am creating a content type for a role-playing game character sheet.
3. Create a feature.xml file
Add a new xml file in your feature folder and call it ‘feature.xml’. In the feature xml file, add the tags to define a feature. Use the GUID generator tool that comes with Visual Studio to create a GUID for your feature and strip off the { }. Finally, add an ElementManifest that points to the contenttype.xml file that you will create in the next step.
4. Create a contenttype.xml file
Add a new xml file in your feature folder and call it ‘contenttype.xml’. In the contenttype.xml file, add an Elements tag and a CotentType tag. In a later article, I will discuss the creation of an ID for a content type. In this example, I use the document type as a parent 0x010100 and add a GUID to the parent ID. Remember to strip out all special characters. Finally, add a FieldRefs tag to hold meta-data.
Watch a demo of this procedure.
That is all there is to creating a content type in Visual Studio using the SharePoint feature mechanism. One last note: While it is often tempting to place more than one content type tag in the contenttype.xml file, this is probably not in your best interests in the long run. By including more than one content type in your feature, you are creating a dependency between them. If you need to modify a type or remove it from your site, you will have to touch all of the other types in the feature. For this reason, I keep each content type in its own feature just like I keep classes in their own class files. That said, there are times when I put more than one class in a file – there are exceptions to every rule.