0

I have the following code inside my RER:-

foreach (ListItem currentFilingSystemItem in collListItem)
    {
Folder folder = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/library3/" + currentFilingSystemItem["DealName"].ToString());
context.Load(folder);
context.ExecuteQuery(); 
folder.MoveTo(context.Web.ServerRelativeUrl + "/ArchDocs/" + currentFilingSystemItem["DealName"].ToString());
folder.Update();

context.Load(folder);
context.Load(folder.Files);
context.Load(folder.Folders);
FileCollection files = folder.Files;
FolderCollection folders = folder.Folders;
context.ExecuteQuery();
foreach (File f in files)
  {
     ListItem item = f.ListItemAllFields;
     context.Load(item);
     context.ExecuteQuery();
     item["DealStage"] = currentFilingSystemItem["DealStage"];
     item.Update();
     context.ExecuteQuery();

  }
foreach (Folder fo in folders)
  {

      ListItem item = fo.ListItemAllFields;
      context.Load(item);
      context.ExecuteQuery();
      item["DealStage"] = currentFilingSystemItem["DealStage"];
      item.Update();
      context.ExecuteQuery();

  }

where i am trying to change a managed metadata column named "DealStage" using this item["DealStage"] = currentFilingSystemItem["DealStage"]; for both File and Folders, but my above code will not raise any exception, but the DealStage will not get update! can anyone advice on this please? Thanks

  • DoesDealStage allow multiple Term selections, or just single value? – willman Feb 26 at 15:55
  • @willman it is a single value – SharePoint TestDev Feb 26 at 16:03
1

As I mentioned in response to your other posted question, you should use [TaxonomyFieldValue][1] (for single-value managed metadata columns) and [TaxonomyFieldValueCollection][2] (for multi-value managed metadata columns). 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.

Give this a try:

foreach (ListItem currentFilingSystemItem in collListItem)
{
    Folder folder = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/library3/" + currentFilingSystemItem["DealName"].ToString());
    context.Load(folder);
    context.ExecuteQuery(); 
    folder.MoveTo(context.Web.ServerRelativeUrl + "/ArchDocs/" + currentFilingSystemItem["DealName"].ToString());
    folder.Update();

    context.Load(folder);
    FileCollection files = folder.Files;
    FolderCollection folders = folder.Folders;

    context.Load(files, fs => fs.Include(f => f.ListItemAllFields));
    context.Load(folders, fs => fs.Include(f => f.ListItemAllFields));
    context.ExecuteQuery();

    var dealStageField = list.Fields.GetByInternalNameOrTitle("DealStage");
    context.Load(dealStageField);
    context.ExecuteQuery();

    var dealStageTaxField = context.CastTo<TaxonomyField>(field);
    var dealStageTermValue = currentFilingSystemItem["DealStage"] as TaxonomyFieldValue;

    foreach (File f in files)
    {
        ListItem item = f.ListItemAllFields;
        dealStageTaxField.SetFieldValueByValue(item, dealStageTermValue);
        item.Update();
        context.ExecuteQuery();
    }
    foreach (Folder fo in folders)
    {
        ListItem item = fo.ListItemAllFields;
        dealStageTaxField.SetFieldValueByValue(item, dealStageTermValue);
        item.Update();
        context.ExecuteQuery();
    }
}
| improve this answer | |
  • your code worked for me, thanks a lot for the help.. but do you know why my code did not work, although on paper it should.. – SharePoint TestDev Feb 26 at 16:47
  • 1
    The two big difficulties with setting a Taxonomy value, are making sure your value is stored in a TaxonomyFieldValue object -- which in your case should already be true since you are copying it from an existing item's field; but the second issue is you can't just set item["mytaxfield"] = the value even if it is already a TaxonomyFieldValue , you have to use the Field.SetFieldValueByValue(listItemTeBeUpdated, taxonomyFieldValueToSave). It was likely this second issue that was catching you. – willman Feb 26 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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