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.

  • 2
    Are you using the Farm or Administrator account as your Crawl Account? Commented May 17, 2017 at 13:01
  • yes, i am using the sharepoint farm account as my crawl account.
    – Paul
    Commented May 23, 2017 at 10:30

2 Answers 2


You need to use a crawl account that has only READ permissions. When you are using an account that is an administrator you will end up with items in the index that should not appear there, for example draft versions will show as well.

In the search service application, change the default crawl account to a new domain account for just this purpose. SharePoint will add the account to the web application policy with Full Read for all web apps in the Local SharePoint sites content source.

For your People Crawl, the User Profile Service, you first need to grant the new crawl account Retrieve People Data for Search Crawlers on the User Profile Service Application. Then you can run a full crawl on the sps3://mysitehost or sps3s://mysitehost depending on your mysite host protocol.

At this point you can either perform a Full Crawl of all content sources, or (and this is more drastic) reset the index first, then perform a full crawl.

  • Thanks for the inputs Matthew. In our application we need all the items to be crawled.Because when i logged in,since i am admin ,i wanted all results to be showed. As per my requirement i need security trimming of search result should happen based on current user context
    – Paul
    Commented May 25, 2017 at 19:22
  • Right, you need to set the crawl account with full READ. That is how SharePoint works, trust me. It will crawl all items. Commented May 26, 2017 at 12:01
  • I saw initial post by Paul, what i can understand is when user with full access searching in sharepoint application and export the results using keyword search query then user gets the exact number of search count displayed in the search web part. But when a normal user exports the results - the exported result count is more the count displayed on the search result web part, which keyword search gets all the search results even if user doesn't have access to those items but in the suggestion provided by you will index everything to search database so this will not help to resolve this issue
    – Bhaskar
    Commented May 26, 2017 at 12:48
  • What do you mean "Export the results"? Why are you talking about the "Search Database"? Commented May 26, 2017 at 13:35

Try impersonating the keyword search query with the normal user while getting the results from search. This may help.

  • Thanks satya for the inputs.I am trying for a impersonated code block where i can include my search code.I will post if i get any luck.
    – Paul
    Commented May 25, 2017 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.