Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an item using REST for Sharepoint 2013

I'm creating a Sharepoint App and i am restricted to using Javascript (jQuery included) and REST endpoints. I would like to delete an item from the host using the web app, but i'm getting an error (403: FORBIDDEN). This is the code i have so far:

executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
    url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + currentListTitle + "')/items(" + result.Id + ")/?@target='" + hostweburl + "'",
    method: "POST",
    headers: {
               "accept": "application/json",
               "X-RequestDigest": ?????
               "IF-MATCH": "*",
               "X-HTTP-Method": "DELETE"
             },
    success: onDeleteItemSuccess,
    error: onDeleteItemFail
});

Now I found out this X-RequestDigest is mandatory and i found some call to get this from REST:

$.ajax({
    url: appweburl + "/_api/SP.AppContextSite(@target)/contextinfo/?@target='" + hostweburl + "'",
    type: "POST",
    contentType: "application/x-www-url-encoded",
    dataType: "json",
    success: function (data) {
        if (data.d)
        {
            digestValue = data.d.GetContextWebInformation.FormDigestValue;
            alert(digestValue);
        }
    },
    error: function (xhr) {
        alert(xhr.status + ': ' + xhr.statusText);
    }
});

But it isn't working at all (this might be because this code was for Sharepoint 2010) and it will keep giving me a 403: FORBIDDEN message.

Does anyone know how to delete a list item from one of the lists using REST (I can't use/edit any code outside of the javascript!)?

Any help is appriciated and if you need any information please don't hesitate to ask.

like image 732
Manuel Avatar asked Oct 06 '22 19:10

Manuel


1 Answers

The code can't be for SharePoint 2010, as _api is new to SP 2013.

[Update] Maybe you mean that your code was working in SP 2013 preview? In SP2013 RTM you need to use:

"Accept": "application/json; odata=verbose"
like image 136
Christophe Avatar answered Oct 10 '22 02:10

Christophe