Wednesday, September 24, 2008

Custom alert in SharePoint

We all know one of the very good features in Moss is Alerts.

And default alert message is also very good.

But as usual we all need to create a custom alerts mail in our SharePoint system.
We may need some style change, header/footer/logo change and also some business logic before sending alert mail.

So here is the way to change default alert mail in SharePoint.

We had this requirement a long time ago but we are not able to do it but suddenly we came across the KB on Microsoft Support site which explain step by step how to do that. Here is the link for KB 948321.

We tried that code and it worked like a charm.
Here is the same code but with some logical comment added by us just to understand the things.
But we recommended that follow the steps from the same link above(KB 948321)
using System;
using System.Text;
using System.Web;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;


namespace SharePointKings.CustomWebParts
{
public class CustomAlert : IAlertNotifyHandler
{
#region IAlertNotifyHandler Members

public bool OnNotification(SPAlertHandlerParams ahp)
{
try
{
//find the site
SPSite site = new SPSite(ahp.siteUrl + ahp.webUrl);
//open the web
SPWeb web = site.OpenWeb();
//get the list that has been changed(modified)
SPList list = web.Lists[ahp.a.ListID];
//get the item that has been changed
SPListItem item = list.GetItemById(ahp.eventData[0].itemId);


//path to reach to that item
string FullPath = HttpUtility.UrlPathEncode(ahp.siteUrl + "/" + ahp.webUrl + "/" + list.Title + "/" + item.Name);
//path to reach to that list
string ListPath = HttpUtility.UrlPathEncode(ahp.siteUrl + "/" + ahp.webUrl + "/" + list.Title);
//path to reach to that web
string webPath = HttpUtility.UrlPathEncode(ahp.siteUrl + "/" + ahp.webUrl);

string build = "";

//eventType string will get that item is Added/Changed or deleted
string eventType = string.Empty;

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";

//this way you can build your email body
//also you can apply the bussiness logic that which field to show
//change your highlighted words every thing you can do
build = "<style type=\"text/css\">.style1 { font-size: small; border: 1px solid #000000;" +
"background-color: #DEE7FE;}.style2 { border: 1px solid #000000;}</style></head>" +
"<p><strong>" + item.Name.ToString() + "</strong> has been " + eventType + "</p>" +
"<br> this is test by <strong>SharepointKings</strong><br> looks nice" +
"<table style=\"width: 100%\" class=\"style2\"><tr><td style=\"width: 25%\" class=\"style1\">" +
"<a href=" + webPath + "/_layouts/mysubs.aspx>Modify my Settings</a></td>" +
"<td style=\"width: 25%\" class=\"style1\"> <a href=" + FullPath + ">View " + item.Name + "</a></td>" +
"<td style=\"width: 25%\" class=\"style1\"><a href=" + ListPath + ">View " + list.Title + "</a></td>" +
" </tr></table>";

//title of the email
string subject = list.Title.ToString();
//over here sending mail is done by SPUtility so check the configuration
//of central admin for outgoing email.
SPUtility.SendEmail(web, true, false, ahp.headers["to"].ToString(), subject, build);
//we don't know why but we as per KB we have to return false.
return false;
}
catch (System.Exception ex)
{
return false;
}
}

#endregion
}
}


After completion of code follow the steps for register these codes in your site, again go to for KB 948321

Here is the overview what to do, this is not the steps to follow just an over view.

For that you need to generate copy of alertTemplates.xml file
Note: Do not directly modify the alertTemplates.xml file. Directly modifying this file is unsupported.Change the Properties tag in that xml with your solution’s assembly.
And use
stsadm -o updatealerttemplates <<with mentioned parameters>>
And restart the Windows SharePoint Services Timer service.

You done it man!!!

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com