Monday, May 9, 2011

How to Create a Content Type using Visual Studio 2010 in SharePoint 2010

In this post we will discuss how to create a Content Type using Visual studio 2010 in SharePoint 2010. Also you can check out:

- How to create event receivers using Visual Studio 2010 in SharePoint 2010?

- Retrieve all web site title and list title in current site collection in SharePoint

- Create user profile synchronization connection in SharePoint 2013

Follow the below Steps:
Open Visual Studio 2010 -> File -> New Project ->Select Content Type project template type -> Then give a name and Location and Solution name. See fig below

Then in the SharePoint Customization Wizard give the local site URL for debugging and then select deploy as farm solution and click on next. See fig below.

Then in this step select a parent content type from which you want to inherit the current content type. Here I have selected the Document content type. Refer figure below.

After step-3, when you open the solution explorer you will see the Elements.xml file where you can change the name, description of the parent content type as seen below in the figure.

Then you can use Field tag to add a field to the content type. For the field ID you can go to Tools->Create GUID and copy the GUID and paste the field’s ID property. Then you can field type to choice as below.

If you are using the file then you need not worry about the intelligence support for the above xml file.

Then refer the above field in the Field Ref attribute in the xml as follows.

The whole XML file will look like the figure below.

Then Deploy the solution and now we are ready with the content type. To use this we have to do some administration as described below.

Go to Share Documents document library then in the ribbon click on Library -> Library Settings->Advance Settings and select Allow management of content types? To Yes (By default No is selected).

Then scroll down to the Content Types section in the Document Library Settings Page. Then click on Add from existing content types as show in the picture below.

Then in the Add Content Types page Select Custom Content Type in the Select site content types from: dropdown list. Select the Content type and click on Add button and click on OK button as show in the figure.

Then we can use the content type in the Shared Documents document library.

Then go to the Share Document document library then click on Add new item then upload a document and then in the properties page it will show the properties like the figure below.

Hope you will enjoy.