Using the SharedQueryManager to display Relevant Search Parameters in a WebPart

Using the SharedQueryManager to display Relevant Search Parameters in a WebPart

The SharePoint Search is powerful yet very complex. There are a lot of settings and parameters that influence the search results.

For example; did you know that the Culture used in the query is used to determine wordbreaker, stemmer, and thesaures? Search with the wrong Culture and you will get unreliable and strange search results. I have seen it happen at a customer; the documents were all written in Dutch but the search was configured to use the English Culture (en-US: 1033). Finding out why some documents were included in the search result while others are left out can be a challenge as you have to check every search parameter at WebPart level, Site level, and Service Application level.

So I have written a small WebPart that uses the SharedQueryManager object to display relevant parameters used by the SharePoint Search as a TreeView. This WebPart can be added to a search page as can be seen in the screenshot below.

image

It is quite easy to retrieve an instance of the QueryManager object using the SharedQueryManager:

QueryManager queryMan = SharedQueryManager.GetInstance(this.Page).QueryManager;

To display all members of the QueryManager object I write a recursive function that enumerates the object’s properties using reflection. For all developers, it works a bit like the QuickWatch inVisual Studio.

namespace AlainDeKlerk.SP2010.ViewQuery
{
    [ToolboxItemAttribute(false)]
    public class ViewQueryWebPart : WebPart
    {
        protected override void CreateChildControls()
        {
            QueryManager queryMan = SharedQueryManager.GetInstance(this.Page).QueryManager;

            SPTreeView queryData = new SPTreeView();
            queryData.ExpandDepth = 1;
            TreeNode rootNode = new TreeNode("Search Parameters");
            rootNode.SelectAction = TreeNodeSelectAction.None;

            queryData.Nodes.Add(rootNode);
            CreateTreeNode(queryMan, ref rootNode, 5);

            this.Controls.Add(queryData);
        }

        public void CreateTreeNode(Object o, ref TreeNode node, int maxDepth)
        {
            TreeNode objectNode = new TreeNode(o.GetType().Name);
            objectNode.SelectAction = TreeNodeSelectAction.None;
            objectNode.ToolTip = o.GetType().FullName;
            node.ChildNodes.Add(objectNode);
            if (node.Depth >= maxDepth) return;

            foreach (PropertyInfo prop in o.GetType().GetProperties())
            {
                if (prop.Name.Equals("Item") || prop.PropertyType.IsArray)
                {
                    IEnumerable array = (IEnumerable)Convert.ChangeType(o, o.GetType());
                    if (array != null)
                    {
                        foreach (Object subO in array)
                        {
                            if (prop.PropertyType.IsClass && prop.PropertyType != typeof(String))
                            {
                                CreateTreeNode(subO, ref objectNode, maxDepth);
                            }
                            else
                            {
                                TreeNode newNode = new TreeNode(String.Format("<i>{0}</i>", subO));
                                newNode.ToolTip = subO.GetType().FullName;
                                newNode.SelectAction = TreeNodeSelectAction.None;
                                objectNode.ChildNodes.Add(newNode);
                            }

                        }
                    }
                }
                else
                {
                    try
                    {
                        Object valueO = prop.GetValue(o, null);
                        TreeNode newNode = new TreeNode(String.Format("{0}: <i>{1}</i>", prop.Name, valueO));
                        newNode.ToolTip = prop.PropertyType.FullName;
                        newNode.SelectAction = TreeNodeSelectAction.None;
                        objectNode.ChildNodes.Add(newNode);
                        if (prop.PropertyType.IsClass && prop.PropertyType != typeof(String))
                        {
                            CreateTreeNode(valueO, ref newNode, maxDepth);
                        }
                    }
                    catch (Exception)
                    {

                    }
                }
            }
        }
    }
}

About the author

Alain

You can leave a response, or trackback from your own site.

Leave a Reply