Guest User

SharePoint Event Receiver IsFieldChanged

a guest
Jun 1st, 2011
555
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class EventReceiverExtensions
  2. {
  3.     static System.Text.RegularExpressions.Regex tagRegex
  4.                 = new System.Text.RegularExpressions.Regex("<[^>]*>", System.Text.RegularExpressions.RegexOptions.Compiled);
  5.  
  6.     public static bool IsFieldChanged(this SPItemEventProperties properties, string fieldName)
  7.     {
  8.         return properties.IsFieldChanged(properties.List.Fields.GetFieldByInternalName(fieldName));
  9.     }
  10.  
  11.     public static bool IsFieldChanged(this SPItemEventProperties properties, SPField field)
  12.     {
  13.         var after = (string)properties.AfterProperties[field.InternalName];            
  14.         var before = Convert.ToString(properties.ListItem[field.Id]);
  15.            
  16.  
  17.         //AfterProperties[fieldname] == null - field not changed
  18.         if (after == null)
  19.         {
  20.             return false;
  21.         }
  22.  
  23.         //AfterProperties[fieldname] == "" - field set to null
  24.         if (after == "" && string.IsNullOrEmpty(before))
  25.         {
  26.             return false;
  27.         }
  28.  
  29.         //AfterProperties[fieldname] != "", old value is null or empty - field changed
  30.         if (string.IsNullOrEmpty(before))
  31.         {
  32.             return true;
  33.         }
  34.  
  35.         var afterValue = field.GetFieldValue(after);
  36.         var beforeValue = field.GetFieldValue(before);
  37.  
  38.         if (afterValue.Equals(beforeValue))
  39.         {
  40.             return false;
  41.         }
  42.  
  43.         //Compare SPFieldLookupValue and SPFieldUserValue
  44.         if (afterValue is SPFieldLookupValue)
  45.         {
  46.             return (afterValue as SPFieldLookupValue).LookupId != (beforeValue as SPFieldLookupValue).LookupId;
  47.         }
  48.  
  49.         //Compare SPFieldMultiChoiceValue
  50.         if (afterValue is SPFieldMultiChoiceValue)
  51.         {
  52.             return !EqualChoices(afterValue as SPFieldMultiChoiceValue, beforeValue as SPFieldMultiChoiceValue);
  53.         }
  54.  
  55.         //Compare strings
  56.         if (afterValue is string)
  57.         {
  58.             //normalize tags on rich edit fields
  59.             if (field is SPFieldMultiLineText && (field as SPFieldMultiLineText).RichText)
  60.             {
  61.                 var a = tagRegex.Replace(afterValue as string, m => m.Value.ToLower());
  62.                 var b = tagRegex.Replace(beforeValue as string, m => m.Value.ToLower());
  63.                 return !a.Equals(b);
  64.             }
  65.         }
  66.            
  67.         //Compare SPFieldLookupValueCollection and SPFieldUserValueCollection
  68.         if (field is SPFieldLookup && (field as SPFieldLookup).AllowMultipleValues)
  69.         {
  70.             var hsa = new HashSet<int>((afterValue as SPFieldLookupValueCollection).OfType<SPFieldLookupValue>().Select(l => l.LookupId));
  71.             var hsb = new HashSet<int>((beforeValue as SPFieldLookupValueCollection).OfType<SPFieldLookupValue>().Select(l => l.LookupId));
  72.             return !hsa.SetEquals(hsb);
  73.         }
  74.  
  75.         return  after != before;
  76.     }
  77.     private static bool EqualChoices(SPFieldMultiChoiceValue a, SPFieldMultiChoiceValue b)
  78.     {
  79.         if (a.Count != b.Count) return false;
  80.  
  81.         var hsa = new HashSet<string>();
  82.         var hsb = new HashSet<string>();
  83.         for (int i = 0; i < a.Count; i++)
  84.         {
  85.             hsa.Add(a[i]);
  86.             hsb.Add(b[i]);
  87.         }
  88.  
  89.         return hsa.SetEquals(hsb);
  90.     }
  91. }
RAW Paste Data