Create a New Site Column Def and Content Type Def in VS 2010

Create a new VS 2010 Project (empty Sharepoint Project), and add a new item (Content Type) called Track . Since every content type has parent, so VS will ask you the parent. Choose “Item” as parent content type.

Rename elements.xml to TrackContentType.xml. Now we need to add site columns to this content type. Since we will be creating a new site column first, so in your project project, add a new Empty Element item, called TrackContentFields. Rename elements.xml to TrackContentFields.xml. Rename Feature1 to TrackContentType.

Content type and site column can be used at web level or site collection level. Change the scope in the feature’s property pane. All the child webs will simply inherit all the content types and site columns from the parent.

For now we scope the feature to web (or site, which is the lowest)

Now we will create site columns in the TrackContentFields.xml file.

In TrackContentFields.xml, copy these code:

<?xml
version=1.0
encoding=utf-8?>

<Elements
xmlns=http://schemas.microsoft.com/sharepoint/>

<Field
ID={52d63c75-2b4e-41f2-9091-c92279c27f75}


Type=DateTime
DisplayName=ReleaseDate
Required=FALSE


Format=DateOnly
Group=
Music


StaticName=ReleaseDate
Name=ReleaseDate

/>

</Elements>

(the “Music” will be the site column group name for this site column)

This is a site column based on the DateTime out of box field definition.

*** Note: User Toolsà Generate GUID to generate new GUID:

Now we need to tie this new site column to the content type that we created earlier.

Open TrackContentType.xml, and under the <FieldRefs> tab, we need to reference various site columns that will be a part of this content type.

So edit it: (put it between <FieldRefs> tags.

<FieldRef ID=”{fa564e0f-0c70-4ab9-b863-0177e6ddd247}” Name=”Title”
DisplayName=”Title” Sealed=”TRUE” />
<FieldRef ID=”{52d63c75-2b4e-41f2-9091-c92279c27f75}”
Name=”ReleaseDate”/>

One is the default Title column, the other is the new site column – ReleaseDate.

So now we have define a site column, add the column to a content type. Once we deploy the solution, we know we will have a new site column and a new content type on the server.

To verify it, in Sharepoint 2010, create a new Custom List called “Album”. Go to Album’s List settings à Advanced Settings à Content Types, and allow management of content types.

Go back to the list settings page, you will see a Content Type section.

Click Add from existing site content types, and add the new content type we just created and deployed. Remove the default Item content type from this list.

If you add a new item via the New Item button, you will see the new content type:

You can add multiple content types to one list.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: