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?

3
  • 1
    Check this. Maybe it will help you. Feb 17, 2020 at 16:47
  • @GaneshSanap thanks for the link,but it is not clear how to achieve this
    – John John
    Feb 17, 2020 at 16:51
  • hi, I think I posted an answer for the same question on Overflow :). Please check if it was of any help :) stackoverflow.com/questions/60266593/…
    – Adam
    Feb 17, 2020 at 21:26

2 Answers 2

2

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.

9
  • thanks for the reply, but i can not find a method named "GetFieldValueByValueCollection".. there is a method named "GetFieldValueAsTaxonomyFieldValueCollection"
    – John John
    Feb 17, 2020 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?
    – John John
    Feb 18, 2020 at 0:29
  • Is your source item from a different site than your destination item?
    – willman
    Feb 18, 2020 at 0:39
  • the same site and the same list
    – John John
    Feb 18, 2020 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, 2020 at 2:28
1

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();

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.