SharePoint 2013 EMail Alerts with wrong URLs under extended zones

I had to make a fix to the problem described in the title above. Basically the issue is that if you have an extended zone and you are initiating an alert from you SharePoint environment in the E-Mail.

There is the following solution which works perfectly:

https://timpanariuovidiu.wordpress.com/2014/06/23/sharepoint-2013-alerts/

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

The sample above works just fine. I did had to make an addition to the code because it was not taking into consideration the headers and the wrong URL there. Here is my version. It is rather similar:



public class CustomAlertHandler : IAlertNotifyHandler
 {
 public bool OnNotification(SPAlertHandlerParams ahp)
 {
 try
 {
 using (SPSite site = new SPSite(ahp.siteUrl + ahp.webUrl))
 {
 using (SPWeb spWeb = site.OpenWeb())
 {
 SPAlert a = ahp.a;

 if (a.Properties.ContainsKey("siteurl"))
 {
 string siteNewURL = a.Properties["siteurl"];

 string siteOldURL = ahp.siteUrl;

 ahp.body = ahp.body.Replace(siteOldURL, siteNewURL);

 ahp.siteUrl = siteNewURL;
// My Addition for the header URL
 if (ahp.headers["x-sharing-config-url"] != null && !String.IsNullOrEmpty(siteNewURL) && !String.IsNullOrEmpty(siteOldURL))
 ahp.headers["x-sharing-config-url"] = ahp.headers["x-sharing-config-url"].ToLower().Replace(System.Web.HttpUtility.UrlEncode(siteOldURL).Replace(".", "%2e"), System.Web.HttpUtility.UrlEncode(siteNewURL).Replace(".", "%2E"));
 }

 string to = ahp.headers["to"].ToString();

 string subject = ahp.headers["subject"].ToString();

 SPUtility.SendEmail(spWeb, ahp.headers, ahp.body);
 }
 }
 return true;
 }
 catch (Exception ex)
 {
 return false;
 }
 }
 }


Additionally to the code above which needs to be deployed from a solution package you also need to create a custom alert template based on the SharePoint alerttemplates.xml in C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\XML.

In the XML within the properties tags you needs to add your assemble reference:

<NotificationHandlerAssembly>AlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>

<NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>

<NotificationHandlerProperties></NotificationHandlerProperties>

Then do the following steps:

Run the following command next:
stsadm -o updatealerttemplates -filename “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\XML\customalerttemplates.xml” -url “Your site URL”

Run iisreset on each server of the farm.

Restart the SharePoint timer services on each server in the farm.

Check out the links above for more info on how to create a custom alert.

Advertisements

2 thoughts on “SharePoint 2013 EMail Alerts with wrong URLs under extended zones

  1. Where do you deploy the CustomAlertHandler.cs file? Thanks!

    • Make a solution package which is deployable to the global assembly cache. Just copy the code in that Visual Studio project and compile and package the SharePoint solution. Then simply install it and follow the other instructions in the post regarding installing the alert from the command line application.

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