web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

API Filter With Characters In Display Name

(0) ShareShare
ReportReport
Posted on by 87

How does one properly escape a query filter for a displayName that contains a special character like a single quote (') or double quote (") or exclamation mark (!)?

I have the same question (0)
  • Suggested answer
    Stefano Demiliani Profile Picture
    37,166 Most Valuable Professional on at

    Single quote escape is %27 and you can escape it in url by using double %27 (so %27%27)

    Double quote can be escaped with %22.

    Exclamation mark should be %21.

  • HPC Keith Profile Picture
    87 on at

    Thank you for the information.  I actually knew the escape character was %27, but did not know that you had to use double.  Why is that?  None of the C# .NET utilities "double-escape" any character.  I manually tried the double-escaped single-quote and verified that it does work, but what does that mean?  Do I have to manually double-escape any special character because Uri.EscapeDataString results in a single-escaping of the single-quote as does System.Net.WebUtility.UrlEncode.  

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,166 Most Valuable Professional on at

    It's actually described in oData docs:

    docs.oasis-open.org/.../odata-v4.01-cs01-part2-url-conventions.html

    For example, one of these rules is that single quotes within string literals are represented as two consecutive single quotes.

  • HPC Keith Profile Picture
    87 on at

    Right, and I understand that, but I can't find anything in C# that escapes these characters (built-in).  Does every ODATA developer manually double-escape characters in strings?

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,166 Most Valuable Professional on at

    You can use:

    YourValue = HttpUtility.UrlEncode(YourValue.Replace("'", "''"));

    Quotes are not OData friendly ;)

  • HPC Keith Profile Picture
    87 on at

    I get it.  I was just hoping for something I wouldn't have to manually replace all of the potential special characters that could exist in a Vendor/Customer/Account name.  I appreciate you walking me through this and I will do something similar to what you've supplied.

  • HPC Keith Profile Picture
    87 on at

    Can you think of any issue with simply replacing special characters with the asterisks (*)?  The reason I ask is that I know that's the wildcard identifier when filtering, but it worked when I needed to search for a vendor with and ampersand (&) in the name, so I tried it for one with the single quotes and it worked fine as well.  Examples below:

    string vendorName1 = "Triple 'C' Ranch";

    string vendorNameEscape = EscapeSpecialCharacters(vendorName1); // gets transformed to "Triple %27%27C%27%27 Ranch" <-- this worked

    string vendorNameWildcard = ReplaceWithWildcard(vendorName2); // gets transformed to "Triple *C* Ranch" <-- this worked


    string vendorName2 = "ADP SCREENING & SELECTION SERVICES";

    string vendorNameEscape2 = EscapeSpecialCharacters(vendorName2); // gets transformed to "ADP SCREENING %26%26 SELECTION SERVICES" <-- does NOT work

    string vendorNameWildcard2 = ReplaceWithWildcard(vendorName2); // gets transformed to "ADP SCREENING * SELECTION SERVICES" <-- this worked

    I can't think of any negative impacts to using this method as it retrieves the record I am looking for and bypasses the method of finding the escaped version of the character.  And, in my tests, the escaped version of the ampersand did not work (any ideas as to why not?).

    I even was able to simple apply Regex to my function to identify and replace the characters and after about 9 tests, it looks as though the query system in BC works just fine with the * character in multiple places of the search string.  My function below:

    public string CleanFilter(string filter)

    {

        return Regex.Replace(filter, "[^a-zA-Z0-9]", "*");

    }

    This uses System.Text.RegularExpressions namespace and this is the constructor I used.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,143

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,694 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,067 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans