简体   繁体   English

使用JavaScript和REST从SharePoint列表中删除项目

[英]Delete item from SharePoint List using JavaScript and REST

I have some JavaScript (physical file located in /SiteAssets library) that should delete an item in a SharePoint list. 我有一些JavaScript(位于/ SiteAssets库中的物理文件),应该删除SharePoint列表中的项目。

We have existing JavaScript code that retrieves data from the list - it looks like this: 我们现有的JavaScript代码可从列表中检索数据-看起来像这样:

(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) (请注意,由于JavaScript在当前用户的上下文中在PDP上运行,因此我们不需要该请求的特定访问令牌)

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.com/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. 我在https://msdn.microsoft.com/zh-cn/library/office/jj164022.aspx#HTTPOps上阅读到,SharePoint的REST端点支持“正常” REST动词,因此我使用DELETE HTTP动词编写了此代码。

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. 但是请求时我得到了403 (FORBIDDEN)

I guess the question is: Am I wrong in assuming that the DELETE verb is supported? 我猜问题是:假设支持DELETE动词是否对吗?

Thanks :-) 谢谢 :-)

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 :-) ...它与使用REST HTTP动词DELETE的简单AJAX请求一起使用:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用REST for Sharepoint 2013删除项目 - How to delete an item using REST for Sharepoint 2013 创建列表项 Sharepoint REST API Javascript 返回错误 500 - Creating List Item Sharepoint REST API Javascript returns Error 500 无法使用 Sharepoint 2016 中的 REST API 更新列表中的列表项 - Unable to update the list item in the list using REST API in Sharepoint 2016 如何使用香草 JavaScript 从列表中删除项目? - How to delete an item from a list using vanilla JavaScript? 如何使用javascript读取sharePoint列表项值(当前项) - How to read sharePoint List item value (current Item) using javascript 使用添加为WebPart的Javascript代码段从SharePoint列表中检索列表项数据 - Retrieve list item data from a SharePoint list using Javascript snippet added as WebPart 如何从 JavaScript 的列表中删除新项目? - How to delete a new item from the list in JavaScript? 如何使用JavaScript从多个网站集中获取SharePoint列表项 - How to get the SharePoint list item from multiple site collections using JavaScript Sharepoint - 使用 javascript 添加带有附件文件的列表项 - Sharepoint - Add list item with attachment file, using javascript 使用Javascript从嵌套数组中删除一个项目 - Delete an item from nested array using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM