Search techniques

  • Orange belt
  • Standard
  • Tip 146

Encode apostrophes and special characters correctly

Quiz question

How do you protect an apostrophe in an OData filter?

Solution

  1. A) You encode it with encodeUriComponent()Wrong. encodeUriComponent() belongs in URLs. In an OData filter it breaks the query syntax.
  2. B) You put a backslash in front of itWrong. A backslash does not escape an apostrophe in OData. The text still ends at that point.
  3. C) You double it with replace()Correct. In OData two apostrophes stand for one apostrophe in the value, and the filter stays valid.

What it is about

Special characters in values break queries and requests because the flow reads them as part of the syntax. The right fix depends on where the value goes. In an OData filter like Title eq 'Tom's Diner' the apostrophe ends the text too early. You double it with replace(), so the query reads 'Tom''s Diner'. For values in a URL you use encodeUriComponent(), which turns spaces, question marks and apostrophes into percent codes. You read encoded values from outside back with decodeUriComponent(). For text in the body of a JSON request you build the object with setProperty() and json(). Then the flow escapes quotes, backslashes and line breaks itself.

When to use it, when not

Use it

When names, titles or free text end up in filters, URLs or JSON requests.

Better not

When the value is only displayed or written to a field and is never part of a query or URL.

Most common mistake

Using encodeUriComponent() in an OData filter. It encodes the syntax as well, and the query fails.

License

Standard This technique uses standard connectors. Microsoft 365 usage rights cover standard connectors. Check the rights for your whole flow. Source on Microsoft Learn: Power Automate licensing FAQ. For your whole flow: License check.

Full tip on powerplatformtip.comPowerPlatformTip #146, with screenshots

Share

Short link: powerautomate.ninja/en/146

Source: PowerPlatformTip #146, Microsoft Learn: Reference for functions in workflow expressions.

Prefer to continue with guidance? The Power Automate live course (in German) at the Power Platform Academy runs over four weeks. The next dates are not set yet, you can join the waiting list on the course page.