Pages

Monday, August 18, 2014

Add items to SharePoint list declaratively through Visual Studio

Problem

We had to add couple items to SharePoint list declaratively. So that when we deploy the solution, SharePoint list and the default items will be provisioned in SharePoint site.

Solution

Step 01:

Add a SharePoint list to Visual Studio.

Step 02:

Open the Elements.xml file of list instance.

image

So you will see something like this.

image

Step 03:

Add the data.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  3.   <ListInstance Title="List1"
  4.                 OnQuickLaunch="TRUE"
  5.                 TemplateType="10000"
  6.                 Url="Lists/List1"
  7.                 Description="My List Instance">
  8.     <Data>
  9.       <Rows>
  10.         <Row>
  11.           <Field Name="Title">Item 001</Field>
  12.         </Row>
  13.         <Row>
  14.           <Field Name="Title">Item 002</Field>
  15.         </Row>
  16.       </Rows>
  17.     </Data>
  18.   </ListInstance>
  19. </Elements>

Here the Name in the field will be column name.

Conclusion

Right click and deploy. Then open the list in the browser and you can see the items.

image You can download the basic sample solution from the below link.

https://drive.google.com/file/d/0ByEnOE8DAdvhVjB4RVkxUk9CcWM/edit?usp=sharing

No comments:

Post a Comment