0

I've set the user alerts fine (I've even checked through the UI that they're there). But when it comes to accessing these Alerts the collection is always null:

I understand that the SPUser must be from the same web in both cases or it can cause a null collection - but they are from the same web.

My method to set the user alerts is:

    public void CreateUserListAlert(SPUser user, SPList list)
    {
        user.ParentWeb.AllowUnsafeUpdates = true;
        SPAlert alert = user.Alerts.Add();
        alert.AlertType = SPAlertType.List;
        alert.AlertFrequency = SPAlertFrequency.Immediate;
        alert.DeliveryChannels = SPAlertDeliveryChannels.Email;
        alert.List = list;
        alert.Update();
        user.ParentWeb.AllowUnsafeUpdates = false;
    }

And the one to retrieve the user alerts: (or attempt to retrieve!)

    public SPAlert GetUserListAlert(SPUser user, SPList list)
    {
        SPAlert alert = null;

        foreach (SPAlert userAlert in user.Alerts)
        {
            if (userAlert.ListID == list.ID)
            {
                alert = userAlert;
                break;
            }
        }

        return alert;
    }

The user that is passed into the latter is:

SPSite site = SPContext.Current.Site;
web = site.AllWebs[webId];
web.CurrentUser;

whereas the first method is passed SPContext.Current.Web.CurrentUser - but both webs are the same - it's just that for the 2nd method I'm not 'in' that web so I can't refer to the current web.

The foreach won't even iterate once - user.Alerts is empty.

Any ideas?

1 Answer 1

3

I ran into some issues with setting alerts on users. Try setting the alert in the web.alerts collection instead of the user.alerts collection.

You may also want to set the Alert Template programmatically, but I am not sure that is required here.

public void CreateUserListAlert(SPUser user, SPList list) 
{ 
    list.ParentWeb.AllowUnsafeUpdates = true; 


    SPAlert alert = list.parentweb.Alerts.Add();

    alert.AlertTemplate = list.AlertTemplate;
    alert.User = user

    alert.AlertType = SPAlertType.List; 
    alert.AlertFrequency = SPAlertFrequency.Immediate; 
    alert.DeliveryChannels = SPAlertDeliveryChannels.Email; 
    alert.List = list; 
    alert.Update(); 
    user.ParentWeb.AllowUnsafeUpdates = false; 
} 
5
  • You may have just solved a big problem of mine, I'll give this a go and mark accordingly. Thanks a LOT. Commented Oct 18, 2012 at 17:52
  • Ghangas - where is the user set here? It needs to be user specific. Just to confirm - would it be alert.User? I know this is pretty self explanatory but just wanted to confirm I'm not missing something. Commented Oct 29, 2012 at 12:54
  • 1
    I would think that it would be: alert.User = user; Also available is the string entry UserId alert.UserId = 1; I would do the top one because you can use the SPUser object. Trying to use the bottom one requires finding the user in the User Information List in the rootweb of the site you are working in then grabbing the Id number to populate with. Seems like a lot of extra work if you are coming into the method with an SPUser already.
    – ghangas
    Commented Oct 29, 2012 at 17:28
  • Thanks a lot Ghangas, that was a huge help! Problem solved. Commented Oct 30, 2012 at 8:33
  • 1
    I'm not sure that AllowUnsafeUpdates is required for this, just as an FYI. I just implemented something similar to this today and it works without it. AllowUnsafeUpdates is designed to permit DB writes during a GET operation, and I can't really imagine a scenario where you'd be setting an alert on a pageload, but whatever floats your boat. Commented Nov 1, 2012 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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