Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
0% accept rate? You need to accept some answers if you want people to help you - Check out the FAQ, top right. –  Ryan Aug 24 '11 at 8:26
    
I did just now! Thanks for reminding ;) –  SPNotSoNewbieNow Aug 24 '11 at 10:25
    
Can you explain how your alert is meant to take effect? I.e., does it only apply to only a named list, or does apply to all lists of a given type (document libraries) in the site? Is it supposed to apply to new lists of that type as well? –  Dave Wise Aug 24 '11 at 15:45
    
@Dave: What I was trying to achieve is to have a notification counter. How many new postings were done, new announcements, new discussions, etc. (all alerts set by the user). This counter will then be saved as a list in "MySite" for that particular user. This will be used in a web part that will display all alert notifications. The web part will be something like this: NEW Alerts!! 1 Discussion(s) 1 Announcement(s) –  SPNotSoNewbieNow Aug 28 '11 at 5:24

1 Answer 1

up vote 10 down vote accepted

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

That's it!

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);
share|improve this answer
    
Is SPAlertTemplateCollection.InitAlertTemplatesFromFile() equivalent to this command: stsadm -o updatealerttemplates -filename "..." -url "..."? The linked article uses this to actually run custom code to generate e-mail alerts, not just register a new template file. –  MgSam Oct 17 '12 at 19:51
    
Hey there I'm having the same issue, and I'm trying your way but it doesn't seem to access my xml file, all I got is an error saying :access denied Microsoft.SharePoint.SPAlertTemplateCollection.CreateOrUpdateAlertTemplates(Str‌​ing name, String xml) any ideas ? –  Glolita Apr 3 '13 at 9:59
    
Can you provide some more details -- like the line of code that is generating the exception, and the relevant lines from your SharePoint logs? Note: SharePoint often misleadingly displays an "access denied" page in the browser even when errors occur that have nothing to do with security! On the other hand though, if the "Access denied" message is appearing in your log files, then I'd say with certainty that it realy is a permissions issue. If all else fails, try elevate privelages or run the code as a different user (i.e. using impersonation or your farm admin accout). –  Nick Larter Apr 4 '13 at 7:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.