2

I have the following code inside my CSOM:-

Folder folder = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/library3/" + currentFilingSystemItem["DealName"].ToString());
context.ExecuteQuery();

currentFilingSystemItem["DealStage"] =  "7178cede-c689-4b47-853a-88bb8b95e230";;
currentFilingSystemItem.Update();
folder.ListItemAllFields["DealStage"] = "7178cede-c689-4b47-853a-88bb8b95e230";
folder.Update();
context.ExecuteQuery();

the above code will update the list item currentFilingSystemItem DealStage value but will not update the folder DealStage value. the DealStage is a managed metadata coumn which alllow single selection. Any advice?

Regards

3
  • Does DealStage allow multiple term selections, or is it just Single Value?
    – willman
    Feb 26, 2020 at 15:57
  • @willman single value only
    – John John
    Feb 26, 2020 at 16:01
  • okay, I have updated a few things (including a couple of types) in my answer below.
    – willman
    Feb 26, 2020 at 16:27

2 Answers 2

0

folder.ListItemAllFields return ListItem object which you need to update not the folder object. Try following way to update.

ListItem item = folder.ListItemAllFields;
item["DealStage"] = "7178cede-c689-4b47-853a-88bb8b95e230";
item.Update();
1
  • this will not raise any error but will not update the DealStage
    – John John
    Feb 26, 2020 at 16:03
0

The primary problem is that you are calling Update() on the folder, when you need to call it on the ListItemAllFields property:

folder.ListItemAllFields["DealStage"] = "7178cede-c689-4b47-853a-88bb8b95e230";
folder.ListItemAllFields.Update();
context.ExecuteQuery();

But, secondarily, you should use TaxonomyFieldValue (for single-value managed metadata columns) and TaxonomyFieldValueCollection (for multi-value managed metadata columns). If your currentFilingSystemItem, then you may have gotten lucky, in most cases, setting a taxonomy field to a raw guid will not update the value correctly. Also note the use of the SetFieldValueByValue method on the actual Field object, rather than simply setting the field equal to a value. Taxonomy fields are considerably more difficult to code updates for than scalar value fields are. Your code should look more like this:

Folder folder = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/library3/" + currentFilingSystemItem["DealName"].ToString());
context.Load(folder);
var folderListItem = folder.ListItemAllFields;
context.Load(folderListItem);
var dealStageField = list.Fields.GetByInternalNameOrTitle("DealStage");
context.Load(dealStageField);
context.ExecuteQuery();

var dealStageTaxField = context.CastTo<TaxonomyField>(field);
var dealStageTermValue = new TaxonomyFieldValue();
//dealStageTermValue.Label = "Display Text from the Term"; // Note that if you do not set this value, or you set it to a value that doesn't match what is in the termstore, the value may display incorrectly in list views for several minutes before SharePoint synchronizes from the termstore.
dealStageTermValue.TermGuid = new Guid("7178cede-c689-4b47-853a-88bb8b95e230");
dealStageTermValue.WssId = -1;

dealStageTaxField.SetFieldValueByValue(currentFilingSystemItem, dealStageTermValue);
currentFilingSystemItem.Update();

dealStageTaxField.SetFieldValueByValue(folderListItem, dealStageTermValue);
folderListItem.Update();

context.ExecuteQuery();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.