Delete item from SharePoint List using JavaScript and REST - Stack Overflow

I have some JavaScript (physical file located in SiteAssets library) that should delete an item in a S

I have some JavaScript (physical file located in /SiteAssets library) that should delete an item in a SharePoint list.

We have existing JavaScript code that retrieves data from the list - it looks like this:

(notice that since the JavaScript runs on a PDP in context of the current user, we don't need a specific access token for the request)

var data = $.ajax({
    url: projSiteUrl + "/_api/lists/getbytitle('<listname>')/items,
    type: "GET",
    dataType: "json",
    async: false,
    headers: {
        Accept: "application/json;odata=verbose"  
    }       
});

So I thought that I could write similar code to delete an item from the list again. I read on .aspx#HTTPOps that the REST endpoint of SharePoint supports the "normal" REST verbs, so I wrote this, using the DELETE HTTP verb.

var restUrl = spSiteUrl + '/_api/web/lists/GetByTitle(\'' + listTitle + '\')/items(' + itemId + ')';
jQuery.ajax({
    url: restUrl,
    type: "DELETE",
    headers: {
        Accept: "application/json;odata=verbose"  
    }       
})

But I am getting a 403 (FORBIDDEN) when requesting.

I guess the question is: Am I wrong in assuming that the DELETE verb is supported?

Thanks :-)

I have some JavaScript (physical file located in /SiteAssets library) that should delete an item in a SharePoint list.

We have existing JavaScript code that retrieves data from the list - it looks like this:

(notice that since the JavaScript runs on a PDP in context of the current user, we don't need a specific access token for the request)

var data = $.ajax({
    url: projSiteUrl + "/_api/lists/getbytitle('<listname>')/items,
    type: "GET",
    dataType: "json",
    async: false,
    headers: {
        Accept: "application/json;odata=verbose"  
    }       
});

So I thought that I could write similar code to delete an item from the list again. I read on https://msdn.microsoft./en-us/library/office/jj164022.aspx#HTTPOps that the REST endpoint of SharePoint supports the "normal" REST verbs, so I wrote this, using the DELETE HTTP verb.

var restUrl = spSiteUrl + '/_api/web/lists/GetByTitle(\'' + listTitle + '\')/items(' + itemId + ')';
jQuery.ajax({
    url: restUrl,
    type: "DELETE",
    headers: {
        Accept: "application/json;odata=verbose"  
    }       
})

But I am getting a 403 (FORBIDDEN) when requesting.

I guess the question is: Am I wrong in assuming that the DELETE verb is supported?

Thanks :-)

Share Improve this question asked Feb 4, 2016 at 7:50 Jesper Lund StocholmJesper Lund Stocholm 2,0233 gold badges32 silver badges50 bronze badges 1
  • you can use spservices library. It is really good github./sympmarc/SPServices – Satyaki Chatterjee Commented Feb 4, 2016 at 8:13
Add a ment  | 

1 Answer 1

Reset to default 4

Ok, so apparently I do need the digest when doing modifications - but not for simple data retrieval.

If I change my code to

jQuery.ajax({
    url: restUrl,
    type: "DELETE",
    headers: {
        Accept: "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "IF-MATCH": "*"
    }       
}).

... it works with a simple AJAX request using the REST HTTP verb DELETE :-)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744851880a4597172.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信