In my previous post on how to customise Alerts, we could see how to change the html of an alert. In this post, I will show how to change the filter for an alert, which defines the conditions in which events cause and alert to trigger and an email sent.
Say for example we have a list with a field called “Assigned To”, and we want know when someone (other than yourself) changes the assignment on that list item.
If we open the custom alert XML file we created in the 12 Hive at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML\alerttemplates.xml, and search for our customised AlertTemplate node, we will find a section called Filters. Inside that node is a few different default FilterDefinitions, which specify the conditions for an alert to fire.
So we can create our own, give it a Friendly Name and Short Name, and define the conditions in a CAML query in the Query section. You can check compare the old and new values of an item using the field_name/Old and field_name/New structure. See the picture below for an example.
Save and register the alert using
stsadm -o updatealerttemplates -url http://yoursite/sites/sitecollname -filename “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML\alerttemplatesCustom.xml”
And set the Alert Template on the list by
SPList spList = null;spList = spWeb.Lists[listName];
SPAlertTemplate newTemplate = new SPAlertTemplate();
newTemplate.Name = “SPAlertTemplateType.MyCustomAlertType“;
spList.AlertTemplate = newTemplate;
Now when you create an alert through the UI you will see your new filter appearing in the alert options.