I'm using REST API to get user profile properties from SP2013 and it is working for most part of users, but when I try to call for users with apostrophe on the username, I'm getting this error:

"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"The expression \\"'domain\\Joe_Doe_O'Brien'\\" is not valid.\"}}}"

On Javascript error object in the return, the status code and status text for this error are:

statusText: Bad Request


This is a snippet code which I am calling SP2013 rest api:

var accountName = "domain\\Joe_Doe_O'Brien";
   var requestHeaders = {
           "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    executor = new SP.RequestExecutor(appweburl);
                  appweburl +
                  "/_api/SP.UserProfiles.PeopleManager/getpropertiesfor(@v)?@v='" + accountName + "'",
              method: "GET",
              contentType: "application/json;odata=verbose",
              headers: requestHeaders,
              success: function (data) {
              error: function (jqxr, errorCode, errorThrown) {

Also I have tried using encodeURIComponent in accountName variable using account name like this:

var accountName = "domain%5CJoe%5FDoe%5FO%27Brien";

But with no success, the same error still has been thrown.

Has SP2013 REST API limitation using some special char at the username to retrieve user properties?

  • Have you tried escaping the apostrophe? – MickB Oct 13 '16 at 13:24
  • No, I just changed apostrophe signal to %27. I have used Amit code and it is working now. – rsmivb Oct 13 '16 at 14:24

Try below code for encoding user name. Below method will replace ' with ''.

function fixedEncodeURIComponent(src) {
return encodeURIComponent(src).replace(/[']/g, function (c) {
return '%' + c.charCodeAt(0).toString(16) + '%' +    c.charCodeAt(0).toString(16);
  • Amit, this code works, but I just did a fix on the first return, instead of encodeURIComponent(src).replace... , I changed to src.replace... Your code was removing \\ between domain and username. – rsmivb Oct 13 '16 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.