David
David

Reputation: 25

Posting metadata to sharepoint 2016 onpremise : System.Text.DecoderFallbackException with special characters

I try to create an item with metadatas in a SharePoint list. One of these metata contains a french "ç" character which makes sharepoint responding with a 500 error, with: code=-1, System.Text.DecoderFallbackException value=Unable to translate Unicode character [E7] at index 147 to specified code page.

using (var client = new SpWebClient("https://intranet/Gestion") { Credentials = CurrentNetworkCredential })
            {
                var payload = new JObject
                {
                    ["__metadata"] = new JObject { ["type"] = "SP.Data.ListeCommentaireGerantListItem" },
                    ["Language"] = "Français (France)"
                };

                var requestUrl = $"/_api/web/lists/GetByTitle('{SHAREPOINT_LIST}')/items";
                Dictionary<string, string> updateHeaders = null;

                var r = client.ExecuteJsonWithDigest(requestUrl, "POST", updateHeaders, payload);
                return Redirect("/");
            }

spWebClient is defined here, basically it adds all needed headers and simplify auth in a webclient class.

i tried to add "application/json; odata=verbose; charset=utf-8" in the header, but same error.

Any help?

Upvotes: 1

Views: 248

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

Try to explicitly set encoding for posting and getting strings to UTF8 via WebClient.Encoding Property:

client.Encoding = System.Text.Encoding.UTF8;

Example

using (var client = new SPWebClient("https://contoso.sharepoint.com") { Credentials = credentials, Encoding = System.Text.Encoding.UTF8 })
{
    //the remaining code goes here...
}

Upvotes: 0

Related Questions