admin管理员组

文章数量:1410717

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.

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.

Share Improve this question asked Nov 30, 2012 at 20:23 ManuelManuel 10.3k5 gold badges44 silver badges61 bronze badges 2
  • 1 Where did you get the snippet from? I am trying to understand the SP.AppContextSite(@target) part. – Christophe Commented Dec 1, 2012 at 1:18
  • That is something i got from a tutorial and has worked in the past, that isn't the problem. – Manuel Commented Dec 3, 2012 at 8:25
Add a ment  | 

1 Answer 1

Reset to default 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"

本文标签: javascriptHow to delete an item using REST for Sharepoint 2013Stack Overflow