/// <summary> /// Can be used to create taxonomy field remotely to web. /// </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="termSet">Taxonomy Termset</param> /// <param name="multiValue">if true, create a multivalue taxonomy field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this Web web, Guid id, string internalName, string displayName, string group, TermSet termSet, bool multiValue = false) { internalName.ValidateNotNullOrEmpty("internalName"); displayName.ValidateNotNullOrEmpty("displayName"); termSet.ValidateNotNullOrEmpty("termSet"); try { var _field = web.CreateField(id, internalName, multiValue ? "TaxonomyFieldTypeMulti" : "TaxonomyFieldType", true, displayName, group, "ShowField=\"Term1033\""); WireUpTaxonomyField(web, _field, termSet, multiValue); _field.Update(); web.Context.ExecuteQuery(); return(_field); } catch (Exception) { ///If there is an exception the hidden field might be present FieldCollection _fields = web.Fields; web.Context.Load(_fields, fc => fc.Include(f => f.Id, f => f.InternalName)); web.Context.ExecuteQuery(); var _hiddenField = id.ToString().Replace("-", ""); var _field = _fields.FirstOrDefault(f => f.InternalName == _hiddenField); if (_field != null) { _field.DeleteObject(); web.Context.ExecuteQuery(); } throw; } }
/// <summary> /// Can be used to create taxonomy field remotely in a list. /// </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="termSet">Taxonomy TermSet</param> /// <param name="multiValue">If true, create a multivalue field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this List list, Guid id, string internalName, string displayName, string group, TermSet termSet, bool multiValue = false) { internalName.ValidateNotNullOrEmpty("internalName"); displayName.ValidateNotNullOrEmpty("displayName"); termSet.ValidateNotNullOrEmpty("termSet"); try { var _field = list.CreateField(id, internalName, multiValue ? "TaxonomyFieldTypeMulti" : "TaxonomyFieldType", true, displayName, group, "ShowField=\"Term1033\""); WireUpTaxonomyField(list, _field, termSet, multiValue); _field.Update(); list.Context.ExecuteQuery(); return _field; } catch (Exception) { ///If there is an exception the hidden field might be present FieldCollection _fields = list.Fields; list.Context.Load(_fields, fc => fc.Include(f => f.Id, f => f.InternalName)); list.Context.ExecuteQuery(); var _hiddenField = id.ToString().Replace("-", ""); var _field = _fields.FirstOrDefault(f => f.InternalName == _hiddenField); if (_field != null) { _field.Hidden = false; // Cannot delete a hidden column _field.Update(); _field.DeleteObject(); list.Context.ExecuteQuery(); } throw; } }