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.

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

2 Answers 2

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...

1
  • 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. Commented Mar 20, 2020 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.