Friday, December 5, 2008

How to create a custom e-mail alert handler in Microsoft Office SharePoint Server

This method creates a class that inherits from the IAlertNotificationHandler interface and that uses the OnNotification method. This method lets you intercept the outgoing e-mail alerts and modify them. You can access most of the properties of the alert. By using XML parsing and SharePoint object model code, you can extract all the information that you must have to modify the e-mail alert. Then, you can build the HTML stub to display the e-mail alert based on your requirements. Also, you can send the e-mail alert by using SharePoint̢۪s SendMail functionality.

public class Class1:IAlertNotifyHandler
{

#region IAlertNotifyHandler Members

public bool OnNotification(SPAlertHandlerParams ahp)
{
try
{
SPSite site = new SPSite(ahp.siteUrl+ahp.webUrl);
SPWeb web = site.OpenWeb();
SPList list=web.Lists[ahp.a.ListID];
SPListItem item = list.GetItemById(ahp.eventData[0].itemId) ;

string FullPath=HttpUtility.UrlPathEncode(ahp.siteUrl+""+ahp.webUrl+""+list.Title+""+item.Name);
string ListPath = HttpUtility.UrlPathEncode(ahp.siteUrl + "" + ahp.webUrl + "" + list.Title);
string webPath=HttpUtility.UrlPathEncode(ahp.siteUrl+""+ahp.webUrl);

string build = "";
if (ahp.eventData[0].eventType==1)
eventType="Added";
else if(ahp.eventData[0].eventType==2)
eventType="Changed";
else if(ahp.eventData[0].eventType==3)
eventType="Deleted";

build = ""+
"

"+ item.Name.ToString() +" has been "+eventType +"

"+
""+
""+
"" +
"
"+
"Modify my Settings
View "+item.Name+"View " + list.Title + "
";
string subject=list.Title.ToString() ;
SPUtility.SendEmail(web,true , false, ahp.headers["to"].ToString(), subject,build);
return false;
}
catch (System.Exception ex)
{
return false;
}
}

#endregion
}

You can put your questions/Suggestions in the comment section, I w ill try to respond as soon as possible.

Thanks,
Sanjay

2 comments:

kuk said...

the event number for Delete = 3 is it correct i dont get delete alerts when i tried to modify my alerts i get alerts for changed and added......

kuk said...

i tried debugging with owstimer.exe
it executes the dll i mean code in other events but in case of deleted items it says::
A first chance exception of type 'System.ArgumentException' occurred in Microsoft.SharePoint.dll
The program '[6592] OWSTIMER.EXE: Managed' has exited with code 0 (0x0).
The program '[6592] OWSTIMER.EXE: T-SQL' has exited with code 0 (0x0).

Any Idea Why it is Happening