Getting Version Changes From SharePoint
SharePoint doesn’t provide a straightforward way to do this it seems. My first crack was to try to leverage the BeforeProperties and AfterProperties in an event receiver, but it turns out that this doesn’t work quite as you would expect based on the naming of these two properties (and the same goes for ChangedProperties).
However, clearly we can see that the version history contains the changes in the fields from one version to the next.
Here’s some basic code to identify the fields that changed from one version to the next:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using (SPSite site = new SPSite("http://my.domain.com/")) using (SPWeb web = site.OpenWeb()) { SPList list = web.GetList("http://my.domain.com/Lists/Stuff"); SPListItem item = list.Items[1]; Console.Out.WriteLine(item.Title); SPListItemVersion currentVersion = item.Versions[0]; SPListItemVersion previousVersion = item.Versions.Count > 1 ? item.Versions[1] : null; Console.Out.WriteLine("Version is current: {0}", currentVersion.IsCurrentVersion); foreach (SPField field in currentVersion.Fields) { if (field.ShowInVersionHistory == false) { continue; } if (previousVersion == null) { Console.Out.WriteLine(" > {0} changed to \"{1}\"", field.StaticName, currentVersion[field.StaticName]); continue; } if (currentVersion[field.StaticName].Equals(previousVersion[field.StaticName])) { continue; } Console.Out.WriteLine(" > {0} changed from \"{1}\" to \"{2}\"", field.StaticName, previousVersion[field.StaticName], currentVersion[field.StaticName]); } } |
This will get you the changes between the current version and the previous version. If it’s a new object, all of the changes will be returned.