All about Microsoft Office SharePoint Server Content Type – Part 3

In the part 1 of the article we covered

  • What is a content type
  • How content types work
  • In the part 2 of the article we covered

  • Extending content types
  • Attaching workflows to a content type
  • Attaching event handlers to a content type
  • In this part 3 of the article we will cover

  • How to create a Content Type programmatically using SharePoint object model
  • Significance of Content Type ID
  • Creating Content Types using Visual Studio Extensions for WSS
  • Deployment of Content Types as Features
  •  In the part 4 (final part) of the article we will cover

    • Solution Package Editing in VS 2008 / WSP view
    • Adding content type to a list definition

    How to create a Content Type programmatically using SharePoint object model:

  • Lets try and add a content type called “NewSpec” that inherits from another custom content type called “Spec” . Make sure you add the content type to the web before you call the update method of the content type. 
    using (SPSite site = new SPSite("http://vslearnmoss/sites/sitecoll1"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPContentType contentType = new SPContentType
             (web.AvailableContentTypes["Spec"],web.ContentTypes, "NewSpec");
            web.ContentTypes.Add(contentType);
            web.Fields.Add("SpecColumn1", SPFieldType.Text, true);
            SPFieldLink fieldLink = new SPFieldLink(web.Fields["SpecColumn1"]);
            contentType.FieldLinks.Add(fieldLink);
            contentType.Update();
        }
    }

     

    Significance of Content Type ID:

    Content type IDs uniquely identify the content type. Content type IDs are designed to be recursive. The content type ID encapsulates that content type’s “lineage”, or the line of parent content types from which the content type inherits. Each content type ID contains the ID of the parent content type, which in turn contains the ID of that content type’s parent, and so on, ultimately back to and including the System content type ID. By parsing the content type ID, you can determine which content types the content type inherits, and how two content types are related.

    Windows SharePoint Services V3 uses this information to determine the relationship between content types, and for push down operations.

    You can construct a valid content type ID using one of two conventions:

    · Parent content type ID + two hexadecimal values

    · Parent content type ID + “00” + hexadecimal GUID

    There is one special case, that of the System content type, which has the content type ID of “0x”. The System content type is the sealed content type from which all other content types ultimately inherit.

    For all other content types, you must use one of the above methods for constructing a valid content type ID.

    Note that if you use the first method, the two hexadecimal values cannot be “00”.

    A content type ID must be unique within a site collection.

    Content type IDs have a maximum length of 512 bytes. Because two hexadecimal characters can fit in each byte, this gives each content type ID an effective maximum length of 1024 characters.

    …more info on Andrew May’s web log

    SPContentType.ID property is a read only property. When you create Content Types programmatically the ID creation is taken care of. If you are creating your content type using features then you will have to define the content types.

     

    Creating Content Types using Visual Studio Extensions for WSS:

    Dev Environment – VS 2008 Professional Edition with VSeWSS 1.2

    • Open Visual Studio, choose File->New->Project
    • Select SharePoint project templates under Visual C#, choose ‘Empty’ project template.

    image

    • Select SharePoint project templates under Visual C#, choose ‘Empty’ project template.
    • Type in your name for the Project and Select the Project Location and click OK
    • After the project structure is created. Right click the project and choose Add-> New Item and select Content Type under SharePoint category.

    image

    • When you add this new item, you will be prompted to choose the Base Content Type. Choose ‘Item’. You can choose other base content types depending upon the need.

    image

    • It will generate an XML file with a dummy field references as well. The file generated follows the Content Type definition schema and Field definition schema.
    • The ContentType ID generated is “0x010044aff40e174c4f7d89861ff89cee1d15”. Here 0x01 denotes ‘Item’ base content type and it is followed by 2 zeros and then the GUID of the new content type 0x010044aff40e174c4f7d89861ff89cee1d15.
    • Let us assume that we want to create 2 site columns. One field of type User and another of type Choice column. Edit the ‘MyBaseContentType.xml’ and include the following.
    • <?xml version="1.0" encoding="utf-8"?>
      <Elements Id="5d7dfb50-7693-4f6f-aa61-0a3cfedca39a"
           xmlns="http://schemas.microsoft.com/sharepoint/">
       
        <Field ID="{5623b931-2e9e-4dc6-81b6-c8a151f37418}"
              Name="Team Member"
              Type="User"
              List='UserInfo'
              UserSelectionMode="1"
                  UserSelectionScope="1"
              DisplayName="Team Member"
              Group="Custom Columns"
              SourceID="http://schemas.microsoft.com/sharepoint/v3"
              ShowAddressBookButton="TRUE">
        </Field>
        <Field ID="{9b39033a-4f1c-4a0f-9deb-73fecdbc3b5d}"
            Name="Project Type"
            Type="Choice"
            DisplayName="Organization"
            Group="Custom Columns"
            Format="Dropdown">
          <CHOICES>
            <CHOICE>Internal</CHOICE>
            <CHOICE>External</CHOICE>
          </CHOICES>
        </Field>
        <ContentType ID="0x010044aff40e174c4f7d89861ff89cee1d15"
                     Name="MyBaseContentType"
                     Group="Development"
                     Description="Developing Content Type"
                     Version="0">
          <FieldRefs>
            <FieldRef ID ="{5623b931-2e9e-4dc6-81b6-c8a151f37418}"
               Name ="Team Member"
               Required="TRUE">
            </FieldRef>
            <FieldRef ID ="{9b39033a-4f1c-4a0f-9deb-73fecdbc3b5d}"
               Name ="Project Type"
               Required="TRUE">
            </FieldRef>
          </FieldRefs>
        </ContentType>
      </Elements>
    • Build the solution. Right Click the Project in the Visual Studio Solution Explorer and Choose ‘Deploy’ option. This Content Type will be packaged as a feature and deployed globally and the feature will be activated.
    • Go To Site Collection->Site Actions->Site Settings->Site Content Types you will see ‘MyBaseContentType’ under the Development as we chose the group name as ‘Development’

    image

    • Go To Site Settings->Site Collection Features you will see the MyBaseContentType feature activated.

    image

    We have now created a new content type and deployed it from the familiar Visual Studio environment. If you want to derive this content type and create a new content type. Follow the below mentioned steps.

    • Create a new Visual Studio project similar to the one described above. Name it as MyDerivedContentType. Choose ‘Item’ as the base content type.
    • Edit the MyDerivedContentType.xml and include field definition for an additional field for representing project status. Refer to the code below.
    <?xml version="1.0" encoding="utf-8"?>
    <Elements Id="84e70bc9-5444-4634-a256-96e0caf8dce6"
           xmlns="http://schemas.microsoft.com/sharepoint/">
      <Field ID="{71ED4BE5-B83F-47e4-866E-08D226709522}"
          Name="Project Status"
          Type="Choice"
          DisplayName="Project Status"
          Group="Custom Columns"
          Format="Dropdown">
        <CHOICES>
          <CHOICE>Not Started</CHOICE>
          <CHOICE>In Progress</CHOICE>
          <CHOICE>Completed</CHOICE>
        </CHOICES>
      </Field>
      <ContentType
          ID="0x010044aff40e174c4f7d89861ff89cee1d15003824d8e586f44e54ac7049578a0100e5"
                   Name="MyDerivedContentType"
                   Group="Development"
                   Description="Developing Content Type"
                   Version="0">
        <FieldRefs>
          <FieldRef ID ="{71ED4BE5-B83F-47e4-866E-08D226709522}"
             Name ="Project Status"
             Required="TRUE">
          </FieldRef>
        </FieldRefs>
      </ContentType>
    </Elements>
    • Update the ContentType ID in the following fashion
      • Item + 00 + BaseContentType + 00 + NewGUID
      • image
    • Add the fields as required. In this example I have added ‘Project Status’ .
    • Build and Deploy the project as explained above.
    • Go To Site Actions->Site Settings->Site Collection Features and Activate the My Derived Content Type Feature.
    • Go to the Site Settings->Site Content Types gallery. You will see the new content type added. image

    Deployment of Content Types as Features:

    Let us go through the steps for creating a WSP file using the MakeCab utility. We will make this as part of the build events and generate the WSP from within Visual Studio environment.

     

    Follow the steps mentioned in Creating Content Types using Visual Studio Extensions for WSS section above for creating a content type project

    I have re used the MyDerivedContentType project.

    • Create a new text file with a name Package.ddf. This will contain the definition for the MakeCab utility. This will include the path for the manifest file, feature.xml and the elements.xml (In our project the element file is named as MyDerivedType.xml)
    ;*** MakeCab Directive file
    ;
    .OPTION Explicit ; Generate errors
    .Set CompressionType=MSZIP
    .Set UniqueFiles=Off
    .Set Cabinet=On
    .Set DiskDirectory1=Package
    .Set CabinetNameTemplate=MyDerivedContentType.cab
    ;
    ; \
    ;**************************************************
     
    manifest.xml
     
    .Set DestinationDir=MyDerivedContentType
    MyDerivedContentType\Feature.xml
    MyDerivedContentType\MyDerivedContentType.xml
    ;
    ;***End
    • Create a new XML file with the name manifest.xml and place it in the project folder. This manifest file will have reference to the Feature manifest(feature.xml) and the Resource (MyDerivedType.xml) location.
    <Solution SolutionId="C8109F9E-6551-4c5b-94CF-E187FEF85712"
          xmlns="http://schemas.microsoft.com/sharepoint/">
     
      <FeatureManifests>
        <FeatureManifest Location="MyDerivedContentType\Feature.xml"/>
      </FeatureManifests>
     
      <Resources>
        <Resource Location ="MyDerivedContentType\MyDerivedContentType.xml"/>
      </Resources>
    </Solution>
    • Create a new XML file and place it in the MyDerivedType folder. The feature.xml will have reference to the elements xml file(MyDerivedType.xml).
    <Feature
        Title="My Derived Content Type Feature"
        Id="850DEEC6-D53D-4aeb-8C60-E4C66CF9FCE4"
        Scope="Site"
        Description="Adds a content type based on the My Base Content type"
        Version="1.0.0.0"
        Hidden="FALSE"
        DefaultResourceFile="core"
        xmlns="http://schemas.microsoft.com/sharepoint/" >
     
      <ElementManifests>
        <!-- Calling Element file to load Custom Column, Webpart and Site Template-->
        <ElementManifest Location="MyDerivedContentType.xml" />
      </ElementManifests>
     
    </Feature>
    • After adding the package.ddf, manifest.xml and the feature.xml the folder structure will look like this

    image

    • Once you are done Right Click the Project and choose properties, Go to Build Events tab and add the following post build commands

    image

    cd "$(ProjectDir)"
    makecab.exe /F Package.ddf
    ren Package\MyDerivedContentType.cab MyDerivedContentType.wsp
    • This command will use the makecab utility after a successful build to create a CAB file as per the package.ddf directive file included. And will rename it to .wsp that can be used for the deployment.
    • In addition it will also create a solution folder which also includes this WSP and the setup.bat to help with the deployment. You can also use STSADM command line utility to ‘ADD’, ‘DEPLOY’ and ‘ACTIVATE’ the feature as required.

    Useful Links:

    Part – 1 , Part – 2 , Part – 3 , Part – 4

    Advertisements

    3 Responses

    1. […] All about Microsoft Office SharePoint Server Content Type – Part 3 […]

    2. […] All about Microsoft Office SharePoint Server Content Type – Part 3 […]

    3. […] All about Microsoft Office SharePoint Server Content Type – Part 3 […]

    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

    %d bloggers like this: