SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have tried following example for my requirement:

Disable search security trimmer?

Search result web part is working fine but the Refinement panel and paging web part stopped working.

Any way to fix this?

share|improve this question
Seems like the user posting that answer also had the same problem, but never wrote back on if he succeeded to solve the problem – Robert Lindgren Apr 29 '13 at 6:26

I have a solution for you ;) ,

you first need to understand that what accounts that youll be running under would effect the security trimming ;) elevating will run under a profile that has selective permissions! what you need is to encapsulate the links code with impersonating a user:

to do this you call follow my code as it already runs under an account for annoymous users! plus i explain in detail between the two, the example is geared towards annoymous users!

SharePoint -access to path is denied

as you can see SPSecurity.RunWithElevatedPrivileges(delegate() is not enough on its own! because of the restrictions for annoymous users and the fact that there is nothing to elevate!

copy my code, within the method that your calling this:

protected override XPathNavigator GetXPathNavigator(string viewPath)

you need to put it within the impersonated SPSite object!

so it will look like this:

/// <summary>
/// get the system token from the list of users to elevate spsite
/// </summary>
/// <param name="spSite"></param>
/// <returns></returns>
protected static SPUserToken GetSystemToken(SPSite spSite) 
    SPUserToken res = null;     
    bool oldCatchAccessDeniedException = spSite.CatchAccessDeniedException;
        spSite.CatchAccessDeniedException = false;
        res = spSite.SystemAccount.UserToken;
    catch (UnauthorizedAccessException)
            using (SPSite elevatedSPSite = new SPSite(spSite.ID))
                res = elevatedSPSite.SystemAccount.UserToken;
            // (***)         
        spSite.CatchAccessDeniedException = oldCatchAccessDeniedException;     
    return res; 

now do the following:

        SPUserToken sysToken = null;
        using (SPSite spSite = new SPSite(SPContext.Current.Site.ID))
            //get the system token from the method GetSystemToken and passing the spSite that you want to run under
            sysToken = GetSystemToken(spSite);

            using (SPSite impersonatedSite = new SPSite(siteUrl, sysToken))
                //we are impersonating the [@"SHAREPOINT\SYSTEM"] account
                using (SPWeb web = impersonatedSite.OpenWeb())
                     //do your code here as the impersonated account like updating a list
                     //in your case you need to call this method:
                     string viewPath = "";

and the method that your calling:

protected override XPathNavigator GetXPathNavigator(string viewPath)
    XmlDocument CustomXml = null;
    string UserQuery = "";
    QueryManager QM = SharedQueryManager.GetInstance(Page, QueryNumber).QueryManager;
    UserQuery = QM.UserQuery;
    QueryManager SourceManager = SharedQueryManager.GetInstance(Page, QueryNumber).QueryManager;   
        SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();
        SearchServiceApplicationProxy searchProxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("Search Service Application");
        QueryManager queryManager = new QueryManager();
        LocationList LocalList = new LocationList();
        Location localSearchLocation = new Location("LocalSearchIndex", searchProxy);
        queryManager.UserQuery = UserQuery;
        CustomXml = queryManager.GetResults(LocalList);
    XPathNavigator xPathNavigator = CustomXml.CreateNavigator(); 
    return xPathNavigator;
share|improve this answer
Thanks ali, for you replay this is relay help full to improve my knowledge. But it doesn't solve my problem which is "Refinement panel and paging web part stopped working..." problem is still there with your posted code. any help ? – Pankaj Apr 29 '13 at 11:03
what error specifically are you getting? have you tried to remove that webpart and add it back in to the page? – ali Sharepoint Apr 29 '13 at 11:48
also the run with elevated privlages within the GetXPathNavigator method doesnt need to be in there! the reason why i have given you the method above is the fact that it will get ridd of those errors and not need to remove security trimming ect as your impersonating not elevating nothing! – ali Sharepoint Apr 29 '13 at 11:51

In order to get the refiners etc. working, use classes that inherit from CoreResultsDatasource and CoreResultsDatasourceView rather than putting the logic in the web part.

The web part needs:

protected override void CreateDataSource()
     DataSource = new CustomResultsDatasource(this);

protected override XPathNavigator GetXPathNavigator(string viewPath)
    // May want more error checking! Take a look at CoreResultsWebPart with ilSpy or Reflector
    CustomResultsDatasource ds = DataSource as CustomResultsDatasource;
    CustomResultsDatasourceView view = ds.GetView() as CustomResultsDatasourceView;
    return view.GetXPathNavigator(null);

CustomResultsDatasource is also really simple:

public class CustomResultsDatasource : CoreResultsDatasource
    public CustomResultsDatasource(CoreResultsWebPart parentWebPart) : base(parentWebPart)
        View = new CustomResultsDatasourceView(this, GetType().Name);

And then your logic goes in CustomResultsDatasourceView:

    public CustomResultsDatasourceView(SearchResultsBaseDatasource dataSourceOwner, string viewName) : base(dataSourceOwner, viewName)
        CustomResultsDatasource datasource = this.DataSourceOwner as CustomResultsDatasource;

        this.QueryManager = SharedQueryManager.GetInstance(datasource.ParentWebpart.Page).QueryManager;

    public override XPathNavigator GetXPathNavigator(DataSourceSelectArguments selectArguments)
         // Code you were trying to put in web part GetXPathNavigator

The demo about 10 minutes into Module 7 of "Advanced developer training for SharePoint 2010" helped.

share|improve this answer

For anyone interested, this is what worked for me:

The queryManager you use should come from the ShareQueryManager, this ensures that the paging and refinement panel still works (and you can still edit properties directly on the derived results web part in the browser).

The only command that needs to be elevated is the queryManager.GetResults(queryManager[0]);

See here for more:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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