2

I am looking for a way to search in content via content search webpart by managed metadata. I need something like {Term.IdWithChildren}, but I need to search in parent terms, instead of child terms.

An example:

- root
-- BranchA
--- Node 1
--- Node 2
--BranchB
--- Node 3

If a user's current navigation node is Node 1 for example, I would like to find all items, which are tagged with Node 1, BranchA or root tag.

1

I found my way arround - I extended ContentBySearchWebPart webpart in a way that it modifies query string before querying sharepoint. It goes recursively by navigation terms from current up to root and per each node, one condition is added. Conditions are added to place in query string marked by string, so the condition can be enabled/disabled from UI.

public class MyContentBySearchWebPart : Microsoft.Office.Server.Search.WebControls.ContentBySearchWebPart
{
    protected override void OnLoad(EventArgs e)
    {
        // Hook replace procedure
        if (this.AppManager != null)
        {
            if (this.AppManager.QueryGroups.ContainsKey(this.QueryGroupName) &&
             this.AppManager.QueryGroups[this.QueryGroupName].DataProvider != null)
            {
                this.AppManager.QueryGroups[this.QueryGroupName].DataProvider.BeforeSerializeToClient += new BeforeSerializeToClientEventHandler(UpdateQueryText);
            }
        }
        base.OnLoad(e);
    }

    private void UpdateQueryText(object sender, BeforeSerializeToClientEventArgs e)
    {
        DataProviderScriptWebPart dataProvider = sender as DataProviderScriptWebPart;

        // replace token in query with navigation Ids
        string currentQueryText = dataProvider.QueryTemplate;
        string token = "FilterMyTag";

        if (currentQueryText.Contains(token))
        {
            dataProvider.QueryTemplate = currentQueryText.Replace(token, BuildQuery());
        }
    }

    private string BuildQuery()
    {
        StringBuilder sb = new StringBuilder();

        // go thru all navigation nodes up to root
        NavigationTerm term = TaxonomyNavigationContext.Current.NavigationTerm;
        while(term != null)
        {
            sb.AppendFormat(" owstaxIdMyFieldWithTag:\"GP0:{0}\"", term.Id);
            term = term.Parent;
        }

        return sb.ToString();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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