Create Terms in Metadata Service Programmatically

Posted on Updated on

Working with Taxonomy Fields that created in Visual Studio without OOTB, it’s not a peace of cake. You have to follow some rules:

  1. In a Feature Receiver, Scoped SITE use the bellow Override
  2. Then you have to activate your field  (Bind) to a specific GUID, if you not it become disabled “Grayed”
    1. Use class bellow “Taxonomy”
  3. For XML Declarative find here you may find other options also
using UTIL = ChangeYourNameSpace.Utils; 
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            var site = properties.Feature.Parent as SPSite;
            var session = new TaxonomySession(site);

            if (session.TermStores.Count == 0) return;
            var termStore = session.TermStores["Managed Metadata Service"];

            if (termStore.Groups.All(x => x.Name != "MY TAGs"))
            {
                //TAGS
                var group = termStore.CreateGroup("MY TAGA");
                var termSetTag = group.CreateTermSet("TAGs");
                termSetTag.CreateTerm("Tag 1", 1033);
                termSetTag.CreateTerm("Tag 2", 1033);
                termSetTag.CreateTerm("Tag 3", 1033);                
                termSetTag.IsAvailableForTagging = true;
                termSetTag.IsOpenForTermCreation = true;
                termSetTag.Contact = "intranet@yourcorp.pt";
            }

            if (termStore.Groups.All(x => x.Name != "MY Documentos"))
            {
                //Tech Documents
                var groupDoc = termStore.CreateGroup("MY Documentos");

                var termSetDocTec = groupDoc.CreateTermSet("My Docs 1");
                termSetDocTec.CreateTerm("Tag 1", 1033);
                termSetDocTec.CreateTerm("Tag 2", 1033);
                termSetDocTec.CreateTerm("Tag 3", 1033);
                termSetDocTec.IsAvailableForTagging = true;
                termSetDocTec.IsOpenForTermCreation = true;
                termSetDocTec.Contact = "intranet@yourcorp.pt";

                var termSetDocCorp = groupDoc.CreateTermSet("My Docks 2");
                termSetDocCorp.CreateTerm("Tag 1", 1033);
                termSetDocCorp.CreateTerm("Tag 2", 1033);
                termSetDocCorp.CreateTerm("Tag 3", 1033);               
                termSetDocCorp.IsAvailableForTagging = true;
                termSetDocCorp.IsOpenForTermCreation = true;
                termSetDocCorp.Contact = "intranet@yourcorp.pt";
            }

            termStore.CommitAll();

            //TAGGING
            UTIL.Taxonomy.ActivateTaxField(site, "{9358ED54-0957-4A56-8047-F940C60D2024}", "TAG's", "TAGs");

            //Documents
            UTIL.Taxonomy.ActivateTaxField(site, "{ECA6E20C-20E0-433C-8D3C-667AD9C25E99}", "Documents", "My Documents");
        });
    }
    catch (Exception ex)
    {
        //Log
    }
}

public static class Taxonomy
{
    public static void ActivateTaxField(SPSite site, string taxonomyGuid,
        string taxonomyGroup, string taxonomyTerm)
    {
        try
        {
            var fieldId = new Guid(taxonomyGuid);
            if (!site.RootWeb.Fields.Contains(fieldId)) return;

            var session = new TaxonomySession(site);

            if (session.TermStores.Count == 0) return;
            var termStore = session.TermStores["Managed Metadata Service"];
            var group = termStore.Groups[taxonomyGroup];
            var termSet = @group.TermSets[taxonomyTerm];

            var field = site.RootWeb.Fields[fieldId] as TaxonomyField;

            if (field == null) return;
            field.SspId = termSet.TermStore.Id;
            field.TermSetId = termSet.Id;
            field.TargetTemplate = string.Empty;
            field.AnchorId = Guid.Empty;
            field.Update();
        }
        catch (Exception ex)
        {
            //Log
        }

    }
} 

Good Luck 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s