WSS Practice: Creating and deleting Alerts

Continuing my WSS exam prep, I had a play with Alerts through the SharePoint Object Model…

First off, a couple of ways of listing the alerts on a site, or on a user:

For a site:

//*** Looking at the site, show the lists a user has alerts for
foreach (SPAlert s1 in site.Alerts)
{
Console.WriteLine("Alert: {0} for {1}", s1.ListUrl,s1.User.ToString());
}

For Each User:
//*** For each user, show what lists they have alerts for.
foreach (SPUser u in site.Users)
{
Console.WriteLine("User: {0}",u.ToString());
foreach ( SPAlert s2 in u.Alerts ) {
Console.WriteLine(" {0} : {1}", u,s2.ListUrl );
}
}

Next up, delete all the SPAlerts in a site:

List<Guid> guids = new List<Guid>();
foreach (SPAlert s3 in site.Alerts)
{
guids.Add(s3.ID);
}
foreach (Guid g in guids)
{
site.Alerts.Delete(g);
}

This might seem a bit excessively complicated – why not just loop through the alerts deleting them? Well, when I tried that I kept invalidating the SPAlertCollection – so I’d delete one alert (the first) and then get an exception. Instead, I decided to create a list of the GUIDs of all the alerts to be deleted, and then delete them afterwards.

Finally, creating a new Alert:

SPUser myUser = site.Users[@"VIRTUALAdministrator"];
SPAlert a = myUser.Alerts.Add();
a.Title = "Programmatic Alert";
a.EventType = SPEventType.Add;
a.AlertType = SPAlertType.List;
a.Status = SPAlertStatus.On;
a.AlertFrequency = SPAlertFrequency.Immediate;
a.List = site.Lists["MyList"];
a.Update();

One thing that I’d not realised was that you can create SPAlert objects with filters based on CAML, and a dynamic recipient. That’s quite a neat trick to know!

Advertisement
WSS Practice: Creating and deleting Alerts

8 thoughts on “WSS Practice: Creating and deleting Alerts

  1. Rather than scan the alert list twice, just use a while loop. This is also faster because it doesn’t create an SPAlert object you don’t really need.

    var alerts = site.Alerts;
    while(alerts.Count > 0)
    {
    alerts.Delete(0);
    }

  2. “Var”? Been workin’ in JavaScript lately? 🙂

    Yup, that’s a much more elegant way of actually doing the deletion. I’d actually initially been looking at deleting some alerts, but not all of them, and hence the ‘loop to find them then loop to delete them’. But for a general ‘delete everything’ your code is *much* tidier.

    Thanks!

  3. Hmm! Hadn’t realised that – I’m very much a C# 2.0 boy. Interesting – but I’m not sure I like implicit typing.

    Still, worth knowing.

    1. Yup, which is nice – but I just like being explicit about what that type is. I’m not worried about compilation errors, but rather clarity. I know it’s something of a matter of taste!

  4. Lito says:

    Hi,

    Just would like to ask, why the below code is not working:

    public override void FeatureActivated(SPFeatureReceiverProperties properties) {
    using (SPSite site = properties.Feature.Parent as SPSite)
    {
    site.AllowUnsafeUpdates = true;
    using (SPWeb web = site.RootWeb)
    {
    foreach (SPUser user in web.SiteUsers)
    {
    SPAlert alert = web.Alerts.Add();
    alert.AlertType = SPAlertType.List;
    alert.List = web.Lists[“Tasks”];
    //use the following codes to set the “eventtypeindex” property
    //all =0, added = 1, modify = 2, deleted = 3, web discussions = 4
    alert.Properties[“eventtypeindex”] = “1”;
    alert.AlertFrequency = SPAlertFrequency.Immediate;
    alert.List = list;
    //passing false to Update method will refrain from sending the alert confirmation mail
    alert.Update(false);
    }
    }
    }
    }

    Thanks,
    Lito

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.