none
Updating a list item via Invoke-RestMethod RRS feed

  • Question

  • I am unable to modify an list item remotely via a REST request via PowerShell.

    Can anyone see what I am doing wrong here?

    I am getting at 403 Forbidden response from SharePoint. 

    {"error":{"code":"-2130575251, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."}}}

    Here is my code:

    $digest = $null
    $sUserName="MYUSERNAMEHERE" 
    $sPassword="MYPASSWORDHERE"
    $sSecPassword=ConvertTo-SecureString $sPassword -AsPlainText -Force        
    $spCredentials = New-Object System.Management.Automation.PSCredential($sUserName, $sSecPassword)
    
    $WebRMethod=[Microsoft.PowerShell.Commands.WebRequestMethod]::POST
    $TargetItemURL="http://someSPhost.domain.com/sites/MYSITENAME/_api/web/lists/GetByTitle('TEST')/items(1)"
    
    $digest = (Invoke-RestMethod -Method post -UseDefaultCredentials -uri "http://someSPhost.domain.com /_api/contextinfo").GetContextWebInformation.FormDigestValue
    
    $header = @{}
    $header.add("Accept",'application/json;odata=verbose')
    $header.add("Content-Type",'application/json;odata=verbose')
    $header.add("X-HTTP-Method",'MERGE')
    $header.add("X-RequestDigest",$digest)
    $header.add("IF-MATCH",'*')
    
    $metadata = @{__metadata = @{type = "SP.Data.TESTListItem"}
                Title = "some new test here"
                }
    
    $JsonMetadata = $metadata | ConvertTo-Json -Compress
    
    Write-host "---------TargetITEM-----------"        
    $TargetItemURL
    Write-host "---------Header-----------"        
    $header
    Write-host "---------Metadata-----------"    
    $metadata
    $metadata.__metadata
    $JsonMetadata
    Write-host "--------------------"        
    
    Invoke-RestMethod -Uri $TargetItemURL -Body $JsonMetadata -Headers $header -Method $WebRMethod -Credential $spCredentials -ContentType "application/json;odata=verbose"
    I am beginning to think this is not possible.


    Eddie Joyner


    • Edited by Edjoyner3 Tuesday, February 26, 2019 3:37 PM added Powershell for reference
    Tuesday, February 26, 2019 3:36 PM

Answers

  • Hi Eddie,

    The following PowerShell script for your reference.

    $user = "domain\admin"
    $pass = "xx"
    $listName = "TEST"
    $itemId=1
    $url = "http://2013/sites/team"
     
    ### TOGGLE DEBUG MODE
    $debug = $false
     
    ### DO NOT MODIFY
    $headers = @{accept = "application/json; odata=verbose"}
    $formDigest = $null
    $secPass = ConvertTo-SecureString $pass -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential ($user, $secPass)
     
    function SetFormDigest() {
        $response = PostRequest ("/_api/contextinfo") $null
        $formDigest = $response.d.GetContextWebInformation.FormDigestValue
        $headers.Add("X-RequestDigest", $formDigest);
        $headers.add("IF-MATCH",'*')
    	$headers.add("X-HTTP-Method",'MERGE')    
        if ($debug) {
            Write-Host "Form Digest: " $formDigest
        }
    }
    function Request ($endpoint, $body, $method) {
        if ($debug) {
            Write-Host "Endpoint: $endpoint, Method: $method, Cred:" $cred.UserName -BackgroundColor Green -ForegroundColor Black
            Write-Host "Header Keys:" $headers.Keys -BackgroundColor DarkGreen -ForegroundColor Gray
            Write-Host "Header Values:" $headers.Values -BackgroundColor DarkGreen -ForegroundColor Gray
            Write-Host "Body:" $body -BackgroundColor DarkGreen -ForegroundColor Gray        
        }
        return Invoke-RestMethod -Uri ($url+$endpoint) -Headers $headers -Method $method -Body $body -Credential $cred -ContentType "application/json;odata=verbose"
    }
    function PostRequest ($endpoint, $body) {
    	 Write-Host "Body1:" $body -BackgroundColor DarkGreen -ForegroundColor Gray
        return Request $endpoint $body ([Microsoft.PowerShell.Commands.WebRequestMethod]::Post)
    }
    function UpdateListItem($listName, $itemId) {
    	SetFormDigest
    	$body = "{ '__metadata': { 'type': 'SP.Data.TESTListItem' }, Title: 'some new test here'}"
        $response = PostRequest ("/_api/web/lists/getbytitle('"+$listName+"')/items("+$itemId+")") $body
    }
    
    ## Script Begin ##
    UpdateListItem $listName $itemId

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Wednesday, February 27, 2019 4:50 AM
    Moderator

All replies

  • Hi Eddie,

    The following PowerShell script for your reference.

    $user = "domain\admin"
    $pass = "xx"
    $listName = "TEST"
    $itemId=1
    $url = "http://2013/sites/team"
     
    ### TOGGLE DEBUG MODE
    $debug = $false
     
    ### DO NOT MODIFY
    $headers = @{accept = "application/json; odata=verbose"}
    $formDigest = $null
    $secPass = ConvertTo-SecureString $pass -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential ($user, $secPass)
     
    function SetFormDigest() {
        $response = PostRequest ("/_api/contextinfo") $null
        $formDigest = $response.d.GetContextWebInformation.FormDigestValue
        $headers.Add("X-RequestDigest", $formDigest);
        $headers.add("IF-MATCH",'*')
    	$headers.add("X-HTTP-Method",'MERGE')    
        if ($debug) {
            Write-Host "Form Digest: " $formDigest
        }
    }
    function Request ($endpoint, $body, $method) {
        if ($debug) {
            Write-Host "Endpoint: $endpoint, Method: $method, Cred:" $cred.UserName -BackgroundColor Green -ForegroundColor Black
            Write-Host "Header Keys:" $headers.Keys -BackgroundColor DarkGreen -ForegroundColor Gray
            Write-Host "Header Values:" $headers.Values -BackgroundColor DarkGreen -ForegroundColor Gray
            Write-Host "Body:" $body -BackgroundColor DarkGreen -ForegroundColor Gray        
        }
        return Invoke-RestMethod -Uri ($url+$endpoint) -Headers $headers -Method $method -Body $body -Credential $cred -ContentType "application/json;odata=verbose"
    }
    function PostRequest ($endpoint, $body) {
    	 Write-Host "Body1:" $body -BackgroundColor DarkGreen -ForegroundColor Gray
        return Request $endpoint $body ([Microsoft.PowerShell.Commands.WebRequestMethod]::Post)
    }
    function UpdateListItem($listName, $itemId) {
    	SetFormDigest
    	$body = "{ '__metadata': { 'type': 'SP.Data.TESTListItem' }, Title: 'some new test here'}"
        $response = PostRequest ("/_api/web/lists/getbytitle('"+$listName+"')/items("+$itemId+")") $body
    }
    
    ## Script Begin ##
    UpdateListItem $listName $itemId

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Wednesday, February 27, 2019 4:50 AM
    Moderator
  • Denis, you are awesome.  Thanks so much

    Eddie Joyner

    Wednesday, February 27, 2019 1:52 PM
  • I would love to know what I was doing wrong in my script. 

    Eddie Joyner

    Wednesday, February 27, 2019 5:15 PM
  • Hi,

    Please replace UseDefaultCredentials with $spCredentials in you code to check if it works.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, February 28, 2019 1:14 AM
    Moderator