0

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

statusCode:400

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);
    executor.executeAsync(
          {
              url:
                  appweburl +
                  "/_api/SP.UserProfiles.PeopleManager/getpropertiesfor(@v)?@v='" + accountName + "'",
              method: "GET",
              contentType: "application/json;odata=verbose",
              headers: requestHeaders,
              success: function (data) {
                  console.log(data);
              },
              error: function (jqxr, errorCode, errorThrown) {
                  console.log(jqxr.responseText);
              }
          }
          );

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?

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

2 Answers 2

1

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);
 });
}
1
  • 1
    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, 2016 at 14:13
0

You can replace the apostrophe in account name with double apostrophes.

Example:

accountname.replace("'","''");

"Joe_Doe_O'Brien".replace("'","''");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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