Add update and delete list items programmatically in sharepoint-client object model
Add update and delete list items programmatically in sharepoint-client object model:
Insert the following namespace
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
Use the following code.
ClientContext ccsite;
List listobj;
ListItemCollection _icoll;
public MainPage()
{
InitializeComponent();
ccsite = new ClientContext(ApplicationContext.Current.Url);
ccsite.Load(ccsite.Web);
listobj = ccsite.Web.Lists.GetByTitle("ListName");
ccsite.Load(listobj);
CamlQuery qry = new CamlQuery();
qry.ViewXml = "<View/>";
_icoll = listobj.GetItems(qry);
ccsite.Load(_icoll);
ccsite.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(success), null);
}
private void success(object sender,ClientRequestSucceededEventArgs arg)
{
Dispatcher.BeginInvoke(datacon);
}
private void datacon()
{
//For insert new data
ListItem litem = listobj.AddItem(new ListItemCreationInformation());
litem["ColumnName1"] = "One";
litem["ColumnName2"] = "Two";
litem.Update();
ccsite.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(succed), null);
//For Update list data
foreach (ListItem item in _icoll)
{
if (Convert.ToString(item["Likes"]) == "88")
{
int id = Convert.ToInt16(item["ID"]);
ListItem litem = listobj.GetItemById(id);
litem["ColumnName1"] = "Yourdata";
litem.Update();
ccsite.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(succed), null);
}
}
//For Delete List item
foreach (ListItem item in _icoll)
{
if (Convert.ToString(item["ColumnName1"]) == "YourData")
{
int id = Convert.ToInt16(item["ID"]);
ListItem litem = listobj.GetItemById(id);
litem.DeleteObject();
ccsite.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(succed), null);
}
}
}
private void succed(object sender, ClientRequestSucceededEventArgs arg)
{ }
Comments
Post a Comment