Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;
        }
share|improve this question

2 Answers 2

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"])
share|improve this answer
up vote 0 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.