【问题标题】:Delete All List Items Sharepoint 2013 REST删除所有列表项 Sharepoint 2013 REST
【发布时间】:2014-04-26 00:27:59
【问题描述】:

我需要使用 REST API 删除 Sharepoint 列表中的所有项目。
我怎样才能做到这一点?
我可以使用以下方法删除单个项目: "/_api/web/lists/getByTitle('MyList')/items('ID')"

我尝试删除 ID,但没有成功。

【问题讨论】:

    标签: rest sharepoint sharepoint-2013 http-delete


    【解决方案1】:

    如果您删除列表并重新创建,则其他 PA 流程将中断。

    只需从 PA 的列表中选择项目 然后在其中添加一个 apply to all:

    _api/Web/Lists/getByTitle('[List]')/('outputs('Get_items')?['body/value']')
    

    【讨论】:

      【解决方案2】:

      你可以试试这个代码。但是您应该知道,这可能是您列表中的例外。使用此代码后,我遇到了列表问题。我删除了所有项目,但我的 ListCount 属性设置为 -3。我建议使用批处理请求来形成和执行请求。会更快更安全

      window.I = 0;
      deleteFunction();
      
      function deleteListItem(listTitle, listItemId, type)
      {
          try
          {
          var listItemUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + listItemId + ")";
          var itemPayload = {'__metadata': {'type': type}};
      
          $.ajax({       
             url: listItemUri,
             type: "POST",   
             contentType: "application/json;odata=verbose",
             headers: { 
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest" : $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "DELETE",
                "If-Match": "*"
             },success :function(){
                 console.log("deleted " + window.I);
                 window.I++; 
                 deleteFunction();
                 },
                 error: function (data) {
                 window.I++; 
                 deleteFunction();
                }
          });
          }
          catch(e)
          {
              console.log("error" + window.I);
              window.I++;
          }
      }
      
      function deleteFunction()
      {
          try
          {
              if(window.I > 1000) return;
              deleteListItem('ListName',window.I,'SP.Data.ListNameListItem');
              console.log("deleted " + window.I);
          }
          catch(e)
          {
              console.log("error" + window.I);
              window.I++;
          }
      } 
      

      【讨论】:

        【解决方案3】:

        你可以试试这个

        function deleteItem(url) {
        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + url,
            type: "DELETE",
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "If-Match": "*"
            },
            success: function (data) {
        
            },
            error: function (error) {
                alert(JSON.stringify(error));
            }
        });
        }
        
        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/items",
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
            },
            success: function (data) {
                var items = data.d.results;
                for(var item in items){
                    var url = "/_api/Web/Lists/getByTitle('MyList')/getItemById(item.ID)"
                    deleteItem(url);
                }
            },
            error: function (error) {
                alert(JSON.stringify(error));
            }
        });
        

        【讨论】:

          【解决方案4】:

          您必须使用上面显示的 URI 对列表中的每个项目进行一次删除调用,并依次传入每个 ID。如果列表中有很多项目,删除然后重新创建列表本身可能会更便宜、更快。

          【讨论】:

          • 好的,所以我应该获取列表中的所有项目并为每个项目调用一个 DELETE。 :( 我想我会去删除列表。谢谢
          猜你喜欢
          • 2014-11-21
          • 1970-01-01
          • 2016-01-08
          • 2017-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多