示例#1
0
        /// <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="fieldCreationInformation">Creation Information of the field</param>
        /// <returns>New taxonomy field</returns>
        public static Field CreateTaxonomyField(this Web web, TaxonomyFieldCreationInformation fieldCreationInformation)
        {
            fieldCreationInformation.InternalName.ValidateNotNullOrEmpty("internalName");
            fieldCreationInformation.DisplayName.ValidateNotNullOrEmpty("displayName");
            fieldCreationInformation.TaxonomyItem.ValidateNotNullOrEmpty("taxonomyItem");

            CleanupTaxonomyHiddenField(web, web.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 = web.CreateField(fieldCreationInformation);

            WireUpTaxonomyFieldInternal(_field, fieldCreationInformation.TaxonomyItem, fieldCreationInformation.MultiValue);
            _field.Update();

            web.Context.ExecuteQuery();

            return _field;
        }
示例#2
0
 private static void CleanupTaxonomyHiddenField(Web web, FieldCollection fields, TaxonomyFieldCreationInformation fieldCreationInformation)
 {
     // if the Guid is empty then we'll have no issue
     if (fieldCreationInformation.Id != Guid.Empty)
     {
         FieldCollection _fields = fields;
         web.Context.Load(_fields, fc => fc.Include(f => f.Id, f => f.InternalName, f => f.Hidden));
         web.Context.ExecuteQuery();
         var _field = _fields.FirstOrDefault(f => f.InternalName.Equals(fieldCreationInformation.InternalName));
         // if the field does not exist we assume the possiblity that it was created earlier then deleted and the hidden field was left behind
         // if the field does exist then return and let the calling process exception out when attempting to create it
         // this does not appear to be an issue with lists, just site columns, but it doesnt hurt to check
         if (_field == null)
         {
             // The hidden field format is the id of the field itself with hyphens removed and the first character replaced
             // with a random character, so get everything to the right of the first character and remove hyphens
             var _hiddenField = fieldCreationInformation.Id.ToString().Replace("-", "").Substring(1);
             _field = _fields.FirstOrDefault(f => f.InternalName.EndsWith(_hiddenField));
             if (_field != null)
             {
                 if (_field.Hidden)
                 {
                     // just in case the field itself is hidden, make sure it is not because depending on the current CU hidden fields may not be deletable
                     _field.Hidden = false;
                     _field.Update();
                 }
                 _field.DeleteObject();
                 web.Context.ExecuteQuery();
             }
         }
     }
 }