SharePoint 2016 Custom Alert not working

Highlighted
Occasional Visitor

Hi there,

 

I am trying to customize the Alert handler made by SharePoint 2016. The similiar way it was done in SP2013.

  1. created a copy of the alertstemplate.xml
  2. Added Custom Handler
  3. Added to the list i want the XML needed in the content type 
  4. Update alert template 

  5. Did TimerJob Restart

However they are not working any body knows why?

 

for your reference

public class ZoneAlertFixer : IAlertNotifyHandler
{

public bool OnNotification(SPAlertHandlerParams ahp)
{
try
{
SendFixedUrlEmail(ahp);
return true;
}
catch (Exception ex) {
return false;
}
}

public void SendCustomEmail(SPAlertHandlerParams ahp)
{
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 = "";
var 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";

 

 

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


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


string subject = list.Title.ToString();
SPUtility.SendEmail(web, true, false, ahp.headers["to"].ToString(), subject, build);
}

public void SendFixedUrlEmail(SPAlertHandlerParams ahp)
{
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;
}
string to = ahp.headers["to"].ToString();
string subject = ahp.headers["subject"].ToString();
SPUtility.SendEmail(spWeb, ahp.headers, ahp.body);
}

}
}
}

 

<NotificationHandlerAssembly>SampleAlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a3902394782e8e4e</NotificationHandlerAssembly><NotificationHandlerClassName>SampleAlertHandler.CustomAlertHandler</NotificationHandlerClassName>

 

stsadm -o updatealerttemplates -filename "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML\custom_alerttemplates.xml" -url "your site url"

0 Replies