Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Updating a list item via Invoke-RestMethod

$
0
0

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



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>