
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:
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 :
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