5

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.

Manuel
  • 10,153
  • 5
  • 41
  • 60

1 Answers1

4

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"
Christophe
  • 27,383
  • 28
  • 97
  • 140
  • Hi guys, i tried the exact thing but i'm still getting the `403: FORBIDDEN` message, from getting the `X-RequestDigest` part. What should i do please? Please share me how you get resolved this. Thanks much! – 夏期劇場 Jul 20 '15 at 10:27
  • "X-RequestDigest": $("#__REQUESTDIGEST").val() - Use sharepoint default masterpage – Vaibhav Jul 22 '15 at 11:51