Preset refinements when calling a SharePoint 2013 search results page

In a recent SharePoint 2013 publishing portal project for a customer, we had to use a content query web part to display news on an intranet portal. The news elements are all of the same content type which contains a taxonomy field used to specify the type of news. In the CQWP, the news are grouped by type and the customer wanted a link with «View all the XYZ news» for each group. Considering the many requirements, the way we did it was to link to a search page and to preset the refinement web part to automatically filter on the type of news.

The query string syntax for the refinement is a bit weird since it’s actually a JSON request that is URL encoded but once decoded it looks like this:

/Pages/results.aspx?k=YOUR_SEARCH_TERM#Default={"k":"YOUR_SEARCH_TERM","r":[{"n":"MANAGED_PROPERTY_NAME","t":["string(\"#GUID_FOR_THE_TERM_VALUE\")"],"o":"and","k":false,"m":null}]}

To use this solution, it means we have to get the term’s GUID for each type of news inside the CQWP’s XSLT. The main problem is that the taxonomy field only returns the name of the term, not its GUID.

We could have hardcoded the GUIDs but since our development process include deployment across many development environments, that wasn’t really doable (MMS term’s GUID are generated by SharePoint i.e. different for each environment). After a bit of thinking and searching, what we ended up doing was to access the hidden note field associated with every taxonomy field (something along the lines of «TaxonomyFieldName_0»). When called in a CQWP, the hidden field returns a string in the «term value|term GUID» format. Once we found that out, a simple string manipulation, URL encoding and some basic XSLT gave us the result we wanted. The result is something along the lines of:

<xsl:variable name="newsTypeGuid">
	<xsl:value-of select="substring-after(@TaxonomyFieldName_0,'|')" />
</xsl:variable>

<xsl:variable name="newsTypeUrl">
	<xsl:value-of select="concat('/Pages/results.aspx?k=%2A_YOUR_SEARCH_TERM_#Default=%7B%22k%22%3A%22_YOUR_SEARCH_TERM_%22%2C%22r%22%3A%5B%7B%22n%22%3A%22_MANAGED_PROPERTY_NAME_%22%2C%22t%22%3A%5B%22string(%5C%22%23', $newsTypeGuid, '%5C%22)%22%5D%2C%22o%22%3A%22and%22%2C%22k%22%3Afalse%2C%22m%22%3Anull%7D%5D%7D%0A')" disable-output-escaping='yes'/>
</xsl:variable>

<a href="{$newsTypeUrl}"><xsl:value-of select="@TaxonomyFieldName" /></a>

I really hope it helps / saves some time to someone!

 

* Il est assez rare que je publie des articles en anglais. Vu le caractère plutôt technique du billet courant j’ai choisi de faire cette publication dans la langue de Shakespeare pour simplifier le vocabulaire et afin de rejoindre un maximum de développeurs.

Advertisements

Laisser un commentaire

Entrer les renseignements ci-dessous ou cliquer sur une icône pour ouvrir une session :

Logo WordPress.com

Vous commentez à l’aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l’aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l’aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l’aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s