Encode apostrophes and special characters correctly
Quiz question
How do you protect an apostrophe in an OData filter?
Solution
- A) You encode it with encodeUriComponent()Wrong. encodeUriComponent() belongs in URLs. In an OData filter it breaks the query syntax.
- 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.
- 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.
The expression from the tip
replace(Title,'''','''''')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 Runs without a premium connector, so the license from Microsoft 365 is enough. Source on Microsoft Learn: Power Automate licensing FAQ. For your whole flow: License check.
Share
Short link: powerautomate.ninja/en/146
Source: PowerPlatformTip #146, Microsoft Learn: Reference for functions in workflow expressions.