1

I have the following code inside my remote event reciever, to create a new folder, and then populate the folder managed metadata fields with the current item (item that was added) values:-

ListItem listItem = context.Web.GetList(webrelativeurl +  "/"+i2["TemplateListUrl"]).AddItem(listItemCreationInformation);

listItem["FolderType"] = currentitem["FolderType"];
listItem["CorpType"] = currentitem["CorpType"];

now the FodlerType on both the current item and the listitem is a managed metadata column which allow single value only, while the CorpType allow multiple values. my above code will assign a value for the FolderType , but will raise this error when updating the CorpType:-

Microsoft.SharePoint.Client.ClientRequestException
HResult=0x80131500 Message=The object is not associated with an object identity or the object identity is invalid. Source= StackTrace:

any advice?

1

You need to get a TaxonomyFieldValueCollection use the SetFieldValueByValueCollection of the TaxonomyField object.

var field = item.ParentList.Fields.GetByInternalNameOrTitle("CorpType");
var corpTypeTaxField = context.CastTo<TaxonomyField>(field);
context.Load(item,i=>i[TaxKeywordFieldName]);

corpTypeTaxField.SetFieldValueByValueCollection(listItem, (TaxonomyFieldValueCollection) currentitem["CorpType"]);    

Note, this gets a lot messier if you have terms that you need to look up to construct a TaxonomyFieldValueCollection from scratch -- copying it from another item is definitely easier.

| improve this answer | |
  • thanks for the reply, but i can not find a method named "GetFieldValueByValueCollection".. there is a method named "GetFieldValueAsTaxonomyFieldValueCollection" – SharePoint TestDev Feb 17 at 23:28
  • i replaced GetFieldValueByValueCollection with GetFieldValueAsTaxonomyFieldValueCollection.. but i will get this error The given guid does not exist in the term store'.. any idea? – SharePoint TestDev Feb 18 at 0:29
  • Is your source item from a different site than your destination item? – willman Feb 18 at 0:39
  • the same site and the same list – SharePoint TestDev Feb 18 at 2:24
  • Can you debug and see what the value of currentitem["CorpType"].ToString() is? Is it possible that the current item is referencing a term that has been deleted? – willman Feb 18 at 2:28
0

You can modify the code as below.

List list = context.Web.GetList(webrelativeurl + "/" + i2["TemplateListUrl"]);

Field field = list.Fields.GetByInternalNameOrTitle("CorpType");
context.Load(field);
context.ExecuteQuery();

TaxonomyField txField = context.CastTo<TaxonomyField>(field);
TaxonomyFieldValueCollection termValues = null;
string termValueString = string.Empty;
termValues = currentitem["CorpType"] as TaxonomyFieldValueCollection;
foreach (TaxonomyFieldValue tv in termValues)
{
    termValueString += tv.WssId + ";#" + tv.Label + "|" + tv.TermGuid + ";#";
}
termValueString= termValueString.Remove(termValueString.Length - 2);
termValues = new TaxonomyFieldValueCollection(context, termValueString, txField);

ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem listItem = list.AddItem(itemCreateInfo);
listItem["FolderType"] = currentitem["FolderType"];
txField.SetFieldValueByValueCollection(listItem, termValues);
listItem.Update();
context.ExecuteQuery();
| improve this answer | |

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.