4

I have a managed metadata column which is updated using the code below. If I pass in a List it updates. If I pass in a document, no error and no update.

(I tried using SPItem instead of SPListItem, from Googling)

string newval = "Charleston|ec12fde0-5e10-4308-9640-e0ab9e1b5c34,Charlotte|2f1cfc1f-538f-4728-a71e-a5a1c8558011,Columbia|38c5ca3e-a26e-4eae-a01e-60e2d0caa926";
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
   using (SPWeb web = site.OpenWeb())
    {
       web.AllowUnsafeUpdates = true;
       SPList olist = web.Lists[list];
       SPListItem iteml = olist.GetItemById(itemid);
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
           var cityList = newval.Replace(",", ";");
           var newList = new Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection(string.Empty);
           newList.PopulateFromLabelGuidPairs(cityList);
           iteml["Cities"] = newList ;
           iteml.Update();
        });
        web.AllowUnsafeUpdates = false;
    }
}
  • Do you have content types associated with the library? – Amal Hashim Sep 1 '16 at 21:49
  • SPItem is an abstract class. You wouldn't use this in the code you shared. – Erin L Sep 1 '16 at 22:20
  • Do you mean that you're passing in a library? Passing a list isn't comparable to passing a single document. – Erin L Sep 1 '16 at 22:23
  • The list is created from document with managed metadata columns added. The original add uploads the file, adds values to the MMD fields, all good. Then I get the list item, modify the MMD values, and update. No error. for Item, updates correctly. For document, returns no error, but no update. I do not want to modify the document, just the MMD values – Mkowa Sep 2 '16 at 13:09
  • Yes, there is Content Type. – Mkowa Sep 2 '16 at 13:11
3
+50

Solved:

string newval = "Charleston|ec12fde0-5e10-4308-9640-e0ab9e1b5c34,Charlotte|2f1cfc1f-538f-4728-a71e-a5a1c8558011,Columbia|38c5ca3e-a26e-4eae-a01e-60e2d0caa926";
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
   using (SPWeb web = site.OpenWeb())
    {
       web.AllowUnsafeUpdates = true;
       SPList olist = web.Lists[list];
       SPListItem iteml = olist.GetItemById(itemid);
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
           var cityList = newval.Replace(",", ";");
           var newList = new Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection(string.Empty);
           newList.PopulateFromLabelGuidPairs(cityList);
           TaxonomyField taxCityField = (TaxonomyField)iteml.Fields.GetFieldByInternalName("Cities");
           taxCityField.SetFieldValue(iteml, newList);
           taxCityField.Update(); 
           iteml.Update();
        });
        web.AllowUnsafeUpdates = false;
    }
}

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.