Content types are a
fundamental concept of SharePoint foundation. Any data that you put
inside SharePoint gets associated with some content type. Any content
type gives you a number of reusable settings. The reusable settings
consist of the structure of information, associated workflows,
information management policies, field rendering templates, and in the
case of documents—document information panels and document formats and
templates.
As an example in your
organization, you may produce proposals for clients or software defect
bug reports. It is possible that both of these are Word documents, but
their meanings are quite different. The workflows that they need to be
routed through are quite different and the associated collected metadata
on each of these can also be quite different. Therefore, it makes sense
to differentiate between them as their own content types.
In fact, any content that you
put inside of SharePoint automatically gets associated with some content
type. At the very base, you have the "item content type," with just one
possible column in it called Title. For every document that you upload
into SharePoint, it gets associated with that document type content
type. So any further content types you create you are basically
inheriting from these base content types, or another content type that
in turn inherits from these base content types. The document content
type also inherits from the item content type, so the granddaddy of all
content types is the item content type.
Let's examine this as
a practical example. Let's say you're setting up the SharePoint site to
collect information about zoo animals. You could just create a custom
list and add the necessary columns in it to capture all the metadata
necessary for a zoo animal. That would allow you to capture data as the
default "item" content type, thus making your zoo animal of about the
same category as school supplies. A better way of storing zoo animals
would be to give zoo animals their own content type.
To create a content type for zoo animals, visit your SharePoint site at http://sp2010
and click site actions\site settings. Then, under the gallery section
click the site content types link and you will see all the existing
content types that are currently set up in your SharePoint site. As you
can see, even out of the box Microsoft has made plenty of use of content
types. Next, click the create button, as shown in Figure 1 to create a new content type.
Fill in the provided form, as shown in Figure 2.
As you can see, you've created a
new zoo animal content type and are inheriting that content type from
the base item content type. Also, you've put this content type in its
own group called zoo. Click the OK button to create the new content
type. SharePoint will now take you to a page that shows you the details
of the newly created content type. The URL of this page will look a
little bit like this: /_layouts/ManageContentType.aspx?ctype=0x010070A9BD7BBA699D48B92FE21F1632CC28.
This ctype querystring
parameter is the content type ID of your newly created content type.
Your ID might be different, but it would start with "0×01". This is
because "0×01", is the content type ID for Zoo Animal's parent content
type which is the item content type. Also, on the same page you will see
a number of other things. You will see a settings section where you can
specify additional settings available on the content type such as
associated workflows. Under the columns section is where you can set up
the structure of your content type, i.e., what columns are available in
the content type. When setting up the structure for your content type,
you can either choose to add from an existing site column or you can
choose to create a new site column. Site columns are what define
individual columns in a content type.
Open a new browser window and visit the site settings for your http://sp2010
site one more time. Under the Galleries section, you will see a link
for site columns. Clicking on that link will show you all the site
columns that have already been set up for you in the SharePoint site.
One site column can be used across multiple content types. Perhaps you
found out in an earlier zoo that if you mix Carnivores and Herbivores
together, somehow you are only left with Carnivores? For the zoo animal
content type, intend to differentiate between carnivores and herbivores.
In the site columns gallery, click the create button to create a new
site column. Go ahead and fill out the form, as shown in Figure 3.
As you can see, you're creating a
new site column called "Is Meat Eater". This will be a yes/no check
box, with the default value of yes. Also, put this site column in its
own group called "Zoo".
Note that you can base your
new site column on a number of preexisting definitions such as single
line of text, multiple lines of text, choice, and so forth. These
preexisting definitions are referred to as field types. Go ahead
and play with the site columns a little bit and explore some of the
additional field types available. I should point out that all your work
so far is limited to the individual site collection you're working in,
so if you completely mess up your site collection just delete the site
collection and recreate it, and you'll start from zero again.
Once you have created that
"is meat eater" site column, return to the browser instance where you
are managing your "Zoo Animal" content type. If you have accidentally
closed the previous browser window, you can find that content type again
by visiting the site content types gallery under site settings and
clicking on the zoo animal content type under the groups zoo at the
bottom of the page.
Back in the zoo animal
content type, under the Columns section, click that add from existing
site columns link. In the form that shows up, look for the new "Is Meat
Eater" site column you had set up previously and add it to your zoo
animal content type.
Your content type is now set
up. Next let's use it in a list. Create a new custom list called
"SharePoint Zoo". The list definition for the custom list determines
that there is only one content type associated with the newly created
list instance. That single content type is the item content type. Using
the ribbon under the list tab, visit the list settings link. On the list
settings page, you will see various settings you can manage for the
list. Click the advanced settings link under the general settings
section. At the very top under advanced settings, you should see a
section allowing or disallowing the management of content types in the
list. This can be seen in Figure 4.
By default, the custom
list definition locks its list instances to the content types that are
defined in the list definition. Since you need to associate the zoo
animal content type with the SharePoint zoo list, choose to allow
management content type by selecting the yes radio button, and then hit
OK at the bottom of the page.
This will take you back to
the list settings page except now a section for the management of
content types will become available. This can be seen in Figure 5.
Click the add from
existing site content types link and add the zoo animal content type to
this list. If you wish, you can associate more content types in the
list, delete the existing item content type, or you can change the
default content type as well. While still under lists settings, click
the item content type link and choose to delete the item content type
from the SharePoint zoo list.
NOTE
Did you just delete the
item content type ? Yes, you did. What happens to all the other content
types that inherit from the item content type? What is important to
realize here is that when you associate a content type with a list, a
copy of that content type is created in the list. Therefore, if you
delete a particular content type from the list settings page, you're
only deleting the copy. You're not deleting it from the Content Types
gallery from the site settings page. This also means that you can edit a
content type by editing the copy of it that has been created in a list.
Editing the copy will not affect the master content type in the content
type gallery, and thus will not affect everything that inherits from
the master. This is a technique that you will find yourself using
frequently.
Now let's go back to the
SharePoint zoo list and click the add new item link in the SharePoint
zoo list. You should see a form, as shown in Figure 6.
As you can see, the definition
of the content type now drives the forms of the list. Creating new
items in this list will now also automatically associate them with the
zoo animal content type. Now you could argue that you could have just
created the "is meat eater" column in the list directly. What is the
benefit of taking the content type route? There are many benefits.
Because your data is now segregated from normal list items, it is more
findable, you can execute custom search queries against the content type
only, you can associate workflows, and many other such advantages.
Let's see one of those in action.
Next, add a few animals in this
list. Now let's say you had a business problem of aggregating all
instances of the zoo animals from various lists in a singular view. How
would you do that?
Because you segregated your
data as its own content type it is now possible to do that. In order to
do so, go into site actions\site settings and under site collection
administration, click the site collections feature link. Then activate
the SharePoint Server Publishing Infrastructure feature. This will make
the content query WebPart available for use. Back on the homepage of the
site, drop an instance of the content query WebPart. You will find this
WebPart in the content rollup section. Now edit the content query
WebPart, and configure it to show all items of the zoo animal content
type from all lists that are based on the custom list definition. This
can be seen in Figure 7.
Your final configured WebPart can be seen in Figure 8.
The content query WebPart is an invaluable tool in your arsenal. Play with it a little bit more and see what else you can do.
You just
created a new content type through the browser. You might have also
noticed that, SharePoint generated a content type ID for you. What if
you wanted to control this content type ID? This is important especially
considering that you may have multiple environments in which you may
want the content type ID to be consistent. Or you may want to content
type ID to be consistent across site collections? What if you wanted
your content type ID to use a site column that uses a custom field type ?
There are many other such scenarios that a developer will find useful
in delivering a typical SharePoint project. Let's start by looking at
how you would write your own custom field types.