Thursday 22 May 2014

Sharepoint list item using client object model

Create New Item
string siteUrl = "http://sp2013";
ctx ctx = new ctx(siteUrl);
List oList = ctx.Web.Lists.GetByTitle("MyList");
ListItemCreationInformation lstCre = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(lstCre);
oListItem["Title"] = "Hello World";
oListItem.Update();
ctx.ExecuteQuery();


Update List Item
string siteUrl = "http://sp2013";
ctx ctx = new ctx(siteUrl);
List oList = ctx.Web.Lists.GetByTitle("MyList");
ListItem oListItem = oList.GetItemById(5);
oListItem["Title"] = "Hello World Updated!!!";
oListItem.Update();
ctx.ExecuteQuery();
Delete List Item
string siteUrl = "http://sp2013";
ctx ctx = new ctx(siteUrl);
List oList = ctx.Web.Lists.GetByTitle("MyList");
ListItem oListItem = oList.GetItemById();
oListItem.DeleteObject();
ctx.ExecuteQuery();