3

I have the following code:

var requestUri = webAbsUrl + "/_api/web/Lists/getByTitle('Staff Directory')/Items?$\
        filter=Title eq '" + currentUserEmail + "'&$\
        select=*";

It works until it encountered an employee with an apostrophe in the last name. I tried encodeURI() and encodeURIComponent() but neither worked. I tried searching online and can't seem to find a quick solution.

Example Email: John.O'Doe@domain.com

3 Answers 3

6

Use below:

var str = fixedEncodeURIComponent(currentUserEmail);

var requestUri = webAbsUrl + "/_api/web/Lists/getByTitle('Staff Directory')/Items?$\
    filter=Title eq '" + str + "'&$\
    select=*";


function fixedEncodeURIComponent(src) {
return encodeURIComponent(src).replace(/[']/g, function (c) {
        return '%' + c.charCodeAt(0).toString(16) + '%' + c.charCodeAt(0).toString(16);
    });
}

Basically, it duplicates the ' twice, and enconde each ' => %27. As a result, the url will show %27%27 per each '

4
  • 1
    does it mean I have to replace a single apostrophe with double apostrophe? I'm confused about how charCodeAt(0).toString(16) = 27? May 2, 2018 at 19:15
  • 1
    I found the answer about .toString(16) here: May 2, 2018 at 19:25
  • 1
    Thanks mate. %27%27 fixed it
    – Anand
    Mar 10, 2020 at 15:32
  • The slashes are unnecessary, though. /_api/web/Lists/getByTitle('Staff Directory')/Items?$filter=Title eq 'John.O''Doe@domain.com' on a browser bar URL will work, so just doing the apostrophe replace with %27%27 within the email address works.
    – vapcguy
    Nov 3, 2021 at 21:07
5

Deepmala's answer will work, but it seems overly complex and hides both the approach and the actual solution. What you're trying to do is replace a single apostrophe with two, but they both have to be URI-encoded.

A simpler and easier-to-understand solution:

var str = fixedEncodeURIComponent(currentUserEmail);

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getByTitle('Staff Directory')/Items?$filter=Title eq '" + str + "'";

function fixedEncodeURIComponent(src) {
    return encodeURIComponent(src).replace(/[']/g, '%27%27');
}
0
2

Based on @Deepmala, @Dave Read, and this sharepoint article over here.

Here is a method that ensures your query string is going to follow the Sharepoint REST url format, encoding any apostrophe, or any other reserved character:

function encodeRestUrl(query: string) {
    return encodeURIComponent(query.replace(/[%]/g, '%25'))
      .replace(/[']/g, '%27%27')
      .replace(/[&]/g, '%26')
      .replace(/[#]/g, '%23')
      .replace(/[?]/g, '%3F')
      .replace(/[/]/g, '%2F')
      .replace(/[+]/g, '%2B');
  }
12
  • A little more context on how to use this is needed, IMHO...
    – vapcguy
    Nov 3, 2021 at 21:17
  • This method ensures your query doenst have characters that make the rest call to fail, like apostrophes, %, and other reserved characters
    – Alberto S.
    Nov 8, 2021 at 9:05
  • What I mean is context on where this goes, how it is called... i.e. What do I do with this thing?!
    – vapcguy
    Nov 8, 2021 at 17:23
  • 1
    Based on the question that is above, this is pretty self explanatory. This is a JS method that you can call passing the query mentioned in the question, as the input param, so your queries can contain apostrophes or other special characters
    – Alberto S.
    Nov 9, 2021 at 21:35
  • 1
    Ok, I had just never seen a const used this way. Usually a const doesn't change. I would have made it a function, but tow-MAY-tow, tow-MAH-tow, I suppose. Thanks for the explanation, even if a bit snarky.
    – vapcguy
    Nov 10, 2021 at 18:05

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.