C
C#2w ago
Jkiller295

I need help with SharePoint CSOM and Managed Metadata columns please

I'm developing a small WinForms application to consolidate files from multiple SharePoint document libraries into a single library on another team site. Both the source and target libraries are within the same SharePoint tenant. I wrote the following code to copy the fields from the source libraries:
2 Replies
Jkiller295
Jkiller295OP2w ago
To set the field values after copying files to the new library, I use this code:
Field targetField = targetLibrary.Fields.GetByInternalNameOrTitle(kvp.Key);
targetContext.Load(targetField);
targetContext.ExecuteQueryRetry();
if (fieldValue is TaxonomyFieldValueCollection termCollection)
{
TaxonomyField targetTaxonomyField = targetContext.CastTo<TaxonomyField>(targetField);
TaxonomyFieldValueCollection destinationTaxonomyValues = new TaxonomyFieldValueCollection(targetContext, null, targetTaxonomyField);
foreach (TaxonomyFieldValue value in termCollection)
{
destinationTaxonomyValues.PopulateFromLabelGuidPairs($"{value.Label}|{value.TermGuid}");
}
targetTaxonomyField.SetFieldValueByValueCollection(newListItem, destinationTaxonomyValues);
}
else if (fieldValue is TaxonomyFieldValue term)
{
TaxonomyField targetTaxonomyField = targetContext.CastTo<TaxonomyField>(targetField);
TaxonomyFieldValue termValue = new TaxonomyFieldValue
{
TermGuid = term.TermGuid
};
targetTaxonomyField.SetFieldValueByValue(newListItem, termValue);
}
Field targetField = targetLibrary.Fields.GetByInternalNameOrTitle(kvp.Key);
targetContext.Load(targetField);
targetContext.ExecuteQueryRetry();
if (fieldValue is TaxonomyFieldValueCollection termCollection)
{
TaxonomyField targetTaxonomyField = targetContext.CastTo<TaxonomyField>(targetField);
TaxonomyFieldValueCollection destinationTaxonomyValues = new TaxonomyFieldValueCollection(targetContext, null, targetTaxonomyField);
foreach (TaxonomyFieldValue value in termCollection)
{
destinationTaxonomyValues.PopulateFromLabelGuidPairs($"{value.Label}|{value.TermGuid}");
}
targetTaxonomyField.SetFieldValueByValueCollection(newListItem, destinationTaxonomyValues);
}
else if (fieldValue is TaxonomyFieldValue term)
{
TaxonomyField targetTaxonomyField = targetContext.CastTo<TaxonomyField>(targetField);
TaxonomyFieldValue termValue = new TaxonomyFieldValue
{
TermGuid = term.TermGuid
};
targetTaxonomyField.SetFieldValueByValue(newListItem, termValue);
}
The issue is that the Taxonomy fields in the target library remain blank. I verified that the field settings match between the source and target libraries, and they reference the same TermSet. I also attempted to manually set the Term values in the target library, but they disappear after saving. Does anyone have any insight into why this might be happening? Any advice is greatly appreciated!
Want results from more Discord servers?
Add your server