0

holds the multiple values of the taxonomy field. I have a string like 2;#Department 2|c98ce851-88fd-41bc-a013-4a69dd6bae66 and assigning it using PopulateFromLabelGuidPairs() but I am not able to see the term stored in it.

My code is:

TaxonomyFieldValueCollection newDepartments = null;
TaxonomyFieldValueCollection oldDepartments = properties.ListItem[FieldNames.Department] as TaxonomyFieldValueCollection;
string strNewDepartments = Convert.ToString(properties.AfterProperties[FieldNames.Department]);
if (!string.IsNullOrEmpty(strNewDepartments))
{
    TaxonomyField deptField = properties.List.Fields.GetFieldByInternalName(FieldNames.Department) as TaxonomyField;
    if (deptField != null)
    {
        newDepartments = new TaxonomyFieldValueCollection(string.Empty);
        newDepartments.PopulateFromLabelGuidPairs(strNewDepartments);
    }
}

I debugged the code and found the strNewDepartments = 2;#Department 2|c98ce851-88fd-41bc-a013-4a69dd6bae66 but when I watched newDepartments[0].Label it shows "" (an empty) value. I writing this code in ItemUpdating event receiver of a list that's why I took the string of the field.

What am I missing?

1

Thank you for answering Mr.ali Sharepoint.

I got the workaround and found the characters which caused issue.

There was an issue in the string i.e. it has three characters which are 2;# I removed that and it worked perfectly.

So, my code is:

TaxonomyField deptField = properties.List.Fields.GetFieldByInternalName(FieldNames.Department) as TaxonomyField;
if (deptField != null)
{
    newDepartments = new TaxonomyFieldValueCollection(string.Empty);
    string finalString = string.Empty; 
    string[] splitString = strNewDepartments.Split('#');
    if (splitString.Length > 0)
    {
        for (int i = 0; i < splitString.Length; i++)
        {
            if (splitString[i].IndexOf('|') > -1)
            {
                finalString += splitString[i];
            }
        }
    }
    newDepartments.PopulateFromLabelGuidPairs(finalString);
}

Hope this helps others too.

0

do it in the ItemUpdated event not ItemUpdating!

You can then use the properties before and after to update what you want as you were doing ;)

dont forget to update the item, also avoid never ending loop by setting:

this.EventFiringEnabled = false;

listitem.update();

this.EventFiringEnabled = true;

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.