Content types
Content type is a reusable collection of settings that you want to apply to a certain category of content.
Content types enable you to manage the metadata and behaviour of a document, item, or folder in a centralised, reusable way.
If we create one Content type in root
website we can use it in sub sites.
Content Types in SharePoint
Let's
start discussing content types in SharePoint 2010. Content Type Hub is a new concept
in SharePoint 2010. But before going into more details, let's first
discuss briefly what the content types are.
So our agenda for the discussion is:
So our agenda for the discussion is:
- What
are the Content Types
- Problem
with Content Types in MOSS 2007
- Content
Type Hub in SharePoint 2010
- How
to publish content types on Content Type hub
- How
to expose published content types for the web applications (How to
subscribe the published content types)
What are the Content Types?
- Content
Types are very important and powerful features in SharePoint.
- A
Content type is a group of site columns (or metadata), workflows and other
settings for the item in the list or document in document library or for
SharePoint page.
- Content
types are reusable component, they are not specific to any list or library
or page layout.
- Content
Type represents group of information with common properties. For example,
you are maintaining the list of the employees for your company, but there
are different details for the employees who are onsite. So here you can
create two different content types; one for onsite employees and another
for local employees. You can associate these content types with the
Employee list. So whenever you enter the details for an onsite employee
you will create the list item from onsite content type. Also by going one
more step ahead we can create base content type with common fields (site
columns) and derive two content types from base content type.
Some More Details:
- We
can create content types in SharePoint by the following 3 ways:
a. From UI
b. Using CAML (Collaborative Application Markup Language)
c. Using SharePoint Object Model (i.e. Programmatically)
- We
can associate one content types to multiple lists and libraries
- SharePoint
comes with several OOB content types.
- Content
types are always inherited; content types are never created from scratch.
So an existing content type is always selected as a base content type for
creating a new content type. So we automatically get all the columns from
the base content type for the new one.
- Content Types are stored in the SharePoint database. Databases are created for the web application. We can view all the content types from content type gallery (Site Action >> Site Settings >>Galleries>>Site Content Types). Content Type gallery is the document library.
Figure. Content Type Gallery option
Problem with Content Types in MOSS
- Now
suppose I have created a few content types for my site collection in
WebApplication1. And now I want to use some of the content types created
in webApplication1 for WebApplication2. But I don't have such an option to
share the content types in multiple web applications. I need to copy and
paste all the content types with code and separately deployment on the
WebApplication2.
- Also then there is no synchronisation mechanism between the content types. If I do some modifications in content types for web application1 then I need to do these changes also content types available for web application2.
Content
Type Hub in SharePoint 2010
- To
overcome the above problem SharePoint 2010 introduces a new concept called
Content Type Hubs.
- Content
Type Hub is the centralized place where all content types are published
and the web applications which will require the content types will
subscribe to the content type hub and can get the published content types
from the hub.
- Web
application which subscribes the published content types also can receive
the updates on published content types.
- So
Content Type hub is nothing but the separate common site collection where
all content types will get created.
- Now
to act as this site collection as a content type hub we need to activate
the "Content Type Syndication Hub" site level feature.
Figure. Feature activation required for Content Type hub
- Once
we have activated this feature we will get a link to "Manage
publishing for this content type" for every content type setting page
as:
Figure. Link for Manage Publishing for this content type
Exposing the Content Types published on Content Type Hub to the web applications
- Now
to expose our content type created on the hub we need to configure Managed
Metadata Service.
- Enterprise
Metadata Management Service (Metadata Service) allows us to share the
content types across the farm.
- Next,
we need to bind the content type hub with the meta data service, for this
go to
Central Administration >> Application Management>>Manage Service Application.
Figure. Manage service applications option
- If
metadata service is already created then we will get two entries, one is
the service and one is connection
Figure. Manage Metadata Service Application and Connection Links
Select the metadata service, click on the Meta data service and click properties button.
On the properties window, put the URL of content type hub site collection.
Figure. Content Type Hub URL option
Also, click on metadata connection, click on properties button and make sure that "Consumes content types from the Content Type Gallery at <URL of your content type hub site collection>" check box is checked.
Figure. Managed metadata connection
Publishing the Content Type
- Now
we can publish any content type from the content type hub site collection,
to be used in the other web applications for which this Meta data
connection is associated.
- Go
to the content type setting page, click on the link "Manage
Publishing for this content type", and make sure that Publish option
button is selected.
Figure. Content Type Publishing options
Now
these publishing of content types are managed by the timer job which is
executed after every 15 minutes and then available to the subscriber web
application.
a. Content Type Hub (Server Job)
b. Content Type Subscriber (Job for the web application which is subscribing the content types)
a. Content Type Hub (Server Job)
b. Content Type Subscriber (Job for the web application which is subscribing the content types)
Associate a Content Type with a SharePoint List or Library
Content
types are an extremely powerful way to associate metadata and actions
with your SharePoint 2010 documents and list items. The role of the
content type is to separate the metadata, workflows, document template,
and other settings from individual lists and libraries.
However, content types by themselves aren’t useful without lists and libraries. So you have to associate them with the lists and libraries where you want to use them to describe content.
To associate a content type with a list or library:
Remove any content types from your list that you aren’t using. To do so:
However, content types by themselves aren’t useful without lists and libraries. So you have to associate them with the lists and libraries where you want to use them to describe content.
To associate a content type with a list or library:
- Browse to the List Settings or Library Settings page for the list or library where you want to use the content type by selecting the List tab or Library tab and the List Settings or Library Settings button.
- Click the Advanced Settings link.
- In the Allow Management of Content Types section, select the Yes radio button and then click OK.The Content Types section appears in the Settings page. All lists and libraries have at least one default content type associated with them, so this content type appears.
- To add another content type to the list or library, click the Add from Existing Site Content Types link.
- On the Select Content Types page, in the Available Site Content Types section, select the content types you want to add to the list or library and click the Add button to move them to the Content Types to Add list.
- Click OK.The new content type appears in the Settings page.
Remove any content types from your list that you aren’t using. To do so:
- In the List Settings or Library Settings page, click the content type you want to remove.See the preceding steps for how to access the Settings page.
- Click the Delete This Content Type link and then click OK at the confirmation prompt.The content type is removed from the list or library. Note: You're removing only this content type from the list or library; it still exists as a site content type.
Creating a Custom Content Type using Visual Studio
Introduction
In this article we will explore the ways of creating a custom content type.
What is a Content Type?
A Content Type is a reusable, shareable metadata. The base type of a content type could be an existing content type like:
- Item
- Document
- Folder
- Event
- Task
- Message
- Comment
- Post
Some more information on Content Type:
Base Type: The base type of the Content Type is System with Id 0x.
Example: If you need a common list format or document format across sites then Content Type is advisable.
Groups: The content types are organized in groups, for example, List Content Types, Document Content Types, and Folder Content Types.
Three important Content Types: Item, Document, and Folder.
Feature: A Content Type can be deployed as a feature.
Scope: The scope of a Content Type could be specified in the feature as Site, Web etc.
Creating Custom Content Types
We can use the following tools for creating Content Types:
- SharePoint UI
- Visual Studio 2010
- SharePoint Designer 2010
The content type is in XML format so Notepad can also do the job but in a different way.
Creating a Content Type using Visual Studio 2010
Now we can try creating a content type using Visual Studio 2010. The SharePoint Developer Tools already contain the template for Content Type creation. Here we are trying to create an Item Content Type with the following fields:
- Title
- Contact Type
Open Visual Studio and create a new project from SharePoint > Content Type.
Enter the name of your content type and click the OK button.
In the next page, you will be prompted with the solution type:
Select the option as Sandboxed solution and click the Next button.
In the next page, select the base type. You can see a large list of base types possible.
Select the base type as Item and click the Finish button. Expand Content Type1 and enter the following code inElements.xml:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field ID="{B7410A27-4E0C-42FB-BC6E-07928C43108D}" Name="ContactType" DisplayName="Contact Type" Type ="Choice" >
<CHOICES>
<CHOICE>Personal Contact</CHOICE>
<CHOICE>Official Contact</CHOICE>
<CHOICE>Other Contact</CHOICE>
</CHOICES>
</Field>
<!-- Parent ContentType: Item (0x01) -->
<ContentType ID="0x01009aa1f989a2604c99af0f57d80fe0b16d"
Name="Contact Type"
Group="Custom Content Types"
Description="Contact Type"
Inherits="TRUE"
Version="0">
<FieldRefs>
<FieldRef ID="{B7410A27-4E0C-42FB-BC6E-07928C43108D}"/>
</FieldRefs>
</ContentType>
</Elements>
The above code performs the following:
- Create a new Choice field named ContactType
- Add three Choices for the field
- The new field is referred inside the Content Type
Deploying the Content Type
The content type is deployed as a feature. Right click on the project and use the menu command Deploy.
Possible Error
If you get the following error:
Error occurred in deployment step ‘Activate Features’: Cannot start service SPUserCodeV4 on computer
It means that the Sandboxed Solution Service is not running in your computer.
Solution: Go to Central Administration and open the following link:
Click the Start link of the following service:
Wait for a while until the service is started and now you can try deployment. After the deployment succeeds, go to the SharePoint site.
Testing the Content Type
Now we can try testing the new Content Type. Our Content Type will prompt with following properties:
- Last Name
- Contact Type
Create a new list from template Contacts and go to the List Settings > Advanced Settings.
Modify the Allow Management property to true and save the changes.
Back to the List Settings, now you can see the following option:
Click on the Add from existing site content types, and in the appearing dialog, choose the new deployed Contact Type, and click the OK button.
Now go back to the List Settings and choose the Change new button.. link
In the above page, choose our Contact Type as the default option and save the changes.
Now go back to the My Contacts list and try to enter a new contact.
You will get the new modified properties dialog. Here our Contact Type is being used.
Note: The Last Name column is the default one with original name as Title.
So this concludes our Content Type creation using Visual Studio. In real world scenarios, packing the Content Type into one Solution would be advantageous for deployment purposes.