SharePoint 2010 ISAPI WCF Service for Search using KeywordQuery not working
-
30-09-2020 - |
Question
Below is my Applied code for Search in ISAPI WCF service, here when I debug DataTable count gives me zero. It works fine in SharePoint 2010 OOTB Search and have also configured Crawl settings. Can you please help me out guys.
try
{
SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));
KeywordQuery keywordQuery = new KeywordQuery(proxy);
keywordQuery.ResultsProvider = SearchProvider.Default;
keywordQuery.ResultTypes = ResultType.RelevantResults;
keywordQuery.TrimDuplicates = true;
keywordQuery.RowLimit = 10;
keywordQuery.QueryText = queryText;
keywordQuery.SortList.Add("LastModifiedTime", Microsoft.Office.Server.Search.Query.SortDirection.Descending);
keywordQuery.HiddenConstraints = string.Concat("scope:\"", "All Sites", "\"");
ResultTableCollection results = keywordQuery.Execute();
ResultTable result = results[ResultType.RelevantResults];
DataTable dt = new DataTable();
dt.Load(result, LoadOption.OverwriteChanges);
DataTable dtFinal = dt.Copy();
}
catch (Exception ex)
{
}
Here is the image of DataTable count
Solution
After some more research I have found out the solution.
You need to write SPSecurity.RunWithElevatedPrivileges(delegate()
, as no user would be found while accessing SharePoint site in this ISAPI wcf service, so RunWithEvelatedPrivileges would help to perform access.
try
{
string siteurl = SPContext.Current.Web.Url;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteurl))
{
KeywordQuery keywordQuery = new KeywordQuery(site);
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange