Create a new alert for a SharePoint list

A code snippet for creating a new alert for a SharePoint list.  The OpenSite() method simply creates an SPSite object named which I’ve named _site.

Make sure that your alertUser has permissions to the list. Also you need to set the property AlwaysNotify = false.  For some reason it doesn’t seem to work if you don’t set this property.

public void CreateAlert(SPList list, SPUser alertUser, string alertTitle, bool notify)

        {

            OpenSite();

            try

            {

                SPWeb web = _site.AllWebs[list.ParentWebUrl];

                SPAlertCollection alerts = web.Alerts;

 

                // get the lists alert template and apply that to the new alert

                SPAlertTemplate alertTemplate = list.AlertTemplate;

                alertTemplate.Name = list.AlertTemplate.Name;

 

                // setup the alert

                SPAlert alert = alerts.Add();

                alert.AlertType = SPAlertType.List;

                alert.AlertTemplate = alertTemplate;

                alert.Title = alertTitle;

                alert.EventType = SPEventType.Add;

                alert.AlertFrequency = SPAlertFrequency.Immediate;

                // make sure to set this to false. Don't know why but doesn't seem to work if you don't.

                alert.AlwaysNotify = false;

                alert.User = alertUser;

                alert.List = list;

                alert.Update(notify);

                

                // cleanup

                web.Close();

                web.Dispose();

            }

            catch

            {

                Console.WriteLine("Unable to create alert");

            }

            CloseSite();

        }


2 Responses to “Create a new alert for a SharePoint list”

  • Pamela Willox Says:

    Excellent post Adam. I hope you can help me!
    I’ve created an item event handler so that when someone posts an item to a list, an alert is created on that item (if it’s the first post i.e fileSystemObjectType folder).
    It works well if it’s a site administrator creating the item, but when it’s a user with member permissions, I have a problem. When it tries to set the alert template name
    I get a permission denied error.

    I have tried to open the site with a user token to impersonate the site collection administrator but still having the same issue.
    Here is the method for the alert up to the point it fails:

    public void CreateAlert(SPWeb myWeb, SPList list, SPListItem item, string alertTitle, SPUser alertUser, bool notify)
    {
    try
    {
    SPUserToken siteAdminToken = GetUserToken(myWeb.Url.ToString(), “siteadmin”);
    using (SPSite site = new SPSite(myWeb.Site.ID, siteAdminToken))
    {
    SPWeb alertsWeb = site.OpenWeb(myWeb.ID);
    SPList alertList = alertsWeb.Lists[list.ID];
    SPListItem alertListItem = alertList.GetItemById(item.ID);
    SPAlertCollection alerts = alertsWeb.Alerts;

    // get the lists alert template and apply that to the new alert
    SPAlertTemplate alertTemplate = alertList.AlertTemplate;
    alertTemplate.Name = alertList.AlertTemplate.Name;

    }

    Can you shed any light?

    Regards,
    Pam

  • SharePoint 2007’s problems « Huynhvothinh’s Weblog Says:

    [...] -http://www.emptycache.com/blog/2008/04/17/create-a-new-alert-for-a-sharepoint-list/ [...]

Leave a Reply