0
votes

I have a service on SharePoint 2013 which is responsible for managing custom menu. I'm trying to use this service from PowerShell like this:

$MenuItem = New-Object -TypeName PSObject
$MenuItem | Add-Member -MemberType NoteProperty -Name LinkEN -Value $pageLink
$MenuItem | Add-Member -MemberType NoteProperty -Name LinkFR -Value $pageLink
$MenuItem | Add-Member -MemberType NoteProperty -Name TitleEN -Value $pageParam.ListTitle;
$MenuItem | Add-Member -MemberType NoteProperty -Name TitleFR -Value $pageParam.ListTitle;

$body = @{ _menuItem = $MenuItem; _parent = $null } | ConvertTo-Json
$url = $url + "/_vti_bin/MenuService.svc/AddMenuItem"
$result = Invoke-RestMethod -Uri $url -Method Post -UseDefaultCredentials -ContentType "application/json; charset=utf-8" -Body $body

It works fine up to the moment when I'm trying to send letter with an accent (e.g. è) through parameters (for example: $pageParam.ListTitle = "ètè"). In this situation I'm getting the following error:

Invoke-RestMethod : Request Error The server encountered an error
processing the request. See server logs for more details. At
FixDocumentPages.ps1:145 char:19
+         $result = Invoke-RestMethod -Uri $url -Method Post -UseDefaultCredential ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I have breakpoint in the server code, but it never gets that far.

Below are the SharePoint logs from this incident:

Entering monitored scope (Request(POST: http://wssppoc:5000/_vti_bin/MenuService.svc/AddMenuItem )). Parent No Name=Request(POST:http ://wssppoc:5000/_vti_bin/MenuService.svc/AddMenuItem) 4af4519d-0879-c02e-e1ad-bb2bcbb09abb Non-OAuth request. IsAuthenticated=False, UserIdentityName=, ClaimsCount=0 4af4519d-0879-c02e-e1ad-bb2bcbb09abb Micro Trace Tags: 0 nasq,1 agb9s 4af4519d-0879-c02e-e1ad-bb2bcbb09abb

Leaving Monitored Scope (Request(POST:http ://wssppoc:5000/_vti_bin/MenuService.svc/AddMenuItem)). Execution Time=1,7839 4af4519d-0879-c02e-e1ad-bb2bcbb09abb

Entering monitored scope (Request(POST:http://wssppoc:5000/_vti_bin/MenuService.svc/AddMenuItem)). Parent No Name=Request(POST:http ://wssppoc:5000/_vti_bin/MenuService.svc/AddMenuItem) 4af4519d-087a-c02e-e1ad-b0990e08ad35 Micro Trace Tags: 0 nasq 4af4519d-087a-c02e-e1ad-b0990e08ad35

Leaving Monitored Scope (Request(POST:http://wssppoc:5000/_vti_bin/MenuService.svc/AddMenuItem)). Execution Time=0,6495 4af4519d-087a-c02e-e1ad-b0990e08ad35

Entering monitored scope (Request(POST:http://wssppoc:5000/_vti_bin/MenuService.svc/AddMenuItem)). Parent No Name=Request(POST:http ://wssppoc:5000/_vti_bin/MenuService.svc/AddMenuItem) 4af4519d-087a-c02e-e1ad-bb3e80204d39 Non-OAuth request. IsAuthenticated=True, UserIdentityName=0#.w|test\administrator, ClaimsCount=29 4af4519d-087a-c02e-e1ad-bb3e80204d39 Micro Trace Tags: 0 nasq,1 agb9s 4af4519d-087a-c02e-e1ad-bb3e80204d39

Leaving Monitored Scope (Request(POST:http://wssppoc:5000/_vti_bin/MenuService.svc/AddMenuItem)). Execution Time=3,9189 4af4519d-087a-c02e-e1ad-bb3e80204d39 ContentIntegrationEngine: Execution started, flow identifier is 4659ab80-0016-499b-a3ff-c1961ca797bd.

Does anyone have any idea what could be wrong?

1
I think this: stackoverflow.com/questions/21598398/… is the likeliest solution to your issue.Sam
Thanks, this is the answer. Unfortunately I cannot set Your response as an answer.Jacek

1 Answers

0
votes

Sam answer is leading to the proper solution. Which is using:

[System.Text.Encoding]::UTF8.GetBytes()

Working code is:

$MenuItem = New-Object -TypeName PSObject
$MenuItem | Add-Member -MemberType NoteProperty -Name LinkEN -Value $pageLink
$MenuItem | Add-Member -MemberType NoteProperty -Name LinkFR -Value $pageLink
$MenuItem | Add-Member -MemberType NoteProperty -Name TitleEN -Value $pageParam.ListTitle;
$MenuItem | Add-Member -MemberType NoteProperty -Name TitleFR -Value $pageParam.ListTitle;

$body = @{ _menuItem = $MenuItem; _parent = $null } | ConvertTo-Json
$url = $url + "/_vti_bin/MenuService.svc/AddMenuItem"
$body = [System.Text.Encoding]::UTF8.GetBytes($body);
$result = Invoke-RestMethod -Uri $url -Method Post -UseDefaultCredentials -ContentType "application/json; charset=utf-8" -Body $body