Create taxonomy field programatically

03/06/2015 16:38

This code creates taxonomy field (hidden note field is also created by sharepoint)

- Add reference Microsoft.SharePoint.Taxonomy

- add using System.Linq;

- Use your own Logging class

- field.IsPathRendered = true; setting is not needed, it was our customer request


 public static TaxonomyField CreateTaxonomyField(SPSite site, string termStoreName, string groupName, string termSetName, string fieldInternalName, string fieldDisplayName, string fieldGroupName)
        {
            TaxonomyField existingField = site.RootWeb.Fields.TryGetFieldByStaticName(fieldInternalName) as TaxonomyField;
            if (existingField == null)
            {
                TaxonomySession session = new TaxonomySession(site);
                if (session.TermStores.Count != 0)
                {
                    SPWeb rootweb = site.RootWeb;
                    TermStore termStore = session.TermStores.OfType<TermStore>().Where(t => t.Name.Equals(termStoreName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                    if (termStore != null)
                    {
                        Group group = termStore.Groups.OfType<Group>().Where(g => g.Name.Equals(groupName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                        if (group != null)
                        {
                            TermSet termSet = group.TermSets.OfType<TermSet>().Where(tset => tset.Name.Equals(termSetName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                            if (termSet != null)
                            {
                                TaxonomyField field = rootweb.Fields.CreateNewField("TaxonomyFieldType", fieldInternalName) as TaxonomyField;

                                field.Group = fieldGroupName;
                                field.SspId = termSet.TermStore.Id;
                                field.TermSetId = termSet.Id;
                                field.TargetTemplate = string.Empty;
                                field.AllowMultipleValues = false;
                                field.CreateValuesInEditForm = true;
                                field.Open = true;
                                field.AnchorId = Guid.Empty;
                                field.IsPathRendered = true;

                                //Add the taxonomy field to site columns
                                rootweb.Fields.Add(field);
                                rootweb.Update();

                                TaxonomyField fieldAdded = rootweb.Fields.TryGetFieldByStaticName(fieldInternalName) as TaxonomyField;
                                fieldAdded.Title = fieldDisplayName; //set human display name
                                fieldAdded.Update();
                                return fieldAdded;
                            }
                            else
                            {
                                Logging.LogInformation(codeFileName, "CreateTaxonomyField", string.Format("Term Set '{0}' doesn't exist on '{1}'.", termSetName, site.Url));
                                return null;
                            }
                        }
                        else
                        {
                            Logging.LogInformation(codeFileName, "CreateTaxonomyField", string.Format("Term Group '{0}' doesn't exist on '{1}'.", groupName, site.Url));
                            return null;
                        }
                    }
                    else
                    {
                        Logging.LogInformation(codeFileName, "CreateTaxonomyField", string.Format("There are no term store '{0}' in '{1}'.", termStoreName, site.Url));
                        return null;
                    }
                }
                else
                {
                    Logging.LogInformation(codeFileName, "CreateTaxonomyField", string.Format("There are no term stores in '{0}'.", site.Url));
                    return null;
                }
            }
            else
            {
                return existingField;
            }
        }