I am using export to excel functionality in my SharePoint web application where, when the user click the export to excel button,the code returns the search results using Keyword query language and export the results into an excel sheet.
The issue that i am facing is that the SharePoint is giving all the results in the excel sheet even though the user is not able to access the results.I want to security trim down my result based on whether the user is able to access the file.
PFB the code that i am using.
using (SPSite site = new SPSite(siteStr))
{
using (SPWeb web = site.OpenWeb())
{
//code to impersonate the site using a normal user id.here rajan8 is a normal user
userToken = web.EnsureUser("domain\\normaluser").UserToken;
//creating spsite instance using normal usertoken.
using (SPSite newspSite = new SPSite(siteStr, userToken))
{
SPServiceContext myContext = SPServiceContext.GetContext(newspSite);
var searchProxy = SearchServiceApplicationProxy.GetProxy(myContext) as SearchServiceApplicationProxy;
KeywordQuery keywordQuery = new KeywordQuery(searchProxy);
keywordQuery.QueryText = "testkeyword";
keywordQuery.RowLimit = 200;
keywordQuery.TrimDuplicates = false;
keywordQuery.StartRow = 0;
SearchExecutor searchExecutor = new SearchExecutor();
ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);
ResultTable resultTable = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults).FirstOrDefault();
}
}
}
here i created the spsite using the user token and perform the search execution,but it again shows more results.