I'm trying to documents of a certain content type that has a certain tagging (the documents are spread over several site collection). To get some performance I'm trying to do it with a KeywordQuery
.
The problem I have is that the user executing the code might not have access to all documents, but the check must still be made on all documents. Therefore I have tried to run the query in an elevated mode:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite eSite = new SPSite(siteUrl))
{
KeywordQuery keywordQuery = new KeywordQuery(eSite);
keywordQuery.QueryText = "Some search query";
keywordQuery.TrimDuplicates = false;
SearchExecutor searchExecutor = new SearchExecutor();
ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);
var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
resultTable = resultTables.FirstOrDefault();
}
});
This code returns an error
Operation is not valid due to the current state of the object
on this row KeywordQuery keywordQuery = new KeywordQuery(eSite);
Then I tried opening up the SPSite
with the user token of the system account, like this:
using (SPSite scSite = new SPSite(siteCollection.Url, SPUserToken.SystemAccount))
{
KeywordQuery keywordQuery = new KeywordQuery(scSite);
keywordQuery.QueryText = "Some query"
keywordQuery.TrimDuplicates = false;
SearchExecutor searchExecutor = new SearchExecutor();
ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);
var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
resultTable = resultTables.FirstOrDefault();
}
This throws no error, but returns results only for the logged in user (not the system account).
Is it possible to execute a KeywordQuery
in elevation, or without security trimming, without using some hardcore impersonation?
SPSite
constructor, which receives system account user tokenSPUserToken
from new but existing site collection objectSPSite site = new SPSite("http://sharepoint1");
SPUserToken systoken = site.SystemAccount.UserToken;
SPUserToken.SystemAccount
?