Skip to content

Handling events

takerusilt edited this page Aug 3, 2016 · 2 revisions

Events to the list item represented by models can be listened on the model classes.

Note: The instance of the model class which each time event being raised is a separate object even though you are creating, updating or deleting the model itself.

public class Announcement : SPModel {
    protected override void OnUpdating(SPModelEventArgs e) {
        // your handling
    }
}

Supported events

The following events are available on model classes:

List item event Model event Async Model event
Adding OnAdding
OnAddingOrUpdating
-
Added OnAdded
OnAddedOrUpdated
OnAddedAsync
OnAddedOrUpdatedAsync
Updating OnUpdating
OnAddingOrUpdating
-
Updated OnUpdated
OnAddedOrUpdated
OnUpdatedAsync
OnAddedOrUpdatedAsync
Updating (Updating to major version) OnPublishing -
Updated (Updated to major version) OnPublished -
Deleting OnDeleting -
Deleted OnDeleted -

Basically a built-in list item event receiver is attached to the list that:

  • Events on the same list are already dispatched to different handling model classes
  • Able to access to the deleted list item even on Deleted event
  • Special handling to Adding and Added events for documents
  • Detect updates that are recognizable as Publishing and Published (commonly requires different handling)

Accessing old values

Old values can be accessed inside Updating event via the SPModelEventArgs.OriginalValue property.

public class Announcement : SPModel {
    protected override void OnUpdating(SPModelEventArgs e) {
        if (this.Description != ((Announcement)e.OriginalValue).Description) {
             // do something
        }
    }
}

Note: The property is returning an SPModel instance. As the content type of a list item can be changed during an update, be careful when you cast the instance if it is likely that the content type will be changed by users.