I am using graph api - for content search in the documents of sharepoint. My search request is -
string searchQuery = this.GetContentSearchQuery(request);
var searchRequest = new
{
requests = new[]
{
new
{
entityTypes = new[] { "listItem" },
query = new
{
query_string = new
{
query = searchQuery,
},
},
from = 0,
size = 25,
fields = new[] { "ID" },
},
},
};
I tried by encoding the characters but it did not work.
private string GetContentSearchQuery(ContentSearchDTO request)
{
StringBuilder queryString = new();
if (!string.IsNullOrEmpty(request.ClientId))
{
// I am appending all the properties needed for the request and then later sending the search text to the body.
string escapedSearchText = this.EscapeSpecialCharacters(request.SearchText);
queryString.Append($" AND body:{escapedSearchText}");
}
return this.EscapeSpecialCharacters(queryString.ToString());
}
below is the EscapeSpecialCharacters method for encoding
private string EscapeSpecialCharacters(string input)
{
StringBuilder escapedString = new StringBuilder();
foreach (char c in input)
{
switch (c)
{
case '(':
case ')':
case '{':
case '}':
escapedString.Append(Uri.EscapeDataString(c.ToString()));
break;
default:
escapedString.Append(c);
break;
}
}
return escapedString.ToString();
}
I am using graph api - https://graph.microsoft/beta/search/query for content search in the documents of sharepoint. My search request is -
string searchQuery = this.GetContentSearchQuery(request);
var searchRequest = new
{
requests = new[]
{
new
{
entityTypes = new[] { "listItem" },
query = new
{
query_string = new
{
query = searchQuery,
},
},
from = 0,
size = 25,
fields = new[] { "ID" },
},
},
};
I tried by encoding the characters but it did not work.
private string GetContentSearchQuery(ContentSearchDTO request)
{
StringBuilder queryString = new();
if (!string.IsNullOrEmpty(request.ClientId))
{
// I am appending all the properties needed for the request and then later sending the search text to the body.
string escapedSearchText = this.EscapeSpecialCharacters(request.SearchText);
queryString.Append($" AND body:{escapedSearchText}");
}
return this.EscapeSpecialCharacters(queryString.ToString());
}
below is the EscapeSpecialCharacters method for encoding
private string EscapeSpecialCharacters(string input)
{
StringBuilder escapedString = new StringBuilder();
foreach (char c in input)
{
switch (c)
{
case '(':
case ')':
case '{':
case '}':
escapedString.Append(Uri.EscapeDataString(c.ToString()));
break;
default:
escapedString.Append(c);
break;
}
}
return escapedString.ToString();
}
Share
Improve this question
edited Mar 28 at 11:21
Aria
asked Mar 28 at 11:15
AriaAria
12 bronze badges
1 Answer
Reset to default 0Here are the URL-encoded representations of the special characters you mentioned:
(
becomes%28
)
becomes%29
{
becomes%7B
}
becomes%7D
An example in the documentation can be found here- https://learn.microsoft/en-us/graph/query-parameters?tabs=http#encoding-query-parameters This explains the ( and ) .
Another example is this queryhttps://graph.microsoft/v1.0/me/messages?$search="subject:example%28test%29%7Bsample%7D"
where the search query is looking for messages with a subject that includes the termexample(testsample}
.