Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
When providing a FieldDefaults under a ListInstance for a taxonomy fi…
Browse files Browse the repository at this point in the history
…eld and setting the ID to -1, this code change will make sure to update the -1 to the actual ID as it exists in the hidden TaxonomyHiddenList list or if it doesn't exist in there yet, create a new entry for it and update the -1 to the ID of this new entry
  • Loading branch information
KoenZomers committed Jul 18, 2019
1 parent ca8da63 commit 03589de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ private static void ValidateTaxonomyFieldDefaultValue(TaxonomyField field)
}
}

private static string GetTaxonomyFieldValidatedValue(TaxonomyField field, string defaultValue)
public static string GetTaxonomyFieldValidatedValue(TaxonomyField field, string defaultValue)
{
string res = null;
object parsedValue = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,22 @@ private static void ProcessFieldDefaults(Web web, TokenParser parser, ListInfo l
{
var fieldDefaultValue = parser.ParseString(fieldDefault.Value);
var field = listInfo.SiteList.Fields.GetByInternalNameOrTitle(fieldDefault.Key);

// If there has been a default value specified, request the type of field so we know if we have to deal with the default value in a special way according to the field type. If no default has been specified, we can simply set the default to NULL, regardless of the field type.
if (!string.IsNullOrEmpty(fieldDefaultValue))
{
// A default value has been provided, request the field type so we know if we have to apply special handling for the type of field
web.Context.Load(field, f => f.TypeAsString);
web.Context.ExecuteQueryRetry();

// In the case of a Taxonomy field, ensure that the default value which is in the format <ID>;#<NAME>|<TERMGUID> has its ID match with the actual ID in the TaxonomyHiddenList of the target site if the ID has been set to -1.
if (field.TypeAsString == "TaxonomyFieldType" || field.TypeAsString == "TaxonomyFieldTypeMulti")
{
fieldDefaultValue = ObjectField.GetTaxonomyFieldValidatedValue(web.Context.CastTo<TaxonomyField>(field), fieldDefaultValue);
}
}
field.DefaultValue = fieldDefaultValue;

field.Update();
web.Context.ExecuteQueryRetry();
}
Expand Down

0 comments on commit 03589de

Please sign in to comment.