/// <summary> /// Can be used to create taxonomy field remotely in a list. Associated to group and term set in the GetDefaultSiteCollectionTermStore /// </summary> /// <param name="list">List to be processed</param> /// <param name="id">Unique Id for the taxonomy field</param> /// <param name="internalName">Internal Name of the field</param> /// <param name="displayName">Display name</param> /// <param name="group">Site column group</param> /// <param name="mmsGroupName">Taxonomy group </param> /// <param name="mmsTermSetName">Term set name</param> /// <param name="multiValue">If true, create multi value field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this List list, Guid id, string internalName, string displayName, string group, string mmsGroupName, string mmsTermSetName, bool multiValue = false) { id.ValidateNotNullOrEmpty("id"); internalName.ValidateNotNullOrEmpty("internalName"); displayName.ValidateNotNullOrEmpty("displayName"); mmsGroupName.ValidateNotNullOrEmpty("mmsGroupName"); mmsTermSetName.ValidateNotNullOrEmpty("mmsTermSetName"); var clientContext = list.Context as ClientContext; TermStore termStore = clientContext.Site.GetDefaultSiteCollectionTermStore(); if (termStore == null) throw new NullReferenceException("The default term store is not available."); // get the term group and term set TermGroup termGroup = termStore.Groups.GetByName(mmsGroupName); TermSet termSet = termGroup.TermSets.GetByName(mmsTermSetName); list.Context.Load(termStore); list.Context.Load(termSet); list.Context.ExecuteQuery(); return list.CreateTaxonomyField(id, internalName, displayName, group, termSet, multiValue); }
/// <summary> /// Can be used to create taxonomy field remotely to web. Associated to group and term set in the GetDefaultSiteCollectionTermStore /// </summary> /// <param name="web">Site to be processed - can be root web or sub site</param> /// <param name="id">Unique Id for the taxonomy field</param> /// <param name="internalName">Internal Name of the field</param> /// <param name="displayName">Display name</param> /// <param name="group">Site column group</param> /// <param name="mmsGroupName">Taxonomy group </param> /// <param name="mmsTermSetName">Term set name</param> /// <param name="multiValue">If true, create a multi value field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this Web web, Guid id, string internalName, string displayName, string group, string mmsGroupName, string mmsTermSetName, bool multiValue = false) { id.ValidateNotNullOrEmpty("id"); internalName.ValidateNotNullOrEmpty("internalName"); displayName.ValidateNotNullOrEmpty("displayName"); // Group can be emtpy mmsGroupName.ValidateNotNullOrEmpty("mmsGroupName"); mmsTermSetName.ValidateNotNullOrEmpty("mmsTermSetName"); TermStore termStore = GetDefaultTermStore(web); if (termStore == null) throw new NullReferenceException("The default term store is not available."); // get the term group and term set TermGroup termGroup = termStore.Groups.GetByName(mmsGroupName); TermSet termSet = termGroup.TermSets.GetByName(mmsTermSetName); web.Context.Load(termStore); web.Context.Load(termSet); web.Context.ExecuteQuery(); return web.CreateTaxonomyField(id, internalName, displayName, group, termSet, multiValue); }