Use Alert Framework when sending notifications in your SharePoint custom app

Here is the scenario (applicable to both WSS and MOSS): I have created an application that sends out an alert once user fills out a form (made as a custom webpart). My webpart has few properties that allow admin to specify how and to whom the message will be sent. I use System.Net.Mail and mail server name is grabbed from Central Admin (no hardcoded values, everything pulled). During my testing – in some cases no message would be sent despite all conditions true. Faulty logic? Think again.

Here is the problem: In my case I have connected to default mail server specified in Central Admin and populated all of the necessary parameters; however, I used CmtpClient.Send to send my message. Coincidentially – the connection to the server got lost – infrastructure guys performed 15 min maintenance and my application ignored the exception. Even if it wouldn`t have ignored it – the event has passed and message is lost.

The correct approach: rather than creating complex logic to save non delivered messages – use alert framework in a list. Create a list that will handle your alerts, whether for admin reporting or actual business alerts, the framework will attempt later delivery when connectivity or any other issues occur. This applies to your custom workflows, event handlers and even application pages attempting to send mail.

Here is the most generic alert creation routine:

SPUser user = mySite.Users["domain\username"];
SPAlert newAlert = user.Alerts.Add();
newAlert.Title = list.Title;
newAlert.AlertType = SPAlertType.List;
newAlert.List = list;
newAlert.Properties["eventtypeindex"] = “0″; // all changes
newAlert.AlertFrequency = SPAlertFrequency.Immediate;
newAlert.Update(false); // false to suppress `you have subscribed ` email

Here is a good post on more options when sending out an alert:
http://www.sharepointology.com/development/how-to-create-alerts-programmatically/

This entry was posted in MOSS, sharepoint, wss and tagged , , , . Bookmark the permalink.

Comments are closed.