Thursday, February 20, 2014

Modifying Results Post Query in SharePoint 2010 CoreResults WebPart


One of the scenarios we encountered was to enhance the search results post Query


Fortunately Core Results WebPart has a provision to do so in its GetXPathNavigator method

Sorry for Pasting code as is ...Will post the formatted code snippet soon.. 


        protected override XPathNavigator GetXPathNavigator(string viewPath)
        {
           
            XmlDocument xDocument = new XmlDocument();
           

            try
            {
                QueryManager queryManager = SharedQueryManager.GetInstance(Page).QueryManager;
                xDocument = queryManager.GetResults(queryManager[0]);

                //Annotation Request
               
                CustomEnhancemer customEnhancerObject = MakeCalltoWCF.EnhancemyResults(xDocument);
                XmlNode xmlNodeSettings = xDocument.SelectSingleNode("All_Results/Result");

                XmlElement xmlNodeCustomSettings = xDocument.CreateElement(customEnhancerObject.Title);

                xmlNodeCustomSettings.SetAttribute(customEnhancerObject.Key1, customEnhancerObject.Value1);
                xmlNodeCustomSettings.InnerText = customEnhancerObject.InnerText;

                xmlNodeSettings.AppendChild(xmlNodeCustomSettings);
                //xDocument.Save()
                Trace.WriteLine(" - XmLDocument: " + xDocument.OuterXml);
                return xDocument.CreateNavigator();

            }

            catch (Exception ex)
            {
                Trace.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + " -In Xpath Exception: " + ex.Message);
            }

            finally
            {
                CloseTraceFile();
            }

            return xDocument.CreateNavigator();
        
        }

No comments: