I have been working on a mechanism to notify users of changes to lists. Why not use the OOTB Alerts feature I heard you ask? this notification is meant to be an alternative to receiving e-Mail alerts and both methods would be available to users.
Anyway I’ve built the basis of the solution around the concept of using a custom built alert handler and templates as described on the SharePoint Team Blog and leveraging the example provided in the SPAlertPipeline project in CodePlex. It all works nicely until I attempt to determine the user that the alert I’m processing has been sent to. For the alternative notification I’m interested in the login of the user not the email address and the User property on the SPAlert object passed to the handler should be able to provide it e.g.
public class AlertHandler:IAlertNotifyHandler
public bool OnNotification(SPAlertHandlerParams ahp)
SPUser alertUser = ahp.alert.User;
Instead when this line is hit I receive a null reference exception! There is little or no information around that others have hit this problem and the MSDN documentation continues to indicate that it is available and works.
The only work around I currently have requires a couple more lines of code but at least provides me with the information I’m looking for. Here it is…
SPSite site = new SPSite(ahp.siteUrl + ahp.webUrl);
SPWeb web = site.OpenWeb();
SPUser alertUser = web.AllUsers.GetByID(ahp.alert.UserId);