Creating a List Item via the SharePoint REST API results in Error (500) Internal Server Error

The below is done in PowerShell, but I imagine other languages supports the same features as well.

When utilizing the SharePoint REST API to create a new List Item which contains characters such as æ, ø, or å you might get a response of Error (500) Internal Server Error.

The reponse might also include the following message

Unable to translate bytes [E5] at index 130 from specified code page to Unicode.

If this is the case, use the following to encode the data you post to the REST API

$digestUrl = 'https://[sharepointurl]/_api/contextinfo'
$itemsUrl = "https://[sharepointurl]/_api/web/lists/getByTitle('[listtitle]')/items"

$itemCreationInfo = New-Object -TypeName psobject -Property @{
__metadata = new-object -typename psobject -property @{ 'type' = "SP.Data.[listtitle]ListItem" }
Title = "Item with ÆØÅ"
}

$headers = @{
"Accept" = 'application/json;odata=verbose'
'Content-Type' = 'application/json;odata=verbose'
'If-Match' = '*'
}

$digest = (Invoke-RestMethod -Method POST -Uri $digestUrl -SessionVariable 'session' -UseDefaultCredentials -Headers $headers).d.GetContextWebInformation.FormDigestValue
$headers["X-RequestDigest"] = $digest

$postData = [System.Text.UTF8Encoding]::GetEncoding('UTF-8').GetBytes((ConvertTo-Json $itemCreationInfo))
Invoke-WebRequest -Method POST -Uri $itemsUrl -UseDefaultCredentials -Headers $headers -WebSession $session -Body $postData

Leave a Reply

  • Your email address will not be published.
  • Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: