Add or remove synonyms to tags

When you add synonyms to a tag, you can filter your content items by the tag name or by the tag synonyms. You can, as well, to add or delete synonyms programmatically.
For more information about adding tags via code, see Add and remove taxonomies: Add tags.

You can add tag synonyms in the code-behind of web form, in a custom widget, and so on.

Add synonyms

To add a new synonym:

  1. Use TaxonomyManager which is the manager class for taxonomies.
  2. Get the specific taxon with name Test.
  3. Create a new synonym of type Synonym.
  4. Define the parent and the value of the synonym created.
  5. Call the manager’s SaveChanges method to persist the changes to the database.

Following is an example of the code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Taxonomies.Tutorials
{
   public partial class TaxonomiesTutorialsSnippets
   {
       public static void AddTagSynonym()
       {
           //gets an instance of the TaxonomyManager
           TaxonomyManager taxaManager = TaxonomyManager.GetManager();

           //get the taxa by name
           var taxa = taxaManager.GetTaxa<FlatTaxon>().Where(t => t.Name == "Test").Single();

           //creates a synonym
           var synonym = taxaManager.CreateSynonym();

           //set the value of the synonym and the tag that it is associated with
           synonym.Value = "value goes here";
           synonym.Parent = taxa;

           //always call SaveChanges in order to finish the operation
           taxaManager.SaveChanges();
       }
   }
}

Remove synonyms

To remove a synonym:

  1. Use TaxonomyManager which is the manager class for taxonomies.
  2. Lookup the synonym by value.
  3. Call the manager class Delete method.
  4. Call the manager class SaveChanges method to persist the changes to the database.

Following is an example of the code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Taxonomies;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Taxonomies.Tutorials
{
   public partial class TaxonomiesTutorialsSnippets
   {
       public static void RemoveTagSynonym()
       {
           //gets an instance of the TaxonomyManager
           TaxonomyManager taxaManager = TaxonomyManager.GetManager();

           //gets the synonym by value
           var synonym = taxaManager.GetSynonyms().Where(s => s.Value == "value goes here").FirstOrDefault();

           //deletes the synonym
           taxaManager.Delete(synonym);

           //call SaveChanges method in order to finish the operation
           taxaManager.SaveChanges();
       }
   }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
This Article Contains
New to Sitefinity?