manish raghu
manish raghu

Reputation: 105

Graph API search with special character in $filter

Below is the code. If I pass search value of search parameter e.g: M'test. It throws error.

What is the right way to use this code to support special character like "'"?

var graphClient = await GetGraphClient();
List<QueryOption> queryOptions = new List<QueryOption>();
queryOptions.Add(new QueryOption("$filter", string.Format("startswith(displayName,'{0}')", search)));
var collection = await graphClient.Data.Request(queryOptions).GetAsync();

Upvotes: 4

Views: 1875

Answers (1)

TheGeneral
TheGeneral

Reputation: 81473

Escaping single quotes

For requests that use single quotes, if any parameter values also contain single quotes, those must be double escaped; otherwise, the request will fail due to invalid syntax.

In your example, M'test would need to be M''test.

You could likely just use string.Replace or regex to replace ' with ''

Upvotes: 8

Related Questions