I'm reading "How to create a custom e-mail alert handler in Microsoft Office SharePoint Server" right now and as follow the how-to-guide, I found out that every time you need to modify alerts you have to go through the whole process again (from registering the dll to the GAC to restarting the Windows SharePoint Services Timer service). I was wondering if you can create a feature to do all this. When the feature is activated the custom alert template file will be used. If the feature is deactivated, it will revert to the original file. Is this possible? Can somebody show me how to do this?
I have achieved this with my custom alerts using nothing but a feature and a feature reciever.
NB: Upgrading your solution package using STSADM (or deploying directly from Visual Studio 2010) will automatically upgrade all your assemblies and provisioned files and recycle your app pool, so all you need to do is connect your custom alert tempates. Here's how:
Step 1: Inside your Visual Studio solution, right click your project and select "Add -> SharePoint Mapped Folder" then select the "TEMPLATE\XML" folder. Place your custom alert template files in here (they should be XML files).
That takes care of deploying your files to the SharePoint root, now you need to instruct SharePoint to parse these files when your feature activates....
Step 2: Inside your feature receiver, just add the following code:
SPAlertTemplateCollection alertTemplates = new SPAlertTemplateCollection((SPWebService)(siteCollection.WebApplication.Parent)); alertTemplates.InitAlertTemplatesFromFile(SPUtility.GetGenericSetupPath("TEMPLATE\\XML\\") + "CustomAlertTemplate.xml"); alertTemplates.InitAlertTemplatesFromFile(SPUtility.GetGenericSetupPath("TEMPLATE\\XML\\") + "CustomAlertTemplate_SMS.xml");
These custom alert templates will now be available to use anywhere within your application using the following code:
// Retrieve your custom alert template. SPAlertTemplateCollection alertTemplates = new SPAlertTemplateCollection((SPWebService)(listItem.ParentList.ParentWeb.Site.WebApplication.Parent)); SPAlertTemplate customAlertTemplate = alertTemplates["CustomAlertTemplateName"]; if (customAlertTemplate == null) throw new ApplicationException("The alert template \"CustomAlertTemplateName\" has not been registered."); // Create the alert without sending a confirmation email. SPAlert newAlert = listItem.ParentList.ParentWeb.CurrentUser.Alerts.Add(); newAlert.Title = "Title"; newAlert.AlertTemplate = customAlertTemplate; newAlert.Update(false);