Powered By Blogger

Jul 6, 2012



Create a SharePoint List definition using c#


This blog post describes an easy way to create a List definition using Microsoft Visual Studio 2010.

First you have to open a List Definition Project in Visual Studio. I have named it as 'ListDefinitionProject1'


Then give the Site name and select 'Deploy as a sandboxed solution'.



Then select 'Custom List' as the list definition type.



Now the solution Explorer will look like as shown below. Then open the Elements.xml file.
(ListDefinition1 -> ListInstance1 -> Elements.xml)


Then edit the Title and the URL as shown below.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="MyList1"
                OnQuickLaunch="TRUE"
                TemplateType="10000"
                Url="Lists/MyList1"
                Description="My List Instance">
  </ListInstance>
</Elements>


Finally Build and Deploy the solution.Then you can see the new List called "MyList1" has created inside the SharePoint site under the Lists.



Now you can add fields to the above created List.
Open the Schema.xml file and within the <Fields></Fields> tag you can add the fields as preferred.

 <Fields>
      <Field Type="Note" Name="ListName" DisplayName="Name" Required="FALSE" ID="{1D54AAC6-DBB8-4454-91B7-E2FAE7697D6A}">
      </Field
    </Fields>


Then inside the ViewFields section of the second View add the FieldRef node as shown below.


<FieldRef Name="Name"></FieldRef>



Now the field 'Name' is visible in the 'MyList1' List.


No comments:

Post a Comment