Click here to Skip to main content
15,881,092 members
Articles / Programming Languages / C#
Article

SharePoint 2010: Creating a Custom Content Type using Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.63/5 (5 votes)
26 Jun 2012CPOL3 min read 83.2K   410   8   8
Ways of creating a custom content type in SharePoint 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.

Image 1

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.

Image 2

Enter the name of your content type and click the OK button.

In the next page, you will be prompted with the solution type:

Image 3

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.

Image 4

Select the base type as Item and click the Finish button. Expand Content Type1 and enter the following code in Elements.xml:

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:

  1. Create a new Choice field named ContactType
  2. Add three Choices for the field
  3. The new field is referred inside the Content Type

Image 5

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:

Image 6

Click the Start link of the following service:

Image 7

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.

Image 8

Modify the Allow Management property to true and save the changes.

Back to the List Settings, now you can see the following option:

Image 9

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.

Image 10

Now go back to the List Settings and choose the Change new button.. link

Image 11

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.

Image 12

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.

Summary

In this article we have explored the creation of custom content type using Visual Studio. The source code attached contains the Content Type we discussed.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.

In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.

Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.

You can find some of his work over here. He blogs at http://jeanpaulva.com

Comments and Discussions

 
GeneralNeed to specify <fieldref name and displayname> Pin
kishore111111127-Jul-15 5:17
kishore111111127-Jul-15 5:17 
QuestionContent Type Heritance Pin
Member 115074989-Mar-15 7:23
Member 115074989-Mar-15 7:23 
QuestionNot able to see custom fields when using Tasks content type Pin
RajkumarBathula16-Feb-13 20:39
RajkumarBathula16-Feb-13 20:39 
QuestionGetting Error in re-deployment Pin
RajkumarBathula16-Oct-12 2:31
RajkumarBathula16-Oct-12 2:31 
AnswerRe: Getting Error in re-deployment Pin
Jean Paul V.A16-Oct-12 4:20
Jean Paul V.A16-Oct-12 4:20 
GeneralRe: Getting Error in re-deployment Pin
RajkumarBathula16-Oct-12 6:58
RajkumarBathula16-Oct-12 6:58 
GeneralMy vote of 4 Pin
MB Seifollahi1-Jul-12 21:18
professionalMB Seifollahi1-Jul-12 21:18 
GeneralRe: My vote of 4 Pin
Jean Paul V.A10-Jul-12 8:09
Jean Paul V.A10-Jul-12 8:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.