Understanding Content Types

While you have worked with site columns in isolation up to this point, you always find yourself using site columns through content types. A content type is a reusable collection of metadata (columns), workflow, behavior, and many other settings. Content types help to categorize data. Say that you have a list that stores vehicle information. You may be storing information on different types of vehicles like cars, trains, ships, and airplanes. Each of these has some attributes specific to them. You can create separate content types for them. For example, you can have a LandVehicle content type that has attributes such as title, speed, and number of tires. You can have another content type called FlyingVehicles that has attributes such as title, speed, maximum height it can reach, and number of wings. And you can have another content type called SeaVehicles that has its own set of attributes.

A content type can include the following information:

• The metadata of the content type. These are represented by site columns of the content type.

• Custom New, Edit, and Display forms to use with this content type.

• Workflows associated with the content type.

• For document content types, the document template on which to base documents of this type.

• Any other custom information that you want to associate with the content type. You can store this information in the content type as one or more XML documents.

You can see the content types available within the site by going to the Site Content Types gallery. To view the Site Content Types gallery, go to Site Actions, Site Settings. Under the Galleries section click Site Content Types. Here you can see the various out of the box and custom content types.

You will now create a new content type named Animal. The content type has title, description and category fields. To create a new content type follow the next steps.


Did You Know?

The out of the box Item content type can be considered as the base content type of all other content types in SharePoint. The Item content type itself inherits from the out of the Box System content type. However, SharePoint does not allow any other content type to inherit from the System content type, and the Item content type is the topmost content type in the hierarchy from which your custom content type can inherit.


The new content type is created and you are taken to the screen shown in Figure 10.6.

Image

Figure 10.6. Site content type information screen

The screen shows many configuration options, most of which are similar to the ones you saw on the list settings page. You can see that the new content type contains the Title site column. This is because you selected the Item content type as the Parent content type when creating the Animal content type.

You can add existing site columns to this content type or create new site columns. To add existing or new site columns to a content type, follow the next steps.

To use the new content type you need to add the content type to a list. To add the content type to the list, follow the next steps.

Creating Content Types Through Features

You already saw in the previous section how to create site columns through features. You now create a content type through features. You create a new content type Product based on the item content type. Create the Product Description and Product Category site columns through your feature and add it to the new content type. In addition you use the Title field from the Item content type to store the name of the Product, you also associate the new content type to your SimpleList list.

Open the CustomSchema project created in the previous section. Add a new item of type Content Type and name it Product. Click Add, and you see the screen that appears in Figure 10.9.

Image

Figure 10.9. Selecting a base content type

You can see various content types present in the site collection, including the Animal content type. Select the Item content type. A new elements.xml file is added under the Products folder as shown in Figure 10.10.

Image

Figure 10.10. CustomSchema project structure

Open the elements.xml file, and you can see the following:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x0100acfda71432f84130a72c55b28097ca2b"
               Name="CustomSchema - Product"
               Group="Custom Content Types"
               Description="My Content Type"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
    </FieldRefs>
  </ContentType>
</Elements>

The ContentType element defines a new content type. The ID attribute uniquely identifies the content type across the site collection. This is represented by the SPContentTypeId class in the object model. Content Type IDs are designed to be recursive. The content type ID encapsulates 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, until System Content Type ID. Hence our new content type starts from 0x01, which is the ID of the Item content type, and the content type ID of the Item content type starts with 0x, which is the content type ID for the System content type. The Inherits attribute allows you to specify whether the content type must inherit the fields of the parent content type.

Modify the elements.xml file as shown in the following code:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <Field Type="Note"
DisplayName="Product Description"
Required="FALSE"
EnforceUniqueValues="FALSE"
NumLines="8"
RichText="TRUE"
RichTextMode="FullHtml"
Group="Custom Columns"
ID="{CA07C01D-B547-4766-A5AD-77D6CA01ABE1}"
StaticName="ProductDescription"
Name="ProductDescription" />

  <Field
Type="Choice"
DisplayName="Product Category"
Required="FALSE"
EnforceUniqueValues="FALSE"
Indexed="FALSE"
Format="Dropdown"
FillInChoice="FALSE"
Group="Custom Columns"
ID="{8753041F-629F-48E7-B5C4-B2488A22F5BF}"
StaticName="ProductCategory"
Name="ProductCategory" >
    <Default>Electronics</Default>
    <CHOICES>
      <CHOICE>Electronics</CHOICE>
      <CHOICE>Food and Beverages</CHOICE>
      <CHOICE>Others</CHOICE>
    </CHOICES>
  </Field>

  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x0100acfda71432f84130a72c55b28097ca2b"
               Name="Product"
               Group="Custom Content Types"
               Description="Represents information about a product"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{CA07C01D-B547-4766-A5AD-77D6CA01ABE1}"
 Name="ProductDescription" />
      <FieldRef ID="{8753041F-629F-48E7-B5C4-B2488A22F5BF}"
Name="ProductCategory" />
    </FieldRefs>
  </ContentType>
  <ContentTypeBinding
    ContentTypeId="0x0100acfda71432f84130a72c55b28097ca2b"
    ListUrl="Lists/SimpleList"/>
</Elements>

Deploy the solution. Browse to the SimpleList, and you should be able to see the new content type in the New Item menu, as shown in Figure 10.11.

Image

Figure 10.11. The Product content type in the New Item menu

Now take a look back at the XML we just wrote. You can see two new site columns, ProductDescription and ProductCategory, defined. The site columns are associated with the content type using the FieldRef element. The ID attribute matches the ID of your site column, and the Name attribute matches the StaticName of the site column. Finally the content type is associated to the lists SimpleList using the ContentTypeBinding element. The ContentTypeId attribute represents the ID of the content type to be associated with the list at the URL given by the ListUrl attribute.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset