Customize alerts

SP2013

Hello everyone,

Today I will talk about a little subtlety in development which I faced recently.

My goal was to change the standard alert message from SharePoint based on custom requirement. For that I had to the code behind to do my test. So I ended up with the implementation of a class inheriting from IAlertNotifyHandler.

Small parenthesis, if you want to do the same, I recommend this article which explains clearly the operation (and I want to thank the author by the way).

http://blogs.msdn.com/b/sharepointdeveloperdocs/archive/2007/12/14/how-to-customizing-alert-emails-using-ialertnotificationhandler.aspx

After a first attempt, I’ve realized that the email received did not looks like the standard mail alert.

In fact, I did not send the entire “header” but just used the “To”. (Again thanks to a passage for the author of this article: http://blogs.msdn.com/b/malag/archive/2008/07/23/alert-emails-not-recognized-by-outlook-as-alert-emails.aspx

Thinking my troubles were ending, I’ve realized that something was also different : the sender’s name!

If we simply use “SPUtility.SendEmail (web, ahp.headers, ahp.body)“, the email address configured in the outgoing mail setup your SharePoint server will be the “sender”, and this is not what is sent by default alerts!

To have exactly the same in your email, you need to change the header “From” as follows.

ahp.headers [“From”] = web.Title + “<” + ahp.headers [“From”] + “>”;

Finally, two things:

1) If you want to test your alerts you have to start the job “Immediate Alerts” for your application via the central administration, “Central Administration -> Monitoring -> review job definition -> Immediate alerts (the one targeting your web application -> run now”. “This job is used for three types of alerts (Immediate, Daily and Weekly)

2) To test your Daily and Weekly alerts, you can easily change the date of mailing via the following PowerShell command

Add-Pssnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue

$web = Get-SPWeb “yourSiteUrl”

$web.Alerts | select Title,ID,AlertFrequency,User   # this will display every alerts of the web with their ID (used for the next part)

$alert = $web.Alerts | where {$_.ID -eq “PASTE GUID HERE”}

$date=Get-Date

$alert.AlertTime =$date;

$alert.Update();

 

Hope this helps!

 

Christopher

Advertisements
This entry was posted in SharePoint 2010, SharePoint 2013 and tagged , . Bookmark the permalink.

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