Friday, August 30, 2013

Create update Delete list item using SharePoint 2013 object model

Here we will see how we can create a list item using SharePoint 2013 client object model. Also you can check:

- Create list using SharePoint 2013 client object model

- More ways to share documents or sites in SharePoint 2013

- SharePoint 2013 Developer Site Template

Here is the full code:
ClientContext context = new ClientContext("http://SiteURL");

List myList = context.Web.Lists.GetByTitle("MyCustomList");

ListItem newItem = myList.Items.Add();

newItem["Title"] = "My First Item";

newItem["Description"] = "My First item Desciption";

newItem.Update();

context.ExecuteQuery();

The above client object model code will add one item to MyCustomList.

Update a list item:

ClientContext context = new ClientContext("http://SiteURL");

List myList = context.Web.Lists.GetByTitle("MyCustomList");

ListItem listItem = myList.Items.GetById(1);

listItem["Title"] = "This is the new title";

listItem["Description"] = "This is the new description";

listItem.Update();

context.ExecuteQuery();

Here it will update the item whose ID=1.

Delete list item:

ClientContext context = new ClientContext("http://SiteURL");

List myList = context.Web.Lists.GetByTitle("MyCustomList");

ListItem listItem = myList.Items.GetById(1);

listItem.DeleteObject();

context.ExecuteQuery();

This will delete the item whose ID = 1.


Twitter Delicious Facebook Digg Favorites More