Click here to Skip to main content
15,867,568 members
Articles / Visual Studio / Visual Studio 2010
Article

SharePoint 2010 - Create List Definition and Instance

Rate me:
Please Sign up or sign in to vote.
4.55/5 (11 votes)
28 Jun 2012CPOL5 min read 117.7K   969   13   21
How to create a List Definition and List Instance using the SharePoint user interface and Visual Studio 2010.

Introduction

In this article we can see how to create a List Definition using:

  • SharePoint user interface
  • Visual Studio 2010

Why we need List Definitions?

We need to have List Definitions for creating multiple list instances from it. This is the best way if we are dealing with data capture of same column types.

The built-in list definitions of SharePoint serve the same purpose. Example: Contact List, Task List, etc. Similarly depending on your project domain, it might be a good idea to turn common list formats into list definitions so that you can create multiple instances easily.

For example: a Computer Configuration list definition could be created for later creation of actual lists like Desktops and Laptops.

Image 1

Scenario

In this example we are trying to capture a Computer Configuration list definition. Following is the field information it should contain:

FieldDescription

Manufacturer

Text Field

Model

Text Field

CPU Make

Choice Field with values of Intel, AMD, Other

CPU Speed

Number Field with Speed in GHz

RAM Size

Number Field with Size in GB

Price

Number Field representing Price

Description

More information on the otherperipherals

Solution 1: Creating List Definition using SharePoint user interface

Step 1: Create a new List

Open a SharePoint site and create a new list named Desktops. You can start with a Custom list.

Following are the list columns after creation:

Image 2

Following is the List in Default View:

Image 3

Step 2: Template the List

Now we can proceed with creating a List Definition using the List Template feature of SharePoint. Go to List Settings and click on Save list as template link.

Image 4

In the appearing dialog, enter the name for the template, for example: Computer Configuration Template.

Image 5

You can leave the checkbox Include Content as unchecked. Click the OK button after entering the details. You will get the following message.

Image 6

Step 3: Visit the Gallery

The newly created template is stored inside the gallery. Clicking the link from the above screen, you can view it.

Image 7

You can also access the list template gallery from: Site Actions > Site Settings > Galleries.

Image 8

Step 4: Create a new List from Template

Now we are ready to test the above template. Try creating a new List (Lists > Create) and in the selection dialog, you can see the new template as shown below:

Image 9

Now you can create multiple list instances from this template. You can also upload this template to another SharePoint site for creating list instances using it.

Solution 2: Creating a List Definition using Visual Studio 2010

For proceeding with this part you need the SharePoint Developer Tools for Visual Studio 2010.  I hope you already have this installed; else you can download it from here.

Step 1: Create the Project

Open Visual Studio and create a new project from the List Definition template.

Image 10

In the next page, select the farm solution option.

Image 11

In the next page, enter the details as shown below and click the Finish button.

Image 12

Please note that you need to derive it from Custom List as we are going to create custom columns for the List Definition.

Now inside the Solution Explorer, you can rename the List Definition name as ComputerConfiguration.

Image 13

About the XML files

As you can see, there are two XML files for the List Definition:

  • Elements.xml
  • Schema.xml

The Elements.xml contains general information about the List Definition like the Name, Description, Quick Launch, etc. Schema.xml, as the name implies, contains the actual metadata information about the List Definition.

Following is the XML content of Elements.xml.

XML
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <!-- Do not change the value of the Name attribute below. If it does not 
         match the folder name of the List Definition project item, 
         an error will occur when the project is run. -->
    <ListTemplate
        Name="ComputerConfiguration"
        Type="10000"
        BaseType="0"
        OnQuickLaunch="TRUE"
        SecurityBits="11"
        Sequence="410"
        DisplayName="Computer Configuration"
        Description="Computer Configuration"
        Image="/_layouts/images/itgen.png"/>
</Elements>

You can modify the above List Definition to change the Display Name and Description.

Step 2: Create the Fields for List Definition

The field information for the List is already mentioned in the beginning of the article. Please have a look at it before we proceed.

Field and FieldRef

For adding a List Definition we have to use the following tags:

  • Field
  • FieldRef

The Field tag is used to represent new field information like the Name, Display Name, ID, Type etc. The FieldRef tag is used to refer to an existing field using the Name.

Open Schema.xml and add the following fields.

XML
<Fields>
  <Field ID="{24B3F6C1-B75B-4821-A466-C1E979B87B81}" Name="Manufacturer" DisplayName="Manufacturer" Type="Text" Required="TRUE"/>
  <Field ID="{24B3F6C1-B75B-4821-A466-C1E979B87B82}" Name="Model" DisplayName="Model" Type="Text" Required="TRUE"/>
  <Field ID="{24B3F6C1-B75B-4821-A466-C1E979B87B83}" Name="CPUMake" DisplayName="CPU Make" Type="Choice" Required="TRUE">
    <CHOICES>
      <CHOICE>Intel</CHOICE>
      <CHOICE>AMD</CHOICE>
      <CHOICE>Other</CHOICE>
    </CHOICES>
  </Field>
  <Field ID="{24B3F6C1-B75B-4821-A466-C1E979B87B84}" Name="CPUSpeed" DisplayName="CPU Speed (GHz)" Type="Number"/>
  <Field ID="{24B3F6C1-B75B-4821-A466-C1E979B87B85}" Name="RAMSize" DisplayName="RAM Size (GB)" Type="Number"/>
  <Field ID="{24B3F6C1-B75B-4821-A466-C1E979B87B86}" Name="Price" DisplayName="Price" Type="Number"/>
  <Field ID="{24B3F6C1-B75B-4821-A466-C1E979B87B87}" Name="Description" DisplayName="Description" Type="Number"/>
</Fields>

Note: You can refer to the attached source code with this article.

The Choice field type is used to create a drop down list in the runtime. More field types can be found in the References link.

Step 3: Try a Test Deployment

Now you can build the project and if it succeeds, try deploying it. Once deployed, try opening the SharePoint site and create a new list. In the templates dialog you can see our Computer Configuration item.

Image 14

You can try creating a test instance from it. You will notice the following:

  • In the items view there is only one column
  • In the new item dialog there is only one column

Image 15

Where are the Fields we added?

So now I have to answer that till now we have created only Fields. We need to add the Field Reference in the appropriate sections to see the required columns.

Please note that there could be 50 fields in a list and we can just show only the required five fields in the view. We can make other calculated / auto assigned fields hidden in the new/edit dialogs as well.

Step 4: Add Fields to the View

Now we can add the required fields to the View of the list instance.

  • Manufacturer
  • Model
  • CPU Make
  • Price

To achieve this, we need to use the FieldRef tag as shown below.

XML
<View BaseViewID="1" Type="HTML" WebPartZoneID="Main" 
    DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" 
    DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" 
    SetupPath="pages\viewpage.aspx" 
    ImageUrl="/_layouts/images/generic.png" Url="AllItems.aspx">
<Toolbar Type="Standard" />
<XslLink Default="TRUE">main.xsl</XslLink>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
  <FieldRef Name="Model"></FieldRef>
  <FieldRef Name="CPUMake"></FieldRef>
  <FieldRef Name="Price"></FieldRef>
</ViewFields>

Please add the bolded section inside the View with BaseViewID as 1.

Now build the project and deploy it. Go to the SharePoint site and refresh the list which we have created. You can see that the above three fields are displayed.

Image 16

References

Summary

In this article we have explored how to create a List Definition and List Instance using the SharePoint user interface and Visual Studio 2010. The attachment contains the source code we have discussed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.

In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.

Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.

You can find some of his work over here. He blogs at http://jeanpaulva.com

Comments and Discussions

 
QuestionSharepoint Lists field definition Pin
Milen Ch19-Mar-15 23:52
Milen Ch19-Mar-15 23:52 
Questionnot able to insert new item Pin
Member 107598176-Nov-14 17:47
Member 107598176-Nov-14 17:47 
Questionhttp://www.codeproject.com/script/Forums/Edit.aspx?fid=1739279&floc=/Articles/412429/SharePoint-2010-Create-List-Definition-and-Instanc Pin
Member 1061083719-Feb-14 19:26
Member 1061083719-Feb-14 19:26 
GeneralMy vote of 5 Pin
Member 1005308214-May-13 19:50
Member 1005308214-May-13 19:50 
GeneralMy vote of 5 Pin
fcis201013-Feb-13 15:08
fcis201013-Feb-13 15:08 
GeneralRe: My vote of 5 Pin
Jean Paul V.A13-Feb-13 15:15
Jean Paul V.A13-Feb-13 15:15 
GeneralRe: My vote of 5 Pin
fcis201013-Feb-13 15:23
fcis201013-Feb-13 15:23 
GeneralRe: My vote of 5 Pin
Jean Paul V.A14-Feb-13 1:51
Jean Paul V.A14-Feb-13 1:51 
GeneralMy vote of 5 Pin
idds2428-Dec-12 3:57
idds2428-Dec-12 3:57 
GeneralRe: My vote of 5 Pin
Jean Paul V.A28-Dec-12 5:22
Jean Paul V.A28-Dec-12 5:22 
QuestionProblem with Form Pin
H3GE4-Sep-12 0:17
H3GE4-Sep-12 0:17 
AnswerRe: Problem with Form Pin
Ravi Kurapati10-Oct-12 16:12
Ravi Kurapati10-Oct-12 16:12 
GeneralRe: Problem with Form Pin
jedik@windowslive.com18-Nov-12 21:12
jedik@windowslive.com18-Nov-12 21:12 
Questionhow to get fiedld id Pin
Member 917679420-Aug-12 22:58
Member 917679420-Aug-12 22:58 
AnswerRe: how to get fiedld id Pin
Jean Paul V.A21-Aug-12 3:45
Jean Paul V.A21-Aug-12 3:45 
GeneralRe: how to get fied id Pin
Member 917679422-Aug-12 1:02
Member 917679422-Aug-12 1:02 
NewsRe: how to get fied id Pin
Jean Paul V.A22-Aug-12 3:35
Jean Paul V.A22-Aug-12 3:35 
GeneralMy vote of 4 Pin
MB Seifollahi1-Jul-12 21:18
professionalMB Seifollahi1-Jul-12 21:18 
GeneralRe: My vote of 4 Pin
Jean Paul V.A21-Aug-12 3:45
Jean Paul V.A21-Aug-12 3:45 
QuestionStep 1 is List Template not List Defintion Pin
Jayant Sharma29-Jun-12 0:10
Jayant Sharma29-Jun-12 0:10 
AnswerRe: Step 1 is List Template not List Defintion Pin
Jean Paul V.A29-Jun-12 3:35
Jean Paul V.A29-Jun-12 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.