4

Looking to create a "common tags" pane on a homepage in SP2013 based on metadata but struggling to find a clean efficient way to do so. Found an MSDN guide on getting the term stores using a Taxonomy Session in c#:

    TaxonomySession session = new TaxonomySession(site);

But this doesn't give me numbers of their use. Effectively, when a user clicks on the tag, it would load a search with the metadata term as the search value.

Tag 1 [5] Tag 2 [3] Tag 3 [6]

Has anybody done this previously, or is it a bit of a none starter?

Thanks

1 Answer 1

3

Haven't done this, but the way I would do it is to make a query to search (f.e. using REST) and ask to return tags as refiners (create a refinable managed property, if it's not there yet). Refiners have a RefinementCount property, which gives an approximate number of hits. Creating a link to search for each refinement result (tag) shouldn't be difficult either. This way you only do one search query to get everything that you need, which is really efficient.

An example REST API query, which will return all items in search index:

https://yoursite.sharepoint.com/_api/search/query?querytext='*'&refiners=%27RefinableTags%27

Once you create this refinable managed property RefinableTags you should get all possible used tags and their count in the resultset PrimaryQueryResult/RefinementResults/Refiners, for example:

<d:Refiners m:type="Collection(Microsoft.Office.Server.Search.REST.Refiner)"> <d:element> <d:Entries m:type="Collection(Microsoft.Office.Server.Search.REST.RefinerEntry)"> <d:element> <d:RefinementCount m:type="Edm.Int64">452</d:RefinementCount> <d:RefinementName>Tag1</d:RefinementName> <d:RefinementToken>"ǂǂ5368617265506f696e7420417070"</d:RefinementToken> <d:RefinementValue>Tag1</d:RefinementValue> </d:element> ... </d:Entries> <d:Name>refinabletags</d:Name> </d:element> </d:Refiners>

You can use RefinementCount for the count, RefinementName to display to user and RefinementToken to create a URL to a search page which refines by this tag (i.e. shows all results with that tag). You will need to URL encode this, but for clarity's sake I'll post this unencoded:

https://yoursite.sharepoint.com/search/Pages/results.aspx?k=#Default={"k":"","r":[{"n":"RefinableTags","t":["\"ǂǂ5368617265506f696e7420417070\""],"o":"and","k":false,"m":null}]}

Edit: just now noticed that you might want to open a search page with the taxonomy written in the search box. In that case it's even easier, just construct the search query in k parameter:

https://yoursite.sharepoint.com/search/Pages/results.aspx?k=Tag1 or for more accurate results: https://yoursite.sharepoint.com/search/Pages/results.aspx?k=RefinableTags:Tag1

6
  • 1
    Thanks @Justas such a detailed answer! Really excited to give this a go, will update shortly.
    – Jacob
    Mar 18, 2016 at 9:02
  • had a go at this today, getting RefinementResults m:null. Not sure if this is becuase it needs to do a crawl. Hoping that tomorrow it returns tags. Have implemented as refineablestring00 in the managed properties and mapped it to the columns etc.
    – Jacob
    Mar 18, 2016 at 15:51
  • Yes, it needs a crawl. Please keep me posted, it's really interesting for me :) Mar 18, 2016 at 16:03
  • 1
    Maybe you're mapping the wrong crawled properties to that RefinableString00? There are sometimes several crawled properties for one taxonomy field, so you could try adding different ones, or all of them, into that managed property. You can also request a reindex of site or list to see the results a little bit faster. Also, for debugging purposes, you could check if you are able to query with this managed property to determine if the problem is in the indexing of that property or in the "refineability" of that property. Mar 21, 2016 at 10:08
  • 1
    re-index of list looks to have got it, now getting output as described with <d:element> <d:RefinementCount m:type="Edm.Int64">2</d:RefinementCount> <d:RefinementName>Group D</d:RefinementName> <d:RefinementToken>"ǂǂ47726f75702044"</d:RefinementToken> <d:RefinementValue>Group D</d:RefinementValue> </d:element> Really pleased, think this could be really useful as feature, thanks @Justas!
    – Jacob
    Mar 21, 2016 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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