How to: Get related terms for a SharePoint 2013 Hashtag

Posted Wednesday, November 13, 2013 10:48 AM by CoreyRoth

If you have been following my posts lately, you might have noticed I have been dissecting the inner workings of the Hashtag Profile page (HashTagProfile.aspx).  I started with using search to query hashtag data and then yesterday I posted on how to query for trending tags.  The last component of this page is how to get the related terms of the hashtag.  As a reminder of what this does, it allows you to associate terms in the term store with a hashtag.  Whenever that term is used, it gets associated with the hashtag.  You can see the related tags in the screenshot below.

RelatedTagsProfilePage

Figuring this out took a bit of digging.  Remember that hashtags are nothing but terms themselves.  Ultimately what I discovered is that each term in the term store has the ability to add associations.  Unfortunately, the method to access these associations is marked internal meaning we can’t access it.  Luckily, I found that this really just gets stored in a custom property of the term.  Let’s take a look at what you need to do.

For simplicity, I used the server-side object model using a console application.  It should be possible to do this with JavaScript too though.  I suspect you can access the same information using SP.Taxonomy.Term.localCustomProperties.

Start by adding references for the following:

  • Microsoft.SharePoint.dll
  • Microsoft.SharePoint.Taxonomy.dll

In our class itself, we’ll need the following namespaces.

using Microsoft.SharePoint;

using Microsoft.SharePoint.Taxonomy;

I am going to get access to the term store in my case using an SPSite object for any given site collection on the site.

using (SPSite siteCollection = new SPSite("http://server/sitecollection"))

Inside, the using block, I get a TaxonomySession and TermStore object.

TaxonomySession taxonomySession = new TaxonomySession(siteCollection);

TermStore termStore = taxonomySession.DefaultKeywordsTermStore;

Now we need to get access to the term in the hashtag.  You can get this in a variety of ways but I am simply going to use the GUID which I took from the URL of the HashTagProfile.aspx page. Otherwise, you can access a Group object first and then find the term you are looking for that way.

RelatedTagsProfilePageUrl

Term

term = termStore.GetTerm(new Guid("cb9308fe-a187-4a3d-9e6a-657b06906f89")); To access the associated terms, we need to look in the CustomProperties collection with a value of _Sys_HashAssoc

string associatedTerms;

term.CustomProperties.TryGetValue("_Sys_HashAssoc", out associatedTerms);

This gives us a delimited string with the GUID of each associated term and the owner.  Here is what it looks like.

"973bdb1b-eb9f-4f8b-8512-6440b0913b4f||0#.w|sharepoint\\sp_setup||4c1ad4aa-77f6-40de-bd16-210cad44d0b3||0#.w|sharepoint\\sp_setup"

The delimiter to use is “||”.  Using string.Split() we can get the GUIDs we need and then look up the names of the terms.

string[] termGuidArray = associatedTerms.Split(new string[1] {"||"}, StringSplitOptions.None);

We are only interested in the terms, so we will loop through them with an increment of 2.

for (int i = 0; i < termGuidArray.Length; i += 2)

{

    Term associatedTerm = termStore.GetTerm(new Guid(termGuidArray[i]));

    Console.WriteLine("{0} ({1})", associatedTerm.Name, termGuidArray[i]);

}

Here we just use the GetTerm() method using the GUID we have parsed to print out the name of the term.

When the code executes, it looks like this.

RelatedTagsConsoleApplication

Here is what the entire code snippet looks like together.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

using Microsoft.SharePoint;

using Microsoft.SharePoint.Taxonomy;

 

namespace TaxonomyConsoleApplication

{

    class Program

    {

        static void Main(string[] args)

        {

            using (SPSite siteCollection = new SPSite("http://sp2010/sites/test"))

            {

                TaxonomySession taxonomySession = new TaxonomySession(siteCollection);

                TermStore termStore = taxonomySession.DefaultKeywordsTermStore;

                Term term = termStore.GetTerm(new Guid("cb9308fe-a187-4a3d-9e6a-657b06906f89"));

                string associatedTerms;

                term.CustomProperties.TryGetValue("_Sys_HashAssoc", out associatedTerms);

 

                string[] termGuidArray = associatedTerms.Split(new string[1] {"||"}, StringSplitOptions.None);

 

                for (int i = 0; i < termGuidArray.Length; i += 2)

                {

                    Term associatedTerm = termStore.GetTerm(new Guid(termGuidArray[i]));

                    Console.WriteLine("{0} ({1})", associatedTerm.Name, termGuidArray[i]);

                }

            }

 

            Console.ReadLine();

        }

    }

}

If you have been wanting to customize this functionality, I hope that you find today’s example useful.

Comments

No Comments

Leave a Comment

(required)
(required)
(optional)
(required)