Skip to content

Get notified by changes

takerusilt edited this page Aug 1, 2016 · 1 revision

To monitor changes within a SharePoint site it is sometimes messy and requires a bit of work, mainly because there is no push notifications and each change may in effect be broken down into several changes.

The library caters this by introducing the SPChangeMonitor class. For simple monitoring you can simply call SPChangeMonitor.CreateMonitor() with a list of monitor filter.

Each filter specifies the object type and change type of that object type you wish to monitor.

For example you want to monitor updates on list and list item you can pass

SPChangeMonitor monitor = SPChangeMonitor.CreateMonitor(siteId,
    new SPChangeMonitorFilter(SPChangeObjectType.List, SPChangeFlags.Update),
    new SPChangeMonitorFilter(SPChangeObjectType.Item, SPChangeFlags.Update));

Then you can listen to the monitor by

monitor.ObjectChanged += (sender, e) => {
   foreach (SPAggregatedChange change in e.Changes) {
       // iterate through changes
   }
};

Any changes for each unique item are aggregated and is represented in a bitmask value. Therefore within a time frame from the last event raised, each unique item will only have one entry so you can deal with the changes more easily.

However, individual changes are still accessibly in each SPAggregatedChange object for details.

Advanced filtering

If that is not enough, you can apply custom filtering by inheriting the SPChangeMonitor class.

You can add custom code to decide each SPChange object should trigger the monitor to raise notifications to subscribers. For example to filter changes on list item:

public override class CustomChangeMonitor : SPChangeMonitor {
    protected override bool ShouldNotify(SPChangeItem change) {
         return MyCustomLogic(change);
    }
}