public static List <Log> UpdateSite(ClientContext ctx, DefaultFields defaultFields)
        {
            const string TaxonomyFieldType      = "TaxonomyFieldType";
            const string TaxonomyFieldTypeMulti = "TaxonomyFieldTypeMulti";
            Web          web = ctx.Web;

            try
            {
                Log.ClearLog();
                ctx.Load(web);

                FieldCollection fields = web.Fields;
                ctx.Load(fields, tcol => tcol.Where(t => t.TypeAsString == TaxonomyFieldType || t.TypeAsString == TaxonomyFieldTypeMulti));
                ctx.ExecuteQuery();

                /// Update Columns

                foreach (var column in defaultFields)
                {
                    Field field = GetField(column.InternalName, fields);
                    if (field == null)
                    {
                        Log.Error(web.Url, "", column.InternalName, "Invalid Field Name");
                    }
                    else
                    {
                        TaxonomyField taxonomyField = ctx.CastTo <TaxonomyField>(field);
                        if (taxonomyField.AllowMultipleValues)
                        {
                            TaxonomyFieldValueCollection taxonomyFieldValues = new TaxonomyFieldValueCollection(ctx, "", field);
                            taxonomyFieldValues.PopulateFromLabelGuidPairs(column.TermLabel + "|" + column.Guid);

                            var validatedValues = taxonomyField.GetValidatedString(taxonomyFieldValues);
                            ctx.ExecuteQuery();
                            taxonomyField.DefaultValue = validatedValues.Value;
                        }
                        else
                        {
                            TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue();
                            taxonomyFieldValue.WssId    = -1;
                            taxonomyFieldValue.Label    = column.TermLabel;
                            taxonomyFieldValue.TermGuid = column.Guid;
                            var validatedValue = taxonomyField.GetValidatedString(taxonomyFieldValue);
                            ctx.ExecuteQuery();
                            taxonomyField.DefaultValue = validatedValue.Value;
                        }

                        taxonomyField.UpdateAndPushChanges(true);
                        ctx.ExecuteQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(web.Url, "", "", ex.Message);
            }
            return(Log.GetLog());
        }
Example #2
0
        private static void ValidateTaxonomyFieldDefaultValue(TaxonomyField field)
        {
            //get validated value with correct WssIds
            var validatedValue = GetTaxonomyFieldValidatedValue(field, field.DefaultValue);

            if (!string.IsNullOrEmpty(validatedValue) && field.DefaultValue != validatedValue)
            {
                field.DefaultValue = validatedValue;
                field.UpdateAndPushChanges(true);
                field.Context.ExecuteQueryRetry();
            }
        }
Example #3
0
        private static void SetTaxonomyFieldOpenValue(TaxonomyField field, string taxonomyFieldXml)
        {
            bool openValue;
            var  taxonomyFieldElement = XElement.Parse(taxonomyFieldXml);
            var  openAttributeValue   = taxonomyFieldElement.Attribute("Open") != null?taxonomyFieldElement.Attribute("Open").Value : "";

            if (bool.TryParse(openAttributeValue, out openValue))
            {
                field.Open = openValue;
                field.UpdateAndPushChanges(true);
                field.Context.ExecuteQueryRetry();
            }
        }
        static void PushStatusFieldUpdate(ClientContext ctx, Web web, FieldCollection fields)
        {
            // Push update of status field, as users isn't able to set column default value on library without it, due to some strange behavior from Microsoft

            try {
                Field statusField = fields.GetFieldByInternalName("EIMStatus");
                if (statusField == null)
                {
                    return;                      //Check if status field has been found
                }
                TaxonomyField taxStatusField = ctx.CastTo <TaxonomyField>(statusField);
                taxStatusField.UpdateAndPushChanges(true);
                ctx.ExecuteQuery();
            }
            catch (Exception ex) {
                Log.Error(web.Url, "", "EIMStatus", ex.Message);
            }
        }
Example #5
0
 private static void ValidateTaxonomyFieldDefaultValue(TaxonomyField field)
 {
     //get validated value with correct WssIds
     var validatedValue = GetTaxonomyFieldValidatedValue(field, field.DefaultValue);
     if (!string.IsNullOrEmpty(validatedValue) && field.DefaultValue != validatedValue)
     {
         field.DefaultValue = validatedValue;
         field.UpdateAndPushChanges(true);
         field.Context.ExecuteQueryRetry();
     }
 }