Friday, January 1, 2010

Client OM,Delete multiple items from Document Library Asynchrously[silverlight]

hi All,
Very happy new year to all of you. first day of 2010 gave me new information while working with client object model asynchrnously, which I want to share with all of you.

Below is the code to delete obect from list, using Client OM ->
private void btn_Delete(object sender, RoutedEventArgs e)
{

foreach (GridData data in dataSource)
{
if (data.IsChecked)
{

if (!(selectedItems.ContainsKey(data.DocID)))
{

selectedItems.Add(data.DocID, data.DocUrl);
}
}
}
if (selectedItems.Count > 0)
{
foreach (var grd in selectedItems)
{
deleteItem(grd.Key, grd.Value);
}

}

else
{
MessageBox.Show("Please select atleast one item");
}
}
private void deleteItem(string itemID, string itemUrl)
{

if (cnt == null)
{
throw new Exception("connection failed");
}
else
{

List olist = cnt.Web.Lists.GetByTitle(listName);
ListItem item = olist.GetItemById(itemID);
item.DeleteObject();
this.Dispatcher.BeginInvoke(delegate()
{
cnt.Load(olist, list => list.Title);

cnt.ExecuteQueryAsync(delete_succeedListner, delete_failureListner);
});
}
}
private void delete_succeedListner(object sender,ClientRequestSucceededEventArgs e)
{

this.Dispatcher.BeginInvoke(delegate()
{
MessageBox.Show("Item deleted");
});

}

this is perfactly fine to delete single item from list. Why I am saying so? just look at code once again, actually item is getting deleted at succeddListner, where nothing otherthan messageBox, which means it will always delete the last item from for loop (where I am caling deleteItem()), it will loose all the previous information and will not work as in synchrouse way. what to do for multiple delete in this case? ideally in succeedLisnter, when one item is deleted. deleteItem() should be invoked again to delete the second item, and so on.check the below code for multiple item delete -
private void btn_Delete(object sender, RoutedEventArgs e)
{

foreach (GridData data in dataSource)
{
if (data.IsChecked)
{

if (!(selectedItems.ContainsKey(data.DocID)))
{

selectedItems.Add(data.DocID, data.DocUrl);
}
}
}
if (selectedItems.Count > 0)
{
string tobeDeleted_key = string.Empty;
string tobeDeleted_value = string.Empty;


tobeDeleted_key = selectedItems.First().Key;
tobeDeleted_value = selectedItems.First().Value;
deleteItem(tobeDeleted_key, tobeDeleted_value);


}

else
{
MessageBox.Show("Please select atleast one item");
}


}
private void deleteItem(string itemID, string itemUrl)
{
cnt = new ClientContext(serverUrl);
if (cnt == null)
{
throw new Exception("connection failed");
}
else
{
MessageBox.Show(itemUrl + " From listName " + listName);
oList = cnt.Web.Lists.GetByTitle(listName);
ListItem item = oList.GetItemById(itemID);
item.DeleteObject();
oList.Update();
this.Dispatcher.BeginInvoke(delegate()
{
cnt.Load(oList, list => list.Title);
cnt.ExecuteQueryAsync(delete_succeedListner, delete_failureListner);
});
}
}
private void delete_succeedListner(object sender,ClientRequestSucceededEventArgs e)
{
string tobeDeleted_key = selectedItems.First().Key;
this.Dispatcher.BeginInvoke(delegate()
{
MessageBox.Show("Item deleted");
selectedItems.Remove(tobeDeleted_key);
if (selectedItems.Count > 0)
{
string tobeDeleted_value = string.Empty;
tobeDeleted_key = selectedItems.First().Key;
tobeDeleted_value = selectedItems.First().Value;
deleteItem(tobeDeleted_key, tobeDeleted_value);
}
else
{
fillDataGrid();
}
});

}

Cheers!!

No comments:

Post a Comment