User property of SPAlert object not set

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)
    {
        try
        {
             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);

Advertisements

Leave a comment

Filed under Microsoft Office Sharepoint Server 2007

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s