0

I have a managed metadata column in a list. With values in English: Brussel in french: Bruxelles.

I need to compare in the ItemUpdating event the before and after properties. I know that the before cant be used because it will return null in the item updating so I have to use the properties.ListItem.

If the user uses the UI in english, then the following code works fine, because the terms are the same.

However if the user selects french, then this wont work. because the afterproperties will be Bruxelles

private void ValidateAssignmentDate(SPItemEventProperties properties, SPListItem item)
        {
            string currentBudgetSection = properties.ListItem["BudgetSection"] == null ? string.Empty : properties.ListItem.GetTaxonomyFieldValue("BudgetSection").ValidatedString.ToString();
            string newBudgetSection = properties.AfterProperties["BudgetSection"].ToString();
            bool budgetSectionSame = newBudgetSection.Equals(currentBudgetSection);

            if(!budgetSectionSame))      
            {

//dosomething

The extension method is: (I cant change the extension method)

public static TaxonomyFieldValue GetTaxonomyFieldValue(this SPListItem item, string fieldName)
        {

            TaxonomyFieldValue returnValue = null;
            try
            {
                TaxonomyField taxonomyField = GetTaxonomyField(item, fieldName);
                if (taxonomyField != null && taxonomyField.Id != null)
                    returnValue = item[taxonomyField.Id] as TaxonomyFieldValue;
            }
            catch (Exception ex)
            {                   
                throw;
            }               
            return returnValue;
        }

2 Answers 2

0

I fixed it like this.

string currentBudgetSection = properties.ListItem["BudgetSection"] == null ? string.Empty : properties.ListItem.GetTaxonomyFieldValueByLanguage(item.Web.Site, "BudgetSection", Thread.CurrentThread.CurrentUICulture.LCID).ToString();
            string newBudgetSection=string.Empty ;
            if (properties.AfterProperties["BudgetSection"] != null && !string.IsNullOrEmpty(properties.AfterProperties["BudgetSection"].ToString()))
            {
                 int startIndex = properties.AfterProperties["BudgetSection"].ToString().IndexOf("#")+1;
                 int endIndex = properties.AfterProperties["BudgetSection"].ToString().IndexOf("|");
                 int length = endIndex - startIndex;
                 newBudgetSection = properties.AfterProperties["BudgetSection"] == null ? string.Empty : properties.AfterProperties["BudgetSection"].ToString().Substring(startIndex, length);
            }


 bool budgetSectionSame = newBudgetSection.Equals(currentBudgetSection);
            if((!budgetSectionSame )
//do something
0

I stumbled across this issue as well. Though your solution works, there is an easier way of solving it.

In ItemUpdating the value of a Managed Metadata field in the AfterProperties is a string. So instead of doing what you have done, you can just create a new instance of the TaxonomyFieldValue.

TaxonomyFieldValue newValue = new TaxonomyFieldValue((string)properties.AfterProperties["CustomManagedMetadataField"])

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.