in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Ton Stegeman [MVP] weblog

I have moved my blog to http://www.tonstegeman.com/blog. If you have a blogpost at sharepointblogs that does not display all the content on the right hand side, please go to the new blog. It has all the posts that are on this blog as well. I you have any feedback, please send me a message through the contact form.

Creating ContentTypes in SharePoint 2007 by using a feature

In one of the previous posts I showed how you can add sitecolumns to a SharePoint 2007 site by using features. It is also possible to deploy content types using features. In this post I will describe how to do that. We will create 3 contenttypes; the first is called “Company Document” and has 2 fields: Region and Industry. The contenttypes “Proposal” and “Whitepaper” inherit from this “Company Document”. Proposal has 1 extra column called ClientName and Whitepaper has and extra column to store the publication date.

One warning: SharePoint 2007 isn’t very informative in case you do something wrong in your sitecolumns or contenttypes. It simply does not appear in the list, leaving you wondering what is wrong. Be warned!

Step 1 – Create the SiteColumns
The first step is to create the site columns. Fields cannot be added directly to contenttypes, they have to be a sitecolumn. I will not describe how to deploy these columns (you can find that here). The feature xml looks like this:

    1 <?xml version="1.0" encoding="utf-8" ?>
    2 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    3   <Field ID="{7d2aeebd-ad31-43f2-9f9f-72d76759dd36}" 
    4       Name="Region" DisplayName="Region" StaticName="Region" Group="Company Metadata Model"
    5       Type="Choice" Sealed="TRUE" AllowDeletion="FALSE"
    6       SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
    7       Description="Identifies the region this document applies to">
    8     <CHOICES>
    9       <CHOICE>EMA</CHOICE><CHOICE>ASPAC</CHOICE><CHOICE>Americas</CHOICE>
   10     </CHOICES>
   11     <Default>EMA</Default>
   12   </Field>
   13   <Field ID="{05d891f3-5f35-41aa-bce0-40e37d08c2e3}" 
   14     Name="Industry" DisplayName="Industry" StaticName="Industry" Group="Company Metadata Model"
   15     Type="Choice" Sealed="TRUE" AllowDeletion="FALSE" FillInChoice="FALSE"
   16     SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
   17     Description="Identifies the industry that the document applies to" >
   18     <CHOICES>
   19       <CHOICE>Auto Dealerships</CHOICE><CHOICE>Auto Manufacturers</CHOICE>
   20     </CHOICES>
   21   </Field>
   22   <Field ID="{a898bf6c-bb0b-4451-b0f3-b6aa27223865}"
   23       Name="CustomerName" DisplayName="Customer Name" StaticName="CustomerName" Group="Company Metadata Model"
   24       Type="Text" Sealed="TRUE" AllowDeletion="FALSE"
   25       SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
   26       Description="Identifies the client for whom the document was prepared" />
   27   <Field ID="{b3c3f26e-a0c6-4607-8f45-5465c8fd67cb}"
   28       Name="PublicationDate" DisplayName="Publication Date" StaticName="PublicationDate" Group="Company Metadata Model"
   29       Type="Text" Sealed="TRUE" AllowDeletion="FALSE"
   30       SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
   31       Description="The official publication date for the document" />
   32 </Elements>

You will need to give you sitecolumns a unique id (a GUID). These ID’s are used to reference the fields from the contenttype XML.
I am still trying to figure out why we now have a Name and a StaticName for a field. If you know this, please let me know!

Step 2 – Create a ContentType feature

  • Create a new subfolder “companycontenttypes” in folder “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES”.
  • Add a new file to this folder called “feature.xml”
  • The contents of this file look like this:
        1 <?xml version="1.0" encoding="utf-8" ?>
        2 <!-- _lcid="1033" _version="12.0.4017" _dal="1" -->
        3 <!-- _LocalBinding -->
        4 <Feature  Id="c36321e0-0107-43fa-85bf-5729f23963fc"
        5           Title="Company Content Types"
        6           Description="Company Content Types"
        7           Version="1.0.0.0"
        8           Scope="Site"
        9           xmlns="http://schemas.microsoft.com/sharepoint/">
       10   <ElementManifests>
       11     <ElementManifest Location="companycontenttypeswss.xml" />
       12   </ElementManifests>
       13 </Feature>

    Make sure you create a new unique ID for the feature. Please note the feature is scoped at the site level.
  • The element manifest is the name of the file in which we will create our contenttypes.

 Step 3 – Create the contenttypes

  • Create a new file in the same folder, with the value of the Location attribute of the ElementManifest element in the feature xml of ste 2.
  • In my case this xml looks like this:
        1 <?xml version="1.0" encoding="utf-8" ?>
        2 <!-- _lcid="1033" _version="12.0.4017" _dal="1" -->
        3 <!-- _LocalBinding -->
        4 <!--
        5 -->
        6 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
        7   <ContentType ID="0x01010D"
        8       Name="Company Document"
        9       Group="Company Metadata Model"
       10       Description="Company document"
       11       Version="0">
       12     <FieldRefs>
       13       <FieldRef ID="{7d2aeebd-ad31-43f2-9f9f-72d76759dd36}" Name="Region" Required="TRUE"/>
       14       <FieldRef ID="{05d891f3-5f35-41aa-bce0-40e37d08c2e3}" Name="Industry" Required="TRUE"/>
       15     </FieldRefs>
       16   </ContentType>
       17   <ContentType ID="0x01010D0D"
       18       Name="Proposal"
       19       Group="Company Metadata Model"
       20       Description="Company proposal"
       21       Version="0">
       22     <FieldRefs>
       23       <FieldRef ID="{a898bf6c-bb0b-4451-b0f3-b6aa27223865}" Name="CustomerName" Required="TRUE"/>
       24     </FieldRefs>
       25   </ContentType>
       26   <ContentType ID="0x01010D0E"
       27       Name="Whitepaper"
       28       Group="Company Metadata Model"
       29       Description="Company whitepaper"
       30       Version="0">
       31     <FieldRefs>
       32       <FieldRef ID="{b3c3f26e-a0c6-4607-8f45-5465c8fd67cb}" Name="PublicationDate" Required="TRUE"/>
       33     </FieldRefs>
       34   </ContentType>
       35 </Elements>
    In this xml the 3 contenttypes are defined. One thing to be aware of is the ID of a contenttype. An ID has to be unique, and by using the correct syntax, you can set the inheritance of a contenttype. The ID of our base contenttype is “0x01010D”. This means that our contenttype inherits from “0x0101”, which is the out of the box SharePoint Document contenttype. Our Proposal inherits from our Company Document and therefore the ID starts with “0x01010D” followed by a unique identifier this Proposal.
    By setting the “Required” attribute, the fields will be required for a user to fill in when the contenttype is used in a list.

Step 3 – Register and Activate the feature

  • Register the feature by using this command:
        stsadm -o installfeature -filename companycontenttypes\feature.xml
  • Activate the feature by using this command:
        stsadm" -o activatefeature -filename companycontenttypes\feature.xml -url http://companyintranet
    Please note that the contenttypes will be avaible in the site that is set with the -url parameter. They are available in the site and all it’s subsites.

Step 4 – Test the contenttypes

  • Create a new document library in your site
  • Switch “Allow management of content types?” on in the advanced settings
  • Add the Proposal and Whitepaper contenttypes to the library
  • The “Columns” section of your document library should now look like this:
    Contenttypes

Comments

No Comments

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts