SharePoint 2010 Search customized - Return all results, but user gets access denied when clicking on some

תשובה SharePoint 2010 Search customized - Return all results, but user gets access denied when clicking on some

  • יום חמישי 08 מרץ 2012 04:15
     
     

    I have a SharePoint 2010 implementation that has an external facing site.  We are using a custom membership provider and AD  (through an extended site).There are permissions setup for some users tologin.   Everyone else can only see the "anonymous" allowed content.   We want the "anonymous" users to be able to see all search results, but when they click on the document, they receive access denied message. I believe we need to code the  search to run the search part at some elevated permission level and then go back to the users normal permission level.  I am not clear on how to go about this.  Any ideas? Thanks


    KathyTech

כל התגובות

  • יום חמישי 08 מרץ 2012 07:19
     
     

    if sub sites didn't inherit the permissions from it's parent site then the problem comes.

    please check the below link:

    http://sharepoint.stackexchange.com/questions/2131/ssp-indexing-pages-and-causing-access-denied-search-results


    MCTS,MCPD Sharepoint 2010. My Blog- http://sharepoint-journey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful


  • יום חמישי 08 מרץ 2012 12:11
     
     תשובה

    Override the core results web part in your own custom web part

    public class MySearchResults: CoreResultsWebPart

    Inside this part, grab the query the user entered

    QueryManager queryManager = SharedQueryManager.GetInstance(this.Page).QueryManager;
    string kw= queryManager.UserQuery;

    Elevate permissions and run a new query using the KeywordQuery class

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
      using (SPSite siteCollection = new SPSite(SPContext.Current.Site.ID))
      {
         SearchServiceApplicationProxy elevatedProxy = 
         (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(siteCollection));

        KeywordQuery keywordQuery = new KeywordQuery(proxy);
        ...

        keywordQuery.QueryText = kw;

       ResultTableCollection results = keywordQuery.Execute();
       ResultTable result = results[ResultType.RelevantResults];
       DataTable table = new DataTable();
       table.Load(result, LoadOption.OverwriteChanges);

      }
    });

    Create an XML document in the correct format expected by the Core Results web part and return it 

    XDocument resultsDoc = new XDocument(new XDeclaration("1.0","utf-8",null));
    XElement allResultsNode = new XElement("All_Results");
    resultsDoc.Add(allResultsNode);

    for (int i = 0; i < restrictedSites.Rows.Count; i++)
      {
        allResultsNode.Add(
        new XElement("Result",
        new XElement("title", table.Rows[i].ItemArray[FIELD_TITLE].ToString()),
        ...

      }
      allResultsNode.Add(
      new XElement("TotalResults", table.Rows.Count.ToString()),
      new XElement("NumberofResults", table.Rows.Count.ToString()));

    return resultsDoc.ToXmlDocument().CreateNavigator();

    Scot


    Author, Professional Business Connectivity Services
    Author, Inside SharePoint 2010 Blog, www.shillier.com
    Twitter, @ScotHillier
    SharePoint Trainer, Critical Path Training

    • סומן כתשובה על-ידי KathTech יום ראשון 11 מרץ 2012 11:19
    •  
  • יום שני 12 מרץ 2012 19:12
     
     

      I am trying to implement this but I cannot have it working.

      I have placed the code for generating the XML result in the "XPathNavigator GetXPathNavigator(string viewPath)" method, but when I place it on the search reult page it gives me an error: Unable to display this Web Part.

      Can you post the full web-part source code?

      Thanks.

  • יום שני 16 אפריל 2012 08:19
     
      קוד כלול

    Just getting the search results using elevated privileges is as simple as this:

            protected override System.Xml.XPath.XPathNavigator GetXPathNavigator(string viewPath)
            {
                XmlDocument xmlDocument = null;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    QueryManager queryManager = SharedQueryManager.GetInstance(this.Page, this.QueryNumber).QueryManager;
                    xmlDocument = queryManager.GetResults(queryManager[0]);
                });
                XPathNavigator xPathNavigator = xmlDocument.CreateNavigator();
    
                return xPathNavigator;
    
            }

    As for using a replacement KeywordQuery, it works fine, but you need to handle a lot of details, see: http://kjellsj.blogspot.com/2012/04/elevated-search-results-sharepoint-2010.html


    • נערך על-ידי KjellSJ יום שני 16 אפריל 2012 09:25
    •  
  • יום שני 16 אפריל 2012 11:08
     
     

    That's what we ended up doing when it was all said and done.

    Thanks

    Scot


    Author, Professional Business Connectivity Services
    Author, Inside SharePoint 2010 Blog, www.shillier.com
    Twitter, @ScotHillier
    SharePoint Trainer, Critical Path Training