Fabian Williams SharePoint Blog

Solving problems with SharePoint day and night

How To: Create Content Types with Site Columns in Code Visual Studio 2010 for SharePoint 2010


Synopsis: So why would you Ever Ever need to feel the pain of creating Content Types from scratch and associate Site Columns also in code with them? Well, the one glaring reason is that if you create site columns and content types out of Code, you loose the control of assigning the GUID which is in effect the id designator of the object.  If you cannot control that, then as you move through a proper SDLC with a DTP environment, you in essence loose control over the same ID’s [objects] being the same across all the environment.  The second reason is that you have a clean and automated way to deploy your solution to any environment, inherently you could package and sell this solution if you wanted to.

So how do you do it?

Sahil Malk (@sahilmalik)has a great book in Building Solution in SharePoint 2010 and he has a great chapter on this topic. I used that as my premise for creating a full fledge Content Type with Site Columns to boot. Doing this in Visual Studio 2010 on SharePoint 2010 and the entire process to deploy is so easy now, even a caveman can do it.

Like Bud Light — Here we go!

First we need to start a new SharePoint Empty Project and start to build your solution out.

clip_image001

Next, and very appropriately, we are doing this as a Sandbox Solution because EVERYTHING we are doing for this effort will be in the Database, nothing will be on the File System. This is where you as a developer can do all the damage you want without worrying about the Farm Admin calling you saying “…hey dude, you brought down the server..” will never happen, sure you can bring down the Site Collection but hey, thats on you buddy..

clip_image002

Once you have your Sandbox Solution set, lets had some “SharePoint Items” first will be a Content Type

clip_image003

As you see below, I am calling mine AdotobClient. The idea here is that for my company Adotob, LLC, I am creating a Content Type that inherits from the Item Content Type which will hold Client specific data. The important thing here is that as a Content Type, this can  be used all across the Enterprise in any Web by anyone.  Build it once, use it many.

clip_image004

Once named, now your project will look like this.

clip_image006

As I mentioned before and as you will see below, everything in SharePoint inherits from a base, in this example I am inheriting from the Item Content Type, the wizard asks me to choose and i selected my choice

clip_image007

clip_image008

Now we will do some clean up. When you add the Content Type, you also get an elements.xml file created for the definition of the Content Type, now realistically if this content type is to be of any use, it will need Site Columns, but for now, we will just rename the elements.xml to something more descriptive.

clip_image009

clip_image010

So, this Content Type will have a few site columns to express what it is about the client we want to capture, it will be of many data types, text, date/time, etc. lets go and add an Empty Element which will be the Site Columns and populate the Elements.xml with the fieldtypes.

clip_image011

Before we do lets spot check what our Project looks like now.

clip_image012

As for clean up, lets rename the Site Columns elements.xml to a more representative name…

clip_image013

Lets us now flesh out the Site Columns, as you see below i am capturing all the necessary information one would capture about a client. Name, address, contact info, social content, etc. Below I use the Make Guid tool in Visual Studio to create those Field ID’s, and i with all my cut and paste, i screwed up the last entry, that type was to be a Text but i left it as date time.. oops.

clip_image014

Once we do that, we now associate those site columns via the GUID [Field ID == Field Ref] in what was the elements.xml of the Content Type

clip_image015

Once you are done, go ahead and build and deploy your solution. Oh make sure that you have Sandbox Solution Service turned on in the Central Administration before you do that, it is not on by default. Below if you inspected your Site Settings at the top level you will find the new Content Type

Trust but Verify

 

clip_image016

Click inside the Content Type, and you will find the site columns we defined.

clip_image017

Once we have done that lets spin up a new Custom List. We will include this content type in there and create an entry

clip_image018

To do that we need to manage content types…

clip_image019

We select the one we created and move it over

clip_image020

I also disabled the Item default content type what was there before so the only one that is an option is the one we created.. see below

clip_image021

Now we will make an entry for a new client

clip_image022

clip_image023

And our result is..

clip_image024

Voilla! you have a Content Type with Site Columns that you can manage NOT ONLY across the enterprise, but between Development, Test and Production Environment.

As usual, your comments, critique and questions are welcomed.

Advertisements

July 3, 2010 - Posted by | Content Types, SharePoint 2010, SharePoint 2010 RTM, SharePoint Development, Site Columns, Visual Studio 2010

8 Comments »

  1. […] This post was mentioned on Twitter by sahilmalik, Adotob LLC. Adotob LLC said: check out @fabianwilliams latest blog on #SharePoint 2010 Content Types built in #VisualStudio 2010 http://bit.ly/bPIgCG please/thanks! […]

    Pingback by Tweets that mention How To: Create Content Types with Site Columns in Code Visual Studio 2010 for SharePoint 2010 « Fabian Williams Blog -- Topsy.com | July 5, 2010 | Reply

  2. Great Post Fabian!

    Comment by André | September 17, 2010 | Reply

    • Thanks Mate!

      Comment by fabiangwilliams | September 22, 2010 | Reply

  3. Thank you for sharing!
    But when i added a New Item of Content Type,it poped up that “Input string was not in a correct format.” The same happened when I created a new project of Content Type.Do you know How to Fixed this?

    Comment by pheobe | October 26, 2010 | Reply

  4. Awesome post Fabian

    Comment by Mark Rackley | November 29, 2010 | Reply

  5. Is it possible to use the existing SharePoint OOTB site columns [eg Rating (0-5)] and if so how?

    Comment by FB | January 31, 2011 | Reply

  6. I like to set up this type of a comment column with ability of reply on my website.
    Can or anyone write html or javarscript for my website? and if yes, please write email to me to discuss about the return for it.

    thanks

    Comment by yuzo nakano | September 17, 2013 | Reply

    • its just a field like anything else that sits on the page. when the page loads, its in the DOM, and if it is in the DOM you can use JavaScript to manipulate it. Im not sure I understood your questions but based on the information you provided, this should answer it.

      Comment by fabiangwilliams | September 17, 2013 | Reply


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: