When i try this particular Search term "~!@#$%^&*()_+{}|:"?*-+[];',.` special charter file" using CSOM, Getting all files in result.

Parth Jani 90 Reputation points
2023-05-23T13:55:18.6433333+00:00

I was trying to Search with REST API as given here
https://learn.microsoft.com/en-us/sharepoint/dev/general-development/sharepoint-search-rest-api-overview
-I am doing this using CSOM for on premise Sharepoint and facing two issues:

  1. When i Search using this particular search term : ~!@#$%^&()_+{}|:"?-+[];',.` special charter file , i am getting all the files in sharepoint irrespective of files contains this keyword or not , same i have tried for exact match , tried to enclosed in double quote "" but same result.
  2. This keyword which i have mentioned above doesn't work with index doc id.
    The way given in below link using CSOM.
    https://learn.microsoft.com/en-us/sharepoint/dev/general-development/pagination-for-large-result-sets

Below is the code for kquery and search in CSOM using offset way, code related to my first question

siteQuery = [http://somesharepoint/]-. some sharepoint url
searchTerm = ~!@#$%^&()_+{}|:"?-+[];',.` special charter file
modified date= "" -> say its null for simplicity.


   SearchExecutor searchExecutor = new SearchExecutor(clientContext);
                    KeywordQuery keywordQuery = CreateKQ(string.Format("(Path: {0}) AND {1} {2}", siteQuery, searchTerm, modifiedDateQuery));
                    //keywordQuery.RowsPerPage = 0;
                    keywordQuery.StartRow = startRow;
                    keywordQuery.EnableSorting = true;
                    keywordQuery.SortList.Add("[DocId]", Microsoft.SharePoint.Client.Search.Query.SortDirection.Ascending);
                    ClientResult
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2023-05-24T09:35:19.4966667+00:00

    Hi @Parth Jani

    Do you want to match files with special characters and print them to the console?

    Here is a test for your reference:

    User's image

    Here is the code for your reference:

    using System;
    using System.Linq;
    using System.Security;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Client.Search.Query;
    
    namespace Fctest
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                string siteUrl = "https://yoursite";
                string username = "youruser";
                string password = "yourpasssword";
                string siteQuery = "*";
                string searchTerm = "~!@#$%^&()_+{}|:\"?-+[];',.`docx";
                string modifiedDateQuery = "";
    
                using (var clientContext = new ClientContext(siteUrl))
                {
                    var passWord = new SecureString();
                    foreach (char c in password)
                    {
                        passWord.AppendChar(c);
                    }
                    clientContext.Credentials = new SharePointOnlineCredentials(username, passWord);
    
                    try
                    {
                        var searchExecutor = new SearchExecutor(clientContext);
                        var keywordQuery = CreateKQ(string.Format("(Path:\"{0}\") AND \"{1}\" {2}", siteQuery, searchTerm, modifiedDateQuery), clientContext);
                       
                        keywordQuery.StartRow = 0; // Set the start row index here
                        keywordQuery.EnableSorting = true;
                        keywordQuery.SortList.Add("[DocId]", SortDirection.Ascending);
                        var results = searchExecutor.ExecuteQuery(keywordQuery);
                        clientContext.ExecuteQuery();
                       
                        var resultTableCollection = results.Value;
                        var resultTable = resultTableCollection.FirstOrDefault();
    
                        // Process the search results here
                        if (resultTable != null && resultTable.ResultRows != null)
                        {
                            foreach (var row in resultTable.ResultRows)
                            {
                                Console.WriteLine("Title: " + row["Title"]);
                                Console.WriteLine("URL: " + row["Path"]);
                            }
                        }
                        else
                        {
                            Console.WriteLine("No results found.");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("An error occurred: " + ex.Message);
                    }
                }
    
                Console.ReadKey();
            }
    
            internal static KeywordQuery CreateKQ(string query, ClientContext clientContext)
            {
                var keywordQuery = new KeywordQuery(clientContext)
                {
                    QueryText = query,
                    EnablePhonetic = false,
                    EnableNicknames = false,
                    EnableQueryRules = false,
                    ProcessPersonalFavorites = false,
                    RowLimit = 500,
                    EnableStemming = false,
                    TrimDuplicates = false
                };
                return keywordQuery;
            }
        }
    }
    
    
    
    

    Here is result :

    User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards

    Cheng Feng

    2 people found this answer helpful.
    0 comments No comments

  2. Parth Jani 90 Reputation points
    2023-05-24T10:27:44.26+00:00

    Noo , what i need is to know and understand , why sharepoint is qualifying all records, while i give the term : "~!@#$%^&()_+{}|:"?-+[];',.`"
    User's image

    As shown above , when i search from UI , it gives me all records.
    these records doesn't even have this search term still they are getting qualified.

    Similarly when i try fetch through CSOM it gives me almost 90 records
    User's image

    So my question is why all records are getting qualified ??

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.