How To : Programmatically Create A Custom List in SharePoint 2010 using Visual Studio 2010

Let’s  create a Candidates List in SharePoint 2010 using Visual Studio 2010:

A list will be having following columns:

1. Title

2. Name

3. Home City

And then we’ll add rows into it programmatically.

Steps :

1. Open Visual Studio 2010 -> File -> New Project -> Visual C# (You may choose Visual Basic) -> SharePoint -> 2010

1.1     Deploy it on Farm Level

2.  Select List Definition as Project Type, give a proper name to your List Definition and press OK.

3.  In Solution Explorer, you will see few files listed automatically by Visual Studio 2010 itself.

4.  We need to look into below files :

1. ListDefinition1 -> ListInstance1 -> Elements.xml

2. ListDefinition1 -> Elements.xml

3. ListDefinition1 -> Schema.xml

5.  First, let’s start with ListDefinition1 -> ListInstance1 -> Elements.xml

Change the ListIntance as below :

<?xml version=”1.0″ encoding=”utf-8″?>
  <ListInstance Title=”SampleList1″
                OnQuickLaunch=”TRUE”
                TemplateType=”10001″
                Url=”Lists/SampleList1-ListInstance1″
                Description=”My List Instance”>
    <Data>
      <Rows>
        <Row>
          <Field Name =”Title”>Mr</Field>
          <Field Name =”Name”>Naimish</Field>
          <Field Name=”HomeCity”>Las Vegas</Field>
        </Row>
        <Row>
          <Field Name =”Title”>Mr</Field>
          <Field Name=”Name”>Vishal</Field>
          <Field Name=”HomeCity”>New York</Field>
        </Row>
        <Row>
          <Field Name =”Title”>Mr</Field>
          <Field Name=”Name”>Vijay</Field>
          <Field Name=”HomeCity”>Chicago</Field>
        </Row>
      </Rows>
    </Data>
  </ListInstance>
</Elements>
6.  Change ListDefinition1 -> Elements.xml
 
Please note that Field IDs will be different for your project. To generate your Field IDs (GUID), In Visual Studio 2010, Go To Tools -> Create GUID

<?xml version=”1.0″ encoding=”utf-8″?>

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;

<Field ID=”{E9C12CCA-26E4-47CA-A388-8959C11CDD13}” Name=”Title” Type=”Note” DisplayName=”Title” Required=”FALSE” ></Field>

<Field ID=”{64B80007-B8AD-4312-B4CA-ADF57500EBF8}” Name=”Name” Type=”Note” DisplayName=”Name” Required=”FALSE” ></Field>

<Field ID=”{AAABDB73-F548-4D79-AED7-4BE42B9A05C3}” Name=”HomeCity” Type=”Note” DisplayName=”HomeCity” Required=”FALSE” ></Field>

<ContentType

ID=”0x01004d360004c8074de6a25f4d8cdd3be8dd”

Name=”SimpleList1″

Group=”Custom Content Type”

Description=”SimpleList1″

Version=”0″>

<FieldRefs>

<FieldRef ID=”{E9C12CCA-26E4-47CA-A388-8959C11CDD13}”/>

<FieldRef ID=”{64B80007-B8AD-4312-B4CA-ADF57500EBF8}”/>

<FieldRef ID=”{AAABDB73-F548-4D79-AED7-4BE42B9A05C3}”/>

</FieldRefs>

</ContentType>

<!– 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=”10001″

BaseType=”0″

AllowDeletion=”FALSE”

DisallowContentTypes=”FALSE”

OnQuickLaunch=”FALSE”

SecurityBits=”11″

Sequence=”410″

DisplayName=”SampleList1″

Description=”My List Definition”

Image=”/_layouts/images/itgen.png”/>

</Elements>

7. ListDefinition1 -> Schema.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<List xmlns:ows=”Microsoft SharePoint” EnableContentTypes=”TRUE” Title=”SampleList1″ FolderCreation=”FALSE” Direction=”$Resources:Direction;” Url=”Lists/SampleList1-ListDefinition1″ BaseType=”0″ xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;

<MetaData>

<ContentTypes>

<ContentTypeRef ID =”0x01004d360004c8074de6a25f4d8cdd3be8dd”></ContentTypeRef>

<ContentTypeRef ID=”0x01″>

<Folder TargetName=”Item” />

</ContentTypeRef>

<ContentTypeRef ID=”0x0120″ />

</ContentTypes>

<Fields>

<Field ID=”{E9C12CCA-26E4-47CA-A388-8959C11CDD13}” Name=”Title” Type=”Note” DisplayName=”Title” Required=”FALSE” ></Field>

<Field ID=”{64B80007-B8AD-4312-B4CA-ADF57500EBF8}” Name=”Name” Type=”Note” DisplayName=”Name” Required=”FALSE” ></Field>

<Field ID=”{AAABDB73-F548-4D79-AED7-4BE42B9A05C3}” Name=”HomeCity” Type=”Note” DisplayName=”HomeCity” Required=”FALSE” ></Field>

</Fields>

<Views>

<View BaseViewID=”0″ Type=”HTML” MobileView=”TRUE” TabularView=”FALSE”>

<Toolbar Type=”Standard” />

<XslLink Default=”TRUE”>main.xsl</XslLink>

<RowLimit Paged=”TRUE”>30</RowLimit>

<ViewFields>

<FieldRef Name=”LinkTitleNoMenu”></FieldRef>

</ViewFields>

<Query>

<OrderBy>

<FieldRef Name=”Modified” Ascending=”FALSE”></FieldRef>

</OrderBy>

</Query>

<ParameterBindings>

<ParameterBinding Name=”AddNewAnnouncement” Location=”Resource(wss,addnewitem)” />

<ParameterBinding Name=”NoAnnouncements” Location=”Resource(wss,noXinviewofY_LIST)” />

<ParameterBinding Name=”NoAnnouncementsHowTo” Location=”Resource(wss,noXinviewofY_ONET_HOME)” />

</ParameterBindings>

</View>

<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=”Attachments”></FieldRef>

<FieldRef Name=”LinkTitle”></FieldRef>

</ViewFields>

<Query>

<OrderBy>

<FieldRef Name=”ID”></FieldRef>

</OrderBy>

</Query>

<ParameterBindings>

<ParameterBinding Name=”NoAnnouncements” Location=”Resource(wss,noXinviewofY_LIST)” />

<ParameterBinding Name=”NoAnnouncementsHowTo” Location=”Resource(wss,noXinviewofY_DEFAULT)” />

</ParameterBindings>

</View>

</Views>

<Forms>

<Form Type=”DisplayForm” Url=”DispForm.aspx” SetupPath=”pages\form.aspx” WebPartZoneID=”Main” />

<Form Type=”EditForm” Url=”EditForm.aspx” SetupPath=”pages\form.aspx” WebPartZoneID=”Main” />

<Form Type=”NewForm” Url=”NewForm.aspx” SetupPath=”pages\form.aspx” WebPartZoneID=”Main” />

</Forms>

</MetaData>

</List>

And Done 🙂 Refresh your SharePoint 2010 site!!

That’s it!!

Though I have tried my best to explain it in as simple way was possible, I would highly recommend to go and read Ben Hedges article on Creating SharePoint 2010 List Definitions in Visual Studio 2010.

Advertisements

Tags: , , , , , , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: