3

I'm trying to delete an Item from a list using the following code:

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
class DeleteListItem
{
    static void Main()
    {   
        string siteUrl = "http://MyServer/sites/MySiteCollection";

        ClientContext clientContext = new ClientContext(siteUrl);
        SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
        ListItem oListItem = oList.GetItemById(2);

        oListItem.DeleteObject();

        clientContext.ExecuteQuery(); 
    }
}

}

for any item Id I try to delete, I always get the same message :

Item does not exist. It may have been deleted by another user.

  • 1
    are you sure that you have access to the item? – Alexander Mar 16 '12 at 14:51
  • With the same code trying to add or get a item I can access to. – hello B Mar 16 '12 at 16:40
  • Your code works perfectly for me. There must be a simple mismatch some place. – Robert Kaucher Mar 16 '12 at 19:23
  • Just something I noticed: Why have you declared Microsoft.SharePoint.Client twice in the usings section? – Vardhaman Deshpande Jul 31 '12 at 13:08
4

Try something like:

string siteUrl = "http://MyServer/sites/MySiteCollection";

ClientContext clientContext = new ClientContext(siteUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

clientContext.Load(oList);
clientContext.ExecuteQuery()

ListItem oListItem = oList.GetItemById(2);

clientContext.Load(oListItem);
clientContext.ExecuteQuery();

oListItem.DeleteObject();

clientContext.ExecuteQuery();   

This worked for me...

| improve this answer | |
  • Even if the question and answer are old... there is no need to load list and item before the delete, the code in the question is correct. – Emaborsa Mar 20 at 20:55
1

The important thing is to NOT call Load in the same "ExecuteQuery-Cycle" as you call DeleteObject (or Recycle). You have to split the operation in two and call ExecuteQuery twice:

var item = customList.GetItemById(24);
ctx.Load(item);
ctx.ExecuteQuery(); //Perform Webservice Call to Get Item

item.Recycle();
ctx.ExecuteQuery(); //Perform Webservice Call to Delete Item
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.