feganmeister
feganmeister

Reputation: 726

SharePoint Online CSOM Site Column Default Value

In SharePoint Online, I'm having problems setting a site Managed Metadata column default value using CSOM (C#). For brevity, I've not included exception handling in my code. Here's what I have:

// Code snippet for what I'm trying to achieve
using (ClientContext ctx = NewCtx(SiteInfo.Url)) // NewCtx is just a static member I use to return a ClientContext object
{
  Field taxColumn = (Field)ctx.Web.Fields.GetByTitle("myMMColumnName");
  ctx.Load(taxColumn);
  ctx.ExecuteQuery();
  TaxonomyFieldValue termField = new TaxonomyFieldValue();
  termField.Label = "My MM Term";
  termField.TermGuid = "b269aef7-6f47-4b02-bf80-7edfb7166a30";
  termField.WssId = -1;
  taxColumn.DefaultValue = value;
  // Place holder for added code (see below)
  taxColumn.Update();
  ctx.Load(taxColumn);
  ctx.ExecuteQuery();
}

The value appears in the UI under Site Columns, but no associated item is created in the TaxonomyHiddenList, which makes sense because setting the lookup WssId to "-1" doesn't resolve and therefore, the default value doesn't have a lookup and appears empty in the list- and library-level column.

The closest resolution I found was an article (albeit not CSOM and linked below) suggested creating a "dummy" list item (against a list that was consuming the MM column). This would trigger the creation of the item in the TaxonomyHiddenList but the article suggests not committing the "dummy" item creation. I've tried this too, but seems to have no effect. This is the code I added to the place holder mentioned in the previous code block:

List myList = ctx.Web.Lists.GetByTitle("My List");
ctx.Load(myList);
ctx.ExecuteQuery();
TaxonomyField taxField = ctx.CastTo<TaxonomyField>(myList.Fields.GetByTitle("myMMColumnName"));
ListItemCreationInformation newItemInfo = new ListItemCreationInformation();
ListItem newItem = myList.AddItem(newItemInfo);
newItem["myMMColumnName"] = termField;
taxField.SetFieldValueByValue(newItem, termField);

The article uses the .SetFieldValue method, which isn't available in CSOM so I may well have assumed incorrectly that .SetFieldValueByValue is comparable.

Thanks for reading!

References: Article - http://sharepointificate.blogspot.com/2014/04/setting-managed-metadata-column-default.html

Upvotes: 2

Views: 3453

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59338

SetFieldValue Method belongs to TaxonomyField class, it means the line:

Field taxColumn = (Field)ctx.Web.Fields.GetByTitle(taxFieldTitle);

needs to be replaced with:

var taxColumn = ctx.CastTo<TaxonomyField>(ctx.Web.Fields.GetByTitle(taxFieldTitle));

or

TaxonomyField taxColumn = ctx.CastTo<TaxonomyField>(ctx.Web.Fields.GetByTitle(taxFieldTitle));

Once TaxonomyField is initialized, the default value could be set as shown below:

//get taxonomy field
var taxColumn = ctx.CastTo<TaxonomyField>(ctx.Web.Fields.GetByTitle(taxFieldTitle));
ctx.Load(taxColumn);
ctx.ExecuteQuery();
//initialize taxonomy field value
var defaultValue = new TaxonomyFieldValue();
defaultValue.WssId = -1;
defaultValue.Label = termLabel;
defaultValue.TermGuid = termId.ToString();
//retrieve validated taxonomy field value
var validatedValue = taxColumn.GetValidatedString(defaultValue);
ctx.ExecuteQuery();
//set default value for a taxonomy field
taxColumn.DefaultValue = validatedValue.Value;
taxColumn.Update();
ctx.ExecuteQuery();

Note: TaxonomyField.GetValidatedString method is utilized for validation of taxonomy field value which in turn includes the resolving of WssId value

Update

Use Field.UpdateAndPushChanges method to propagate changesto all lists that use the field

So, replace:

taxColumn.Update();
ctx.ExecuteQuery();

with:

taxColumn.UpdateAndPushChanges(true);
ctx.ExecuteQuery();

Upvotes: 2

Related Questions