Tuesday, May 25, 2010

Developing a List Definition in SharePoint 2010



This tutorial will guide you through creating a SharePoint 2010 List Definition using Visual Studio 2010.

Open Visual Studio 2010

From the Visual Studio 2010 menu, select File --> New --> Project....
Select the Visual C# --> SharePoint 2010 --> List Definition project template.
Enter "Music" in the Name textbox and click on OK.

In the SharePoint Configuration Wizard select "Deploy as farm solution" and click on "Next"




















Enter "Music" in the What is the display name of the list definition? textbox.
Select Custom List in the What is the type of the list definition? drop-down box.




















In the Solution Explorer, expand ListInstance1 and open the Elements.xml file (do not confuse this Elements.xml file with the other Elements.xml file which is on the same level as ListDefinition1)


 
Within the ListInstance element change:
      the Title attribute to Music
      and change the TemplateType attribute to 10001.


Now, open the Elements.xml file which is on the same level as ListDefinition1.
Within the ListTemplate element, change the Type attribute to 10001 and add the DisallowContentTypes=”FALSE” attribute, as shown 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"
DisallowContentTypes="FALSE"
Type="10001"
BaseType="0"
OnQuickLaunch="TRUE"
SecurityBits="11"
Sequence="410"
DisplayName="Music"
Description="My List Definition"
Image="/_layouts/images/itgen.gif"/>
</Elements>

Insert the following xml into the top of the Elements element in the Elements.xml file. This XML describes the Music Item content type the list will store.
<ContentType

ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94"
Name="Music Item"
Group="Custom Content Types"
Description="Music item content type."
Version="0">
<FieldRefs>
<FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" />
<FieldRef ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" />
<FieldRef ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" />
<FieldRef ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" />
</FieldRefs>
</ContentType>

Insert the following xml into the top of the Elements element in the Elements.xml file. This XML describes the Fields the Music Item content type uses.
<Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE" Sortable="FALSE" ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="Album Description" Name="Album Description" Group="Custom Columns" />The Elements.xml file should now look like the following.


<Field Type="Text" DisplayName="Title" Required="FALSE" MaxLength="255" ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" StaticName="AlbumTitle" Name="AlbumTitle" Group="Custom Columns" />


<Field Type="Text" DisplayName="Artist" Required="FALSE" MaxLength="255" ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" StaticName="Artist" Name="Artist" Group="Custom Columns" />












































In the Solution Explorer, open Schema.xml
Add the EnableContentTypes=”TRUE” attribute to the List element inside of the Schema.xml file












Insert the following XML into the ContentTypes element in the schema.xml file. This XML describes the Music Item content type this list will store.

<ContentTypeRef ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94" />



















Insert the following XML into the Fields element. This XML describes the fields the list will store. These are directly related to the fields in the Content Type we added in the previous step.

<Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE"

Sortable="FALSE"
ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="Album Description"
Name="Album Description" Group="Custom Columns" />
<Field Type="Text" DisplayName="Title" Required="FALSE" MaxLength="255"
ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}"
StaticName="AlbumTitle" Name="AlbumTitle" Group="Custom Columns" />
<Field Type="Text" DisplayName="Artist" Required="FALSE" MaxLength="255"
ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}"
StaticName="Artist" Name="Artist" Group="Custom Columns" />

Insert the following XML into the ViewFields element in the 2nd view, BaseViewID="1". This XML describes which fields should be visible in this particular view. BaseView 1 is set as the default view and this is the view the newly created fields should display in.

<ViewFields>

<FieldRef Name="Attachments"></FieldRef>
<FieldRef Name="LinkTitle"></FieldRef>
<FieldRef Name="Album Description"></FieldRef>
<FieldRef Name="AlbumTitle"></FieldRef>
<FieldRef Name="Artist"></FieldRef>
</ViewFields>
 
 From the menu, select Build
From the menu, select Deploy
Open your target SP2010 website in a browser session.
Under Lists in the left-side navigation, click on Music to view the new list.

















Enjoy !

10 comments:

Anonymous said...

but how to dynamically add entries to that list?

Anonymous said...

written sparingly and uninteresting. theme completely uncovered...

Anonymous said...

Hey hi..

thanks for the post, it really gave me jump start.

but my requirement was creating a list definition using sandbox solutions and without creating content type, just using metadata

i did a some background work

i came up with this
http://sharepointbuzzer.wordpress.com/2010/10/21/list-definition-using-sandbox-in-sharepoint-2010/

let me know whats your thought on it

Anonymous said...

It seems there are a few mistakes....Insert the following xml into the top of the Elements element in the Elements.xml file. This XML describes the Fields the Music Item content type uses. ...should be schemas.xml

Unknown said...

Your solution is rocking for start up ...

Can you please what's main purpose of list defination ?

Alex said...

Hello,

Unfortunately in my case after deployment when I try to edit any item from this list I see only "Title" field in edit dialog window...

Any suggestions?
Thank you upfront!

Anonymous said...

how does this solution work for sandbox solution? need to deploy to sharepoint online, but got the same error as Alex....

Memtech said...

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

Unknown said...

how to get particular list field id?

Violet P said...

I ennjoyed reading this

Post a Comment