Tagging is fun in Sitecore

Searchresult

I would like to talk to you about tagging and tagging content in Sitecore(7.2). As always there are hundred ways of doing it – that’s why I love Sitecore 🙂
I found this hidden gem in Sitecore (a built-in function) and fell in love with it – Semantics.

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.

From a great post that explains it all – CONTENT TAGGING WITH SITECORE 7

The cool thing is that Semantics is part of the /sitecore/templates/System/Templates/Template. That means you can tag everything without adding/creating your own “Tagging template”. You can tag pictures, files, items etc.

The control presenting the tags is a “MULTILIST WITH SEARCH” and is located in /sitecore/templates/System/Templates/Sections/Tagging/Tagging/__Semantics.
BuiltInTagging

The actual tags are defined in the TagRepository in /sitecore/system/Settings/Buckets/TagRepository.
TagRepositoryLocation
The class for handling the tags, Sitecore.Buckets.Search.Tags.SitecoreHostedTagRepository, is referenced in following item /sitecore/system/Settings/Buckets/Tag Repositories/Sitecore Items.
TagRepositoryClass

Create your own tags

To create/add your own tags, you can do it by:

  1. Creating new tags into current TagRepository (/sitecore/system/Settings/Buckets/TagRepository).
  2. Have multiple tag repositories within TagRepository folder (/sitecore/system/Settings/Buckets/TagRepository).
  3. Create your own TagRepository and make your own class and put reference in /sitecore/system/Settings/Buckets/Tag Repositories. Check out this great post – Sitecore tags and Multi Site in Action

I went for the second approach – Doing multiple tag repositories within current TagRepository. It’s quite straight forward.
TagRepositoryCustom

  1. Unbucket TagRepository folder, remove the sample tag/s.
  2. Add your own folder/s and tags. It’s important that you use the tag template /sitecore/templates/System/Item Buckets/Tag.
  3. Finally bucket your folders (optional)

There are some quirks with the search in the “Multilist with Search”. In order to make the search work properly, you will have to change the source in the _Semantics template field: /sitecore/templates/System/Templates/Sections/Tagging/Tagging/__Semantics

The default source looks something like this:

StartSearchLocation={154D56CC-0DE2-43C7-BBC0-A25BD7FFD901}&Filter=_path:154d56cc0de243c7bbc0a25bd7ffd901|-_group:154d56cc0de243c7bbc0a25bd7ffd901

The id in StartSearchLocation is the TagRepository folder and the minus sign removes the folder name in the search result.

First time (when rendering the page) everything works fine but when you do your search (Ajax call) the minus sign messes up things, it adds a space. That’s why you get an empty result. I changed the source to look something like this:

StartSearchLocation={154D56CC-0DE2-43C7-BBC0-A25BD7FFD901}&Filter=_templatename:tag&SortField=_name

I choose to filter by the template tag instead.

Here is a great post on how you can configure/set up the “Multilist with Search” – Constraining the Sitecore 7 Multilist and Treelist Fields with and without Search

Faceting the tags

So now you have your tags and you start tagging your content, that’s great but you don’t see anything (in the search result in the content editor). Next thing is to make the tags visible, you’ll do it in /sitecore/system/Settings/Buckets/Facets/Tags.
FacetsTags
To make it work: you enable it, add the __semantics field and check the “Global facet” checkbox.

Now you will see the tags in the search result, but you will only see the ID’s.
TagsInresult

Make the tags friendly

Next thing is to show some friendly names instead of the ID’s. The computed index field comes to the rescue 🙂
It allows you to perform additional processing before adding data to an index. Here are some great posts you should read:
SITECORE 7: COMPUTED INDEX FIELDS
Computed Index Fields – Sitecore 7 Content Search

In order to make a computed index field you will need to create a class which inherits IComputedIndexField. Method ComputeFieldValue does the magic, in this case I will list the display names from the selected tags in the “Multilist with Search” control.

namespace Sandbox.SitecoreCustomizations.ComputedIndexFields
{
    public class TagsFacet : IComputedIndexField
    {
        private static readonly ID Semantics = new ID("{A14F1B0C-4384-49EC-8790-28A440F3670C}");

        public object ComputeFieldValue(IIndexable indexable)
        {
            var indexableItem = indexable as SitecoreIndexableItem;

            return indexableItem == null ? null : indexableItem.Item.GetMultiListValues(Semantics).Select(tag => tag.DisplayName).ToList();
        }

        public string FieldName { get; set; }
        public string ReturnType { get; set; }
    }
}

Next thing is to add the computed index field to your config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration>
        <defaultIndexConfiguration>
          <fields hint="raw:AddComputedIndexField">
            <field fieldName="tagsfacet" storageType="yes" indexType="untokenized"> Sandbox.SitecoreCustomizations.ComputedIndexFields.TagsFacet, Sandbox</field>
          </fields>
          <fields hint="raw:AddFieldByFieldName">
            <field fieldName="tagsfacet" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String"    settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
                <Analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
            </field>          
          </fields>
        </defaultIndexConfiguration>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>

The part AddFieldByFieldName ensures that facet names(tags) are displayed correctly. I was going crazy and could not understand why the facet names were split up.

But thanks to Martinas blog post, SITECORE 7 SEARCH TIPS: COMPUTED FIELDS, I found the solution.

If you are storing a value for display or faceting purposes (e.g. the name of a city, which you would want saved as ‘Los Angeles’, not ‘Los’ and ‘Angeles’ separately), you want it to be stored as untokenized. To do this, add your computed field as an entry in the fieldNames hint=”raw:AddFieldByFieldName” section of the config file alongside the regular, non-computed fields and specify an Analyzer

Thank you Martina 🙂

Finally update item /sitecore/system/Settings/Buckets/Facets/Tags by enter the computed field name tagsfacet instead of the __semantics field.
Now the facets will be presented with their display name instead of just showing an id.

That’s all for now folks 🙂
P.S Don’t forget to update your Visual Studio 2013, update 4 is out.


7 thoughts on “Tagging is fun in Sitecore

    1. Hi Sean

      Thanks for testing my stuff 🙂

      The GetMultilistValues is an extension method I’ve created, here is the code for it:
      public static IEnumerable GetMultiListValues(this Item item, ID fieldId)
      {
      return (new MultilistField(item.Fields[fieldId])).GetItems();
      }

      Thanks again for reading my post.
      Göran

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.