private static Field CreateTaxonomyFieldInternal(this List list, Guid id, string internalName, string displayName, string group, TaxonomyItem taxonomyItem, bool multiValue) { internalName.ValidateNotNullOrEmpty("internalName"); displayName.ValidateNotNullOrEmpty("displayName"); taxonomyItem.ValidateNotNullOrEmpty("taxonomyItem"); try { List<KeyValuePair<string, string>> additionalAttributes = new List<KeyValuePair<string, string>>(); additionalAttributes.Add(new KeyValuePair<string, string>("ShowField", "Term1033")); FieldCreationInformation fieldCI = new FieldCreationInformation(multiValue ? "TaxonomyFieldTypeMulti" : "TaxonomyFieldType") { Id = id, InternalName = internalName, AddToDefaultView = true, DisplayName = displayName, Group = group, AdditionalAttributes = additionalAttributes }; var _field = list.CreateField(fieldCI); WireUpTaxonomyFieldInternal(_field, taxonomyItem, 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; } }
/// <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; } }
/// <summary> /// Can be used to create taxonomy field remotely in a list. /// </summary> /// <param name="list">List to be processed</param> /// <param name="fieldCreationInformation">Creation information of the field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this List list, TaxonomyFieldCreationInformation fieldCreationInformation) { fieldCreationInformation.InternalName.ValidateNotNullOrEmpty("internalName"); fieldCreationInformation.DisplayName.ValidateNotNullOrEmpty("displayName"); fieldCreationInformation.TaxonomyItem.ValidateNotNullOrEmpty("taxonomyItem"); CleanupTaxonomyHiddenField(list.ParentWeb, list.Fields, fieldCreationInformation); if (fieldCreationInformation.Id == Guid.Empty) { fieldCreationInformation.Id = Guid.NewGuid(); } var showFieldAttribute = new KeyValuePair<string, string>(); if (fieldCreationInformation.AdditionalAttributes != null) { showFieldAttribute = fieldCreationInformation.AdditionalAttributes.FirstOrDefault(a => a.Key == "ShowField"); } if (showFieldAttribute.Key == null) { if (fieldCreationInformation.AdditionalAttributes == null) { fieldCreationInformation.AdditionalAttributes = new List<KeyValuePair<string, string>>(); } ((List<KeyValuePair<string, string>>)fieldCreationInformation.AdditionalAttributes).Add(new KeyValuePair<string, string>("ShowField", "Term1033")); } var _field = list.CreateField(fieldCreationInformation); WireUpTaxonomyFieldInternal(_field, fieldCreationInformation.TaxonomyItem, fieldCreationInformation.MultiValue); _field.Update(); list.Context.ExecuteQueryRetry(); return _field; }
/// <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> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this Web web, Guid id, string internalName, string displayName, string group, string mmsGroupName, string mmsTermSetName) { try { var _field = web.CreateField(id, internalName, "TaxonomyFieldType", true, displayName, group, "ShowField=\"Term1033\""); web.WireUpTaxonomyField(id, mmsGroupName, mmsTermSetName); _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> /// Creates field from xml structure which follows the classic feature framework structure /// </summary> /// <param name="web">Site to be processed - can be root web or sub site. Site columns should be created to root site.</param> /// <param name="xd">Actual XML document</param> /// <param name="skipFieldIfExists">If set to true and field exists, field is skipped. If set to false, exception is raised.</param> public static void CreateFieldsFromXML(this Web web, XmlDocument xmlDoc) { XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("namespace", "http://schemas.microsoft.com/sharepoint/"); XmlDocument xdocField = null; XmlNodeList fields = xmlDoc.SelectNodes("//namespace:Field", nsmgr); int count = fields.Count; foreach (XmlNode field in fields) { xdocField = new XmlDocument(); xdocField.LoadXml(field.OuterXml); string fieldName = xdocField.SelectSingleNode("//namespace:Field", nsmgr).Attributes["Name"].Value; // IF field already existed, let's move on if (web.FieldExistsByName(fieldName)) { continue; } web.CreateField(field.OuterXml); } }
/// <summary> /// Can be used to create taxonomy field remotely in a list. /// </summary> /// <param name="list">List to be processed</param> /// <param name="fieldCreationInformation">Creation information of the field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this List list, TaxonomyFieldCreationInformation fieldCreationInformation) { fieldCreationInformation.InternalName.ValidateNotNullOrEmpty("internalName"); fieldCreationInformation.DisplayName.ValidateNotNullOrEmpty("displayName"); fieldCreationInformation.TaxonomyItem.ValidateNotNullOrEmpty("taxonomyItem"); if (fieldCreationInformation.Id == Guid.Empty) { fieldCreationInformation.Id = Guid.NewGuid(); } try { List<KeyValuePair<string, string>> additionalAttributes = new List<KeyValuePair<string, string>>(); additionalAttributes.Add(new KeyValuePair<string, string>("ShowField", "Term1033")); var _field = list.CreateField(fieldCreationInformation); WireUpTaxonomyFieldInternal(_field, fieldCreationInformation.TaxonomyItem, fieldCreationInformation.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 = fieldCreationInformation.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; } }