Thursday, January 5, 2012

How to Create a Custom List Definition for SharePoint 2010 in Visual Studio

So you have got a custom list created and wanted to make it as a List definition, so that anyone can create instances of the list which has lot of customized columns.

Well, Creating a List definition is relatively simpler in SharePoint 2010. Follow these steps to create a custom list definition in SharePoint 2010 using Visual Studio.

1. Create a New Visual studio Project, choose List definition as the Project type.

2. Update the  <Fields> node in Schema.xml:
SharePoint 2010 custom list definition schema.xml
If you are creating list definition based on content type, You can update the <ContentTypes> node in Schema.xml with the content type reference, fields, etc. Otherwise, Take off the <ContentTypes> node and insert the <Fields> node alone (Here, that's what we are going to do).

How to get the fields schema XML?
We can get the Field Schema XML from Visual Studio 2010 Server Explorer or Using SharePoint Manager 2010.
create list definition sharepoint
Copy the Field Type schema XML and paste them under <Fields> node

<Field Type="Text" DisplayName="Trainer Name" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" ID="{8a9ad061-1de6-4712-bc8d-99af27e2f60d}" SourceID="{7a9e7fc5-edc2-4072-bafd-8770aa819d39}" StaticName="TrainerName" Name="TrainerName" ColName="nvarchar3" RowOrdinal="0" />
<Field Type="DateTime" DisplayName="Training Date" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="DateOnly" ID="{38407f75-57b1-4992-bd22-7cae650359d4}" SourceID="{7a9e7fc5-edc2-4072-bafd-8770aa819d39}" StaticName="TrainingDate" Name="TrainingDate" ColName="datetime1" RowOrdinal="0" />
<Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Type="Text" Name="Title" DisplayName="Training Title" Required="TRUE" SourceID="" StaticName="Title" FromBaseType="TRUE" MaxLength="255" Indexed="FALSE" ColName="nvarchar4" />

Add Fields to the Default view:
In Schema.xml file navigate to  <Views> <View BaseViewID="1"... ><ViewFields> node and add the fields to the view.
Copy the Name attributes of the <Field> elements (such as Name="TrainerName") and place them under ViewFields
SharePoint 2010 list definition add field
          <FieldRef Name="Title"></FieldRef>
          <FieldRef Name="TrainerName"></FieldRef>
          <FieldRef Name="TrainingDate"></FieldRef>

4. Creating Instances Of Custom List Definition:
You can create a List instances of custom list definition by specifying TemplateType and FeatureId parameters in ListInstance's Elements.xml as Same as the Custom List definition Feature's. (Just the TemplateType Parameter would do!, FeatureId is optional)

List Instance's TemplateType and List Template's Type must be Same to create List Instance of the particular List definition.

SharePoint 2010 list Instance element.xml
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
  <ListInstance Title="Training List"
                Description="Training List for Portal">
sharepoint 2010 list definition example
Build & Deploy the solution. That's all! We are done creating SharePoint 2010 custom list definition using visual studio.

Last but not Least:
  • It is possible to create a List definition from a Content type. Visual studio offers project template for creating list instance from content type. All you have to do is: Create a Content Type Project, Add relevant fields to the Content type. Once done, Add New Item >> choose "List Definition From Content Type". 
MSDN Post on create list definition SharePoint 2010 existing list:

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...