Home > Uncategorized > HOWTO: Create a List and bind a Content Type to it in SharePoint 2010

HOWTO: Create a List and bind a Content Type to it in SharePoint 2010

In the following HOWTO I will explain how you can create a list declaratively on a SharePoint site by making use of the Visual Studio 2010 Tools. In that list we will give automatically add the hotel conten type to the list so that the user can only add “hotels” to that list. If you do not know how to create a content type to achieve this, read the following blog post: https://koenvosters.wordpress.com/2010/04/27/howto-build-a-deployable-content-type-for-sharepoint-2010/

We will start from an Empty SharePoint Project. Rightclick the project, select add new item, List Instance.
Add the display name and description and change the list to instantiate to Custom List. If you want a specific url, make sure you specify this in the wizard.

This should result in the follwing xml being generated for you:

Code Snippet
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  3.   <ListInstance Title="List of Hotels"
  4.                  OnQuickLaunch="TRUE"
  5.                  TemplateType="100"
  6.                  FeatureId="00bfea71-de22-43b2-a848-c05709900100"
  7.                  Url="Lists/Hotels"
  8.                  Description="List of the Hotels that can be booked by the Travel Agency">
  9.   </ListInstance>
  10. </Elements>

This List Instance will create a list based on the content type item. Once we deploy the solution we will see that a new list has been created called “List of Hotels”. Now we move to the steps to add this content type to a list. First thing to know is that we need to be sure that the list and the content type have already been installed on the server. How do we do that? We will create a new feature in our project and that Feature will have an activation dependency on the feature with the list instance and the content type xaml definition. At that moment the future binding the two together can only be executed when both content types have been created.


Add the feature, rename it to something meaningfull like ConnectingListCtypes and double click it. In that screen, give it as title Travel Agency. At the bottom of the screen you will see an area called Feature Activation Dependencies. Click on Add on the right side and select the feature with the list instance and the content type in it. Then click Add.


As we have two features it would mean the user would have to click twice to enable our Travel Agency list with content types. To avoid this behavior we will make the feature containing the list instance and the content type hidden. You can do this by going to the properties of the Feature and setting the property Is Hidden to false. That means that whenever I activate my TravelAgent feature it will automatically activate my depencies, invisible to the user.


Ok, so now all is linked together, all we need to do is write the actual code that will link the two together. First, we will add a Feature Receiver to the TravelAgent Feature. You can do this by rightclicking the feature and selecting “Add Event Receiver”. Uncomment the code of the FeatureActivated function.

In the code we will create an object for the list called “list” and an object for the contenttype called mytype. We will add this contenttype to the list and remove the existing content type called “Item” which the list was based on. Do not forget to call the update method on the list or the changes will not be submitted.

Code Snippet
  1. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  2.         {
  3.             try
  4.             {
  6.                 using (SPWeb web = SPContext.Current.Web)
  7.                 {
  8.                     SPList list = web.Lists["List of Hotels"];
  9.                     SPContentType mytype = web.AvailableContentTypes["Hotel"];
  10.                     list.ContentTypesEnabled = true;
  11.                     list.ContentTypes.Add(mytype);
  12.                     SPContentType itemtype = list.ContentTypes["Item"];
  13.                     list.ContentTypes.Delete(itemtype.Id);
  14.                     list.Update();
  16.                 }
  18.             }
  19.             catch (Exception ex)
  20.             {
  21.                 TextWriterTraceListener listener = new TextWriterTraceListener("c:\\customcodelogs.txt");
  22.                 listener.WriteLine(ex.StackTrace);
  23.                 listener.Dispose();
  24.             }
  25.         }

As the other feature is hidden you should only see 1 feature to activate in the site settings. Once you activate it, you should have a content type called Hotel in your site collection and a list called “List of Hotels” in your site. Adding an item in your list will present you the new content type. In one of my next posts we will be creating a custom field type so that we can assign “stars” to hotels in an easier way.

Comments are always welcome 🙂

Categories: Uncategorized
  1. Sak
    October 15, 2010 at 12:32 am

    I am a student, working for a firm.
    I need to work in sharepoint. Having no prior experience in sharepoint has made it really difficult for me to focus and/or even get started.
    I really need help. Can you please please please please help me.

    I have done everything as you showed in your tutorial but i have some doubts, do you mind clearing my doubts? I will be really really thankful to you.

    Please advise.
    Thank you so much for your help in advance.


  1. No trackbacks yet.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: