Create SharePoint list definition using visual studio

List definitions are the templates for creating the list and defining a schema for a SharePoint list. It contains information on what views are being used, which columns and content types are being used, and other metadata information. We can create a list definition using visual studio 2010 in SharePoint 2010.

Create SharePoint list definition using visual studio

SharePoint List definition is used to create multiple list instances from it. Follow the below steps to create a list definition using Visual Studio 2010.

Step-1:
Open Visual Studio 2010, File -> New Project -> Then in the New Project dialog box choose List Definition project from the list of templates. Then give a name and click on Ok.

Step-2:
In the next dialog box give a local site for debugging and then choose to Deploy as a farm solution and then click on Next.

Step-3:
Then in the SharePoint Customization Wizard, Give a display name for the list definition and chose what is the type of list definition. Here I chose custom list and click on Finish as shown in the figure below:

create list definition in sharepoint 2013 programmatically
create list definition in sharepoint 2013 programmatically

Step-4:
Now check out the XML files.

1- Elements.xml:
This .xml file only gives general information about the list definition like Name, Description, Quick Launch etc like below:

<?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="ListDefinition1"
Type="10000"
BaseType="0"
OnQuickLaunch="TRUE"
SecurityBits="11"
Sequence="410"
DisplayName="MyNewListDefinition - CustomListDefinition"
Description="My List Definition"
Image="/_layouts/images/itgen.png"/>
</Elements>

2- Schema.xml:
This file defines all the fields. You can add fileds inside <Fields><Field></Field></Fields> tag. Example below

<Fields>
<Field ID="{DA1D6D10-3047-4A5B-91ED-93A5864153FE}" Name="FirstName" DisplayName="First Name" Type="Text" Required="TRUE"/>
<Field ID="{0F97F5C5-BF6A-40F0-A651-3E2B0CE9C520}" Name="LastName" DisplayName="Last Name" Type="Text" Required="TRUE"/>
<Field ID="{B53C4419-BA73-45D3-AE3B-F96E8B575052}" Name="Gender" DisplayName="Gender" Type="Choice" Required="TRUE">
<CHOICES>
<CHOICE>Female</CHOICE>
<CHOICE>Male</CHOICE>
<CHOICE>Not Specified</CHOICE>
</CHOICES>
</Field>
</Fields>

Here we have added 3 fields name as: FirstName, LastName & Gender.

The full code will be like below:

how to create list definition in sharepoint 2013 using visual studio 2013
how to create list definition in sharepoint 2013 using visual studio 2013

Step-5:
Now we have to give the FieldRef else the fields will not be shown on the list. Do like below:

In the same schema.xml file go to the <Views> section (where BaseViewID= “1”) and add fields ref in the

<ViewFields></ViwFields> section like below:
<ViewFields>
<FieldRef Name="Attachments"></FieldRef>
<FieldRef Name="LinkTitle"></FieldRef>
<FieldRef Name="FirstName"></FieldRef>
<FieldRef Name="LastName"></FieldRef>
<FieldRef Name="Gender"></FieldRef>
</ViewFields>

Here the Name is the name that we have given while defining Fields. It should look like below:

how to create list definition in sharepoint 2010 using visual studio 2010
how to create list definition in sharepoint 2010 using visual studio 2010

If you want to hide attachment and Title column then you can remove those columns from the <ViewFields> section.

Step-6:
Now Build and Deploy the solution. And then Create a New List. In the available templates, you can choose the list definition that you made now. Then you can create the list.

list definition in sharepoint 2013
list definition in sharepoint 2013

Once you create the list. The list will appear like below:

create list definition sharepoint 2010 using visual studio 2012
create list definition sharepoint 2010 using visual studio 2012

You may like the following SharePoint 2010 tutorials:

In this tutorial, we learned how to create list definition in SharePoint 2010.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>