Sunday, June 12, 2011

How to get the definition of the current selected Term in Navigation?

Based on what user selected from the "Metadata navigation" bar, sometimes we may want to do something extra, such as apply a special filter to a web part, etc.   It is quite easy, because there is a system context object "MetadataNavigationContext", and the property "UniqueNodePath" is the GUID of the selected Term.



Below is the source code used in a web part.

            string strDescription = string.Empty;

            string strTermGuid = MetadataNavigationContext.Current.UniqueNodePath;
            if (string.IsNullOrEmpty(strTermGuid))
                return;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                TaxonomySession objTaxonomySession = new TaxonomySession(SPContext.Current.Site);
                Guid guidTermID = new Guid(strTermGuid);
                Term objTerm = objTaxonomySession.GetTerm(guidTermID);
                strDescription = objTerm.GetDescription();
            });

            Label1.Text = strDescription;

1 comment:

  1. This was exactly what i wasm looking for, however in MS documentation is says that MetadataNavigationContext.UniqueNodePath is reserved for internal use, any ideas on how to do this in a supported way?

    ReplyDelete