Saturday, 15 March 2014

Delete List Item in Sharepoint 2010 using ClientContext

Delete List Item in Sharepoint 2010 using ClientContext
List list;
ListItem item;
string siteUrl = "http://sp2010";
private void ButtonDelete_Click(object sender, System.Windows.RoutedEventArgs e)
{
ClientContext spContext = new ClientContext(siteUrl);
list = spContext.Web.Lists.GetByTitle("Announcements");
spContext.Load(list);
//We have to pass id dynamically
item = list.GetItemById(2);
item.DeleteObject();
spContext.ExecuteQueryAsync(OnDeleteSuccess, OnFailure);
}
private void OnDeleteSuccess(object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Item Deleted");
});
}
private void OnFailure(object sender, ClientRequestFailedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace);
});
}