LibrarySites.Banner

Content Tagging with Sitecore 7

Sitecore 7 has added some new features to change the way we structure and classify content. In this post we'll dive into tags and content tagging.

The Sitecore content tree provides a hierarchical structure to create and maintain our content. But often the content we're working with doesn't fall into a strict hierarchy. Item Buckets have changed the way in which we structure and design our Sitecore sites allowing users to manage large numbers of items in an unstructured manner.

But just because content is unstructured doesn't mean it's not related in some way; often in a more complex relationship than hierarchical. Consider how one might locate a piece of content inside an Item Bucket. You could use textual search, but you might not know what you're looking for.

To allow relationships and taxonomy to be applied to items within a bucket (or even outside a bucket) we can use tags. Tags are a single taxonomy dimension which could represent anything of interest. Consider a product catalog where products can be tagged in numerous ways. A coffee machine could be tagged as both "Coffee" and "Beverage" while a coffee grinder could be tagged as both "Coffee" and "Accessory" and a juicer could be tagged as "Beverage" and "Health" (juice is healthy right?). There are overlapping classifications with these products which doesn't lead to a strict hierarchy of classification. Now one can easily find all products that are coffee products or all products that are accessories by searching by the tag. A non-rigid structure has been applied to the items that can overlap with other classifications.

Sitecore 7+ comes with a "Semantics" field (named __semantics) to provide a general tagging facility to users. This field is only used in the Sitecore UI and is not used by any other part of Sitecore, so feel free to use it as you see fit in your solutions.

On the Right Version

Sitecore 7.2 has just been released and includes a number of improvements to Item Buckets. If you're going to make use of the examples below make sure you're running Sitecore version 7.2 or higher.

Tags in Action

Let's take a look at tags in action. I have set up a demo product catalog inside an Item Bucket.

  

The products in the catalog can be categorized a number of different ways, so this is a good case for tagging. I'll start by clearing out the Test Tag sample tag in the default Sitecore tag repository located at /sitecore/system/Settings/Buckets/TagRepository, then I'll populate my product specific tags into the repository. Now I can tag my products with the relevant tags by simply selecting the tags in each products __semantics field.

Once all my products are tagged I can easily find all products with a specific tag by using the built in Search by Tag filter in the Search UI of my products Item Bucket.

  

Once I select the Search by Tag filter, as I start to type Sitecore will run an autocomplete on the name of the tag I'm typing. To use the tag I just have to click the tag in the autocomplete dropdown. As a shortcut to clicking through the UI and selecting the tag filter I could also just manually type tag: in the search box.

  

Note that you cannot just enter text for the tag, you must click the entry in the dropdown as the tag is stored by ID, not by name, and clicking the tag will ensure the Sitecore UI uses the ID for the search.

Multiple Tag Repositories

Most real world Sitecore solutions will likely require different sets of tags depending on the type of content. Take the above product catalog scenario as an example. What if we'd now like to start tagging images and videos in the media library? The set of tags used to categorize products would probably be different to those required to properly categorize images. Images would require tags such as banner or grayscale that would have no meaning for a product.

To properly manage the different sets of tags for different kinds of content we will need to create our own tag repository. This will involve both the creation of the tag repository folder and adding additional fields to the relevant templates to allow specifying differing search start locations.

To create the new repository we will move the existing tag repository to a folder then duplicate the existing tag repository and rename it. To ensure the individual tag repository folders don't get bucketed we'll also need to unbucket the TagRepository item.

  

We now need to make updates to our templates to use the appropriate tag repository and add the new tag fields. We cannot make use of the existing __semantics field as the field source needs to be set differently on the relevant templates.

Add a new field named tags to the templates in question of field type Multilist with Search and set the field source using the following format, substituting the ID to the ID of the tag repository in the content tree:

Filter=_path:154d56cc0de243c7bbc0a25bd7ffd901|-_group:154d56cc0de243c7bbc0a25bd7ffd901

The above configuration sets the search location to the tag repository and excludes the repository folder itself.

The last thing left to do is ensure the custom tag fields are indexed properly so the search UI keeps working as expected. The following configuration patch will index the custom tag field to the same field name as the out-of-the-box semantics field.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="https://www.sitecore.com/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultLuceneIndexConfiguration>
          <fields hint="raw:AddCustomField">
            <field luceneName="__semantics" storageType="yes" indexType="tokenized" multivalued="true">tags</field>
          </fields>
        </defaultLuceneIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

This works because Lucene allows multiple fields of the same name within the same document.

We are now able to use the separate tag repositories for each kind of content and the Search by Tag filter will continue to work.

  • Thanks, this was useful. One question - What is this area for? /sitecore/system/Settings/Buckets/Tag Repositories Andy

  • I was following along your example, the only problem is when I go to add the patch file to /app_config/includes I receive the error  "Field control failed to render: Invalid cast from 'System.String' to Sitecore.ContentSearch.ProviderIndexConfiguration'."  on any Multiist with Search fields.  Any help would be appreciated

  • Good stuff, although I also ran into the same issue/error as Dave Breyley and can't find a solution.  Any help would also be appreciated.  Thanks!

  • How would your search for untagged items in say a bucket that has thousands of content files?

  • You can create a default tag in your standard value.... "UNTAGGED"

  • Nice information. I have implemented in other ways and wanted to have feedback here.  The requirement is to implement 10-12 field for tag repository and may require more. Details:- 1) Created multiple tag repository ( TagRepository A and TagRepository B) 2) Tagging location is :- /System/Templates/Sections/Tagging which is having field =”__Semantics” type multilist with search and the location of tag repository. 3) I have duplicate this section with name TagRepository A and TagRepository B 4) Set the source location to newly created tag template - TagRepository A to TagRepository A and for TagRepository B to TagRepository B 5) Change the display name of the field ( remain same “_Semantics” field ( As this is a out of the box field of Sitecore and we can’t change to have the core functionality). 6) Inherited these two template in the page template , Now I have two “__Semantics”  fields in with different display name. 7) Both are showing different tag repository , Able to choose appropriate and it’s giving the result in the search, Index file is having the right information.  Additional changes:-  1) Null value while getting field because of duplicate field (Actually, only the display name is different, but the actual field is ”__Semantics”). Solution:- I use the solar index to get/read the tag values and able to get the right information.  As of now it’s working fine and the approach is have the flexibility to create new tag section run time without changing any code or custom logic to update the index,( As the actual name is same so it will take care automatically).  Please share feedback Thanks Jitendra