Print Shortlink

Creating custom list alert templates in SharePoint 2010

SharePoint 2010 allows you to create custom alert templates per list, meaning you can customize what’s going to show up in your SPList alert body.

In this example I won’t focus how you can actually edit the alert CAML – you can figure it out based on existing list of templates, let`s see what`s involved in creating a new alert template and assigning it to the list:

1. Create a new Visual Studio 2010 SharePoint solution.

2. Map a TEMPLATES folder in your solution directory and create a new folder underneath it called XML. This XML folder will contain an XML file that will define your alerts.

3. Create a new file in sub folder (project specific name) inside XML folder in your solution and add XML File into a sub folder.

4. Navigate to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML on your file system and open alerttemplates.xmlthis is out-of-the-box alert definition that you should not modify. Take a loot at the structure of the file and how each alert is defined.

5. Copy alert definition of the list type that closely resembles what you want your new alert to look like ad paste it into your solutions custom alert XML file. If we were to copy generic list template – everything including the XML below would have to be copied:

<AlertTemplate Type="List" 
Name="SPAlertTemplateType.GenericList">
</AlertTemplate>

6. Make any adjustments to the custom alert CAML (this is something you will have to figure out based your needs).

7. Create a new site scoped feature which will add our custom alert template to the list of your choice and not to any other on the site.

8. Add a receiver to the feature and in FeatureActivated method place the following code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite site = properties.Feature.Parent as SPSite;
    XmlDocument xmlDoc = new XmlDocument();
    // The path to your alert templates
    xmlDoc.Load(SPUtility.GetGenericSetupPath(@"XML\YourProject")
+ "YourAlertTemplates.xml");
    // The list you want to have custom alert template
    SPList list = site..Lists["YourList"];
    SPAlertTemplate template = new SPAlertTemplate();
    template.Xml = xmlDoc.InnerXml;
    template.Name = "YourAlertTemplate";
    list.AlertTemplate = template;
    list.ParentWeb.AllowUnsafeUpdates = true;
    list.Update();
}

Above is going to take care of loading the template and assigning it to the list.

Now when this code is executed and template loaded to the list – new alert that are triggered with the template are going to come with the definition you have specified in your custom alert template file.

Good Luck!