Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

2 Answers

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;
    try
    {
        spSite.CatchAccessDeniedException = false;
        res = spSite.SystemAccount.UserToken;
    }
    catch (UnauthorizedAccessException)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite elevatedSPSite = new SPSite(spSite.ID))
                res = elevatedSPSite.SystemAccount.UserToken;
            // (***)         
        });
    }
    finally    
    {         
        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 = "";
                     GetXPathNavigator(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;   
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        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);
        LocalList.Add(localSearchLocation);
        queryManager.UserQuery = UserQuery;
        queryManager.Add(LocalList);
        queryManager.IsTriggered(LocalList);
        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
add comment

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
add comment

Your Answer

 
discard

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.