I am at my wits end with this one.
I am unable to modify an list item remotely via a REST request.
Can anyone see what I am doing wrong here?
I am getting at 403 Forbidden response from SharePoint Specifically.
{"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."}}}
Code I am using:
#if($pass = $Host.ui.PromptForCredential("Sharepoint List Load","Enter your credentials","",""))
#{ $cred = New-Object System.Management.Automation.PSCredential ($pass)
#}else{write-host "Cancelling.";exit}
$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"