最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to include Special characters search like (,),{,} in graph api search - Stack Overflow

programmeradmin5浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

Here 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 query https://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 term example(testsample}.

发布评论

评论列表(0)

  1. 暂无评论