繁体   English   中英

Powershell:使用 REST API 从 Sharepoint 列表中删除项目 - 远程服务器返回错误:(400) 错误请求

[英]Powershell: Using REST API to Delete Items from a Sharepoint List - The remote server returned an error: (400) Bad Request

我的问题是在 Sharepoint 2013 年到 2019 年的迁移之后。 我们必须从列表中删除项目的脚本已停止工作。 我可以很好地获取列表中所有项目的 id,但第一次删除会返回 400 错误。 我注意到 REST API URL 发生了变化,我做了这些,但不确定我在这里遗漏了什么。 下面的代码片段。

    Function Get-Headers {

    #=======================================================================
    # Function Parameters
    #=======================================================================
    Param()
    Write-Verbose "Executing Function: Get-IdaptiveAuthToken"

    #=======================================================================
    # Set the URI
    #=======================================================================
    $Uri = $SharePointSite + "/_api/contextinfo"

    #=======================================================================
    # Obtain the Request Digest
    #=======================================================================
    Try   { 
        Write-Verbose "Executing Command: `$RequestDigest = Invoke-RestMethod -Uri $Uri -Method POST  -UseDefaultCredentials"
        $RequestDigest = Invoke-RestMethod -Uri $Uri -Method POST -UseDefaultCredentials
    }
    Catch { $ErrorMessage = $_.Exception.Message; Exit-ErrorMsg "Unable to obtain SharePoint Context Request Digest: $ErrorMessage" }

    #=======================================================================
    # Extract the Form Digest Value
    #=======================================================================
    $RequestDigest = $RequestDigest.GetContextWebInformation.FormDigestValue
    Write-Verbose "RequestDigest: $RequestDigest"

    #=======================================================================
    # Set the Headers
    #=======================================================================
    $Headers = New-Object System.Collections.Hashtable
    $Headers.Add("Accept", "application/json; odata=nometadata") # 'nometadata' reduced info passing back & forth vs.'verbose
    $Headers.Add("X-RequestDigest", $RequestDigest)
    $Headers.Add("IF-MATCH",'*')
    $Headers.Add("X-HTTP-Method",'DELETE')

    #=======================================================================
    # Return the Headers
    #=======================================================================
    Write-Verbose "Returning: $Headers"
    Return $Headers

} # END Function Get-Headers    ############################################################################
    #   Function: Delete-SharePointItemsById
    ############################################################################
    Function Delete-SharePointItemsById {

        #=======================================================================
        # Function Parameters
        #=======================================================================
        Param(
            [Parameter(Mandatory = $True)]
            [ValidateNotNullOrEmpty()]
            [String] $ListName,

            [Parameter(Mandatory = $True)]
            [ValidateNotNullOrEmpty()]
            [Array] $ItemIdList,

            [Parameter(Mandatory = $True)]
            [ValidateNotNullOrEmpty()]
            [Hashtable] $Headers
        )
        Write-Verbose "Executing Function: Delete-SharePointItemsById"
        Write-Verbose "Parameter ListName: $ListName"
        Write-Verbose "Parameter ItemIdList: $ItemIdList"
        Write-Verbose "Parameter Headers: $Headers"

        #=======================================================================
        # Cycle through the List and Delete each item
        #=======================================================================
        ForEach ($ItemId in $ItemIdList) {
            $Uri = $SharePointSite + "/_api/web/lists/GetByTitle('" + $ListName + "')/items" + $ItemId 
            Try {
                Write-Verbose "Executing Command: `$Response = Invoke-RestMethod -Uri $Uri -UseDefaultCredentials -Method Post -ContentType `"application/json`" -Headers $Headers"
                Write-Output "  - Deleting SharePoint Item: $ItemId"
                $Response = Invoke-RestMethod -Uri $Uri -UseDefaultCredentials -Method Post -ContentType "application/json" -Headers $Headers
            }
            Catch { $ErrorMessage = $_.Exception.Message; Exit-ErrorMsg "Unable to delete SharePoint List Item [$ItemId]: $ErrorMessage" }

        } # END ForEach ItemId

    } # END Function Delete-SharePointItemsById

输出

* Initializing Headers ...
VERBOSE: Executing Function: Get-IdaptiveAuthToken
VERBOSE: Executing Command: $RequestDigest = Invoke-RestMethod -Uri http://sites.example.com/teams/Cloud_Enablement_Program/_api/contextinfo -Method POST  -UseDefaultCredentials
VERBOSE: POST http://sites.example.com/teams/Cloud_Enablement_Program/_api/contextinfo with 0-byte payload
VERBOSE: received -1-byte response of content type application/xml;charset=utf-8
VERBOSE: RequestDigest: 0x28A751C086DD8EFBE5EB6BF211B461D628EAE2756282F209186DC17930381B736B43E791E5C94369A9BF07E985D334708B55D47DBECA64A62E6D0AA68F9E2422,27 Jan 2020 23:30:03 -0000
VERBOSE: Returning: System.Collections.Hashtable
* Removing Items from Idaptive List [ AWS Idaptive Accounts ] ...
VERBOSE: Executing Function: Get-SharePointIdList
VERBOSE: Parameter ListName: AWS Idaptive Accounts
VERBOSE: Executing Command: $Response = Invoke-RestMethod -Uri http://sites.example.com/teams/Cloud_Enablement_Program/_api/web/lists/GetByTitle('AWS Idaptive Accounts')/items -UseDefaultCredentials
VERBOSE: GET http://sites.example.com/teams/Cloud_Enablement_Program/_api/web/lists/GetByTitle('AWS Idaptive Accounts')/items with 0-byte payload
VERBOSE: received -1-byte response of content type application/atom+xml;type=feed;charset=utf-8
VERBOSE: SharePoint List Id: 5b0ec0be-7238-45ef-b7db-032accbb2677
VERBOSE: SharePoint List Id: 1d980706-df50-4ac2-98ee-a124eef0a5ec
VERBOSE: SharePoint List Id: cc79d83a-4ab0-4ea2-b94d-14df34aae624
....
VERBOSE: Executing Function: Delete-SharePointItemsById
VERBOSE: Parameter ListName: AWS Idaptive Accounts
VERBOSE: Parameter ItemIdList: 5b0ec0be-7238-45ef-b7db-032accbb2677 1d980706-df50-4ac2-98ee-a124eef0a5ec cc79d83a-4ab0-4ea2-b94d-14df34aae624 090aa8f4-dadf-4d3d-80df-5ccd9aa0af90 56c7c14b-136b-4968-ab1a-99db0892bf34 c8fa5d34-990d-4afa-95a7-c937fea1a926 b7736
7d6-84dc-484e-81bd-78a0df6bb7b9 08a6fc2a-e124-4c21-aba1-c802aa0ca4b6 e9c935f8-9f9b-4cb7-bd82-01cd518e0186 1781a50e-e351-4332-9023-48d57ba99d2e 4fae3776-d380-4111-9dd4-c78f2bfe5043 9bf0fb16-0577-44ea-a8a7-ad58cf17db4e 8c73c2f8-9850-4ae3-8b9e-91aefdc12e76 4cd0
d3d8-1ba0-4466-8db8-ea952e3f7b8a 75ab9d7e-4b89-446f-8ab1-c7143351bb21 b8bae64e-d953-413c-9cc5-c1d62c6ad5e2 94369ac1-62ac-4013-bc7e-b6bc880f417f 32823411-4fe0-4664-956a-7c53c3185171 9168d632-c9a6-40ef-ba3f-a240947245b0 992ba0a5-0887-4490-9299-4efe942fd6fb 9fd
23d5a-06a4-4326-9a12-77f6886cac56 90ef4012-ae8f-4abd-b419-6864cda3fe72 a7afcc1c-f6a5-4365-9f29-14b1d5a021b5 5aa3ec79-71c6-424d-8651-e56aa399abf9 0176f1e5-8bbc-4e30-84dd-e8dfbc1faa7e 18285b72-e177-4b4c-8af6-5a8dd249877a 3a4978a4-a698-4f22-91de-2ef7e7a3b8b1 e0
4c69a7-3b80-487f-8cca-88e280db19b6 8b6d5c94-013f-4365-9921-aca17f60bd11 1fa16766-cc25-47f2-aae6-02f85cba2b0e 51a24cca-8432-4b37-b8d6-69e2b209205b d643a6d2-5c0e-49b2-bab0-ea5a29664756 ac9eba02-fe28-4e0c-bcf0-50601c33bdca e9475ab7-a297-45b5-810d-ac2dc1580319 5
59f558c-abe5-46df-bb26-ba67f76fa3fb 06cb8d32-4cf4-4abf-b788-41e4b809b1a1 a5100bd1-ac42-4d29-a2bb-82cb9b9fdbcc e2ba3fa8-52fd-4351-a6bb-86b8e100f862 12cb3e5a-46c4-42d9-adf5-4a3c482b28f1 4ea5cc0e-153d-4428-ab52-383ec0c95453 515e1763-bcf9-4bcb-90e0-d0e877815356 
78198224-2367-4441-b833-7872c6be5036 8c089453-c8d7-4bee-b063-cf983b022ccc e271f351-17ea-4a4f-8a38-1382335b2a2f d86694d4-0cd0-4018-9bde-70c0697de3ba cbc857c7-c92b-4c51-b683-045f376009b6 d1d6c934-7561-4754-b2f3-d7b569a55690 ef1738d4-9e2e-49a1-bcc3-0332384bcf74
 404b3986-6b85-4d09-8384-64ada47c4c15 a5ddd055-a90b-40a0-8852-1056c6e21a9b 29552b5e-d123-41c4-9c22-38e0df5f2719 2a74c4d0-9162-4f33-b4e2-2b420559de96 93f0b98a-736d-4a11-a03b-2af99592ab99 b32886ff-0802-4b43-8e19-72b851c0fbe1 32e505ac-b213-41c5-9bbf-11eb0bc7aad
4 1b2f059b-9691-4c33-a57c-e64a71668c51 d9fe0d64-5113-4a62-9b43-9f8489b66070 95a12522-fec9-4ba0-aad6-4c4c05a90b07 e1ed3ba8-e3a8-4ab3-9a71-400409844334 abf8b4fe-819c-4cc2-ad80-522f2c9de57c 50ab4fe7-9345-49fc-8ef0-dcbc9a8e5029 38b54626-a265-4f2e-ba6c-40b8e909cd
4c a948b488-293c-4727-9b2e-c0756758378b 4cee1c15-e5bc-49c1-bc30-ec68d2b56917 1f5f2b8f-2150-468b-bb15-5b3bbf0a0dc2 cdbf7ce3-5268-4f12-be31-cce195a0f2ed 4000273c-658d-4087-8df4-faff05cca2c5
VERBOSE: Parameter Headers: System.Collections.Hashtable
VERBOSE: Executing Command: $Response = Invoke-RestMethod -Uri http://sites.example.com/teams/Cloud_Enablement_Program/_api/web/lists/GetByTitle('AWS Idaptive Accounts')/items5b0ec0be-7238-45ef-b7db-032accbb2677 -UseDefaultCredentials -Method Post -ContentT
ype "application/json" -Headers System.Collections.Hashtable
  - Deleting SharePoint Item: 5b0ec0be-7238-45ef-b7db-032accbb2677
VERBOSE: POST http://sites.example.com/teams/Cloud_Enablement_Program/_api/web/lists/GetByTitle('AWS Idaptive Accounts')/items5b0ec0be-7238-45ef-b7db-032accbb2677 with 0-byte payload

==============================================================================================================
 ERROR: Unable to delete SharePoint List Item [5b0ec0be-7238-45ef-b7db-032accbb2677]: The remote server returned an error: (400) Bad Request.
==============================================================================================================

我确实相信您的 Post uri 格式错误。 itemsGuid没有分离,这看起来有点奇怪。

POST http://sites.example.com/teams/Cloud_Enablement_Program/_api/web/lists/GetByTitle('AWS Idaptive Accounts')/items5b0ec0be-7238-45ef-b7db-032accbb2677

看起来它需要为($ItemId) 包含括号

https://{site_url}/_api/web/lists/getbytitle('Documents')/items({item_id})

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM