"We are what our thoughts have made us; so take care about what you think. Words are secondary. Thoughts live; they travel far."

Create a List Definition in SharePoint 2010 using Visual Studio 2010

In this demonstration, I am going to create a list definition using Visual Studio 2010. For the demonstration purpose, I am going to create a list definition for storing expenses, once deployed, the list definition can be used to create lists to store expenses. The list definition will define a list template that inherit from custom list and add columns expensedate and amount.

Open Visual Studio, Go to File -> New project, In the Template selection dialog, choose List Definition as the template type.

Give a proper name to your List Definition project. Make sure the .Net framework selected is 3.5 as SharePoint 2010 is built on .Net framework 3.5.clip_image002

Click OK button, once you entered all the details.

In the next dialog you need to choose the SharePoint portal you need to use for debug. Also you need to specify whether it is a farm solution or sand boxed solution. Enter your SharePoint portal address and Click Next

clip_image003

Click Next once you are done

Now Visual Studio will bring you the List definition settings dialog. Here you can define the title for your list definition and choose a base list to inherit from. I choose my base list as “Custom List”. In addition to this you can decide whether to include a list instance when deploying this feature. Since I want to check my list definition, I selected “Add a list instance to this list definition” option. Click finish button, once you are done


clip_image004

Click Finish button once you are done with this step.

Visual Studio will add a default feature to your project and add a list definition. In the solution explorer the project will look similar to the following. I published an article for developing a SharePoint 2010 feature using Visual Studio, to read that article click here.

clip_image005

Let me explain the files exists under the List Definition

Elements.xml – this file defines the list template and fields such as name, display name, type etc. The properties specified in this file will be displayed in the Create Page, when you create a list template from this file.

Let us examine the Elements.Xml file. See the snapshot of the elements.xml file

clip_image006

The complete reference for Elements file for List template can be found in the below URL

http://msdn.microsoft.com/en-us/library/ms462947.aspx

The type identifier must be unique within the feature, but need not be unique across all feature definitions or site definitions. So make sure when you have multiple list templates in the same feature, you made them with different type attributes. It is also recommended to create Type value greater than 10000. For my List Instance I assigned the Type attribute with a value of 10001.

After Modifications, the List template file looks as follows.

clip_image007

Notice that, I just updated the Type attribute, but depending on your needs you may add other attributes.

Now I am going to add fields for my List template. As I mentioned initially, I will have only 2 fields date and amount. Refer this link to under stand more about Field element http://msdn.microsoft.com/en-us/library/ms437580.aspx

When you define a field, you need to specifiy a guid as ID enclosed in braces{}. To create a Guid, do the following steps.

From visual Studio menu, select tools, then select create guid

clip_image009

The Create Guid dialog will appear as follows

clip_image010

I have added the following fields to List Template.

 

<Field Type="DateTime" DisplayName="Expense Date" Required="TRUE" ID="{AB764690-E5F3-48EC-8E54-EDDB6900574A}" StaticName="ExpenseDate" Name="ExpenseDate" Group="Expense Columns" />
<Field Type="Number" DisplayName="Amount" Required="TRUE" ID="{DDCD3DE5-5975-4C4E-B0C2-1460D84EC7A8}" StaticName="Amount" Name="Amount" Group="Expense Columns" />

Make sure for each field you create you use unique guid. Now I want to create a content type that includes these fields. When you define a content type, you need to define an ID for the content type. This is a bit tricky. Refer the below article to under stand more about content type ID.

http://msdn.microsoft.com/en-us/library/aa543822.aspx

My content type should be a sub of item content type, so I am going to use the following rule to create my list content type id.

 

System

Item

prefix

Hexadecimal GUID

0x

01

00

4FDDEDBF38D14332A625BCBC60F1667A

 

(for hexadecimal guid, I create a guid from visual studio and then removed braces and hyphens)

So my content type id is 0x01004FDDEDBF38D14332A625BCBC60F1667A

Add the following content type tag just above the ListTemplate start tag.

<ContentType ID="0x01004FDDEDBF38D14332A625BCBC60F1667A" Name="Expense Item" Group="Expense Content Types" Description="Expense item content type." >
  <FieldRefs>
    <FieldRef ID="{AB764690-E5F3-48EC-8E54-EDDB6900574A}"/>
    <FieldRef ID="{DDCD3DE5-5975-4C4E-B0C2-1460D84EC7A8}"/>
  </FieldRefs>
</ContentType>

The definition is very straight forward. See the <FieldRefs> tags where you add the reference to previously created fields.

Find the snapshot of Elements.xml file after the above items added.

clip_image012

Now you have defined fields, content type and list template. Now you need to link your list definition to content type. You need to perform this in the schema.xml file. Open schema.xml file, by default it will look similar to the below snapshot.

clip_image013

Now you need to replace the <ContentTypes></ContentTypes> element with the below

 

<ContentTypes>
  <ContentTypeRef ID="0x01004FDDEDBF38D14332A625BCBC60F1667A"></ContentTypeRef>
</ContentTypes>

Now copy all the <Field> tags from Elements.xml and paste in between <Fields> and </Fields>. This is a duplication of work but you need to do this. After replaced, the Fields tag will look as follows.

 

<Fields>
  <Field Type="DateTime" DisplayName="Expense Date" Required="TRUE" ID="{AB764690-E5F3-48EC-8E54-EDDB6900574A}"
  StaticName="ExpenseDate" Name="ExpenseDate" Group="Expense Columns" />
  <Field Type="Number" DisplayName="Amount" Required="TRUE" ID="{DDCD3DE5-5975-4C4E-B0C2-1460D84EC7A8}"
  StaticName="Amount" Name="Amount" Group="Expense Columns" />
</Fields>

Now you need to add the field references to view fields. There will be multiple <View> tags, you can add field references in all or atleast in the View with BaseViewID=1, as this is the default view. See the snapshot of default view in the schema.xml file

clip_image014

In between ViewFields add the below markup.

<FieldRef Name="ExpenseDate"></FieldRef>

<FieldRef Name="Amount"></FieldRef>

See the snapshot of schema.xml after made the changes.

clip_image016

clip_image018

Now you are done with the list definition. Now you need to modify the List Instance to use the template you just created. Open the elements.xml file under the List Instance 1, the snap shot of default Elements.xml is as follows.

clip_image019

You need to change the TemplateType to the Type attribute you mentioned in the List Template, i.e. 10001. Also you can modify the list title, url etc. Also you can add initial data to the list instance so that the data will be inserted to the default list instance on feature activation.

Place the below xml inside <ListInstance> tag for inserting initial data

<Data>
  <Rows>
    <Row>
      <Field Name="Title">travel expense</Field>
      <Field Name="ExpenseDate">01-01-2011</Field>
      <Field Name="Amount">20.00</Field>
    </Row>
    <Row>
       <Field Name="Title">your expense title</Field>
      <Field Name="ExpenseDate">01-01-2011</Field>
      <Field Name="Amount">10.00</Field>
    </Row>
  </Rows>
</Data>

See the snap shot of List Instance 1 after all changes were made.

clip_image020

Now we are ready to deploy the List defenition. Right click the project and click on deploy.

clip_image021

If you made everything correct, visual Studio will successfully deploy the list definition. Now you can find one list instance created in your SharePoint site. Open the list instance, you will find the data already inserted to it.

clip_image023

When you create new item in SharePoint, you will find the expense template.

clip_image025

It is easy to create List Definitions for SharePoint 2010 using Visual Studio 2010. Once you successfully tested your list definition project, you can package it to WSP file and deploy to multiple farms.

5 Comments

  • This is one of the best articles so far I have read online. No crap, just useful information. Very well presented. Its really give a wonderful information of sharepoint to beginner as well as developer. Thanks for sharing with us. Check out this link too its also having a wonderful explanation on list definition in sharepoint 2010...
    http://mindstick.com/Articles/db52b9c2-9775-4899-a1f3-4f85fce9ec32/?List%20Definition%20in%20SharePoint%202010

    Thanks

  • Hi ,
    good aricle on creation of list definition.

    Thanks,
    Devendra

  • Hi.

    Nice article.

    I need to customize the forms to view and edit the list items. How I develop the forms in VS?

  • Thank you very much. Nice explanation.

  • Error If you try to delete the Column ExpenseDate or Amount after deploying.

    Any Solution on that ? Please try and reply.
    My Email Address: shriji1111.brijesh@gmail.com

    Please reply on that. By the Way really nice article this is to understand Custom List Definition.

Comments have been disabled for this content.