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'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?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

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; 
} 
share|improve this answer
    
You may have just solved a big problem of mine, I'll give this a go and mark accordingly. Thanks a LOT. –  DeeMac Oct 18 '12 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. –  DeeMac Oct 29 '12 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 Oct 29 '12 at 17:28
    
Thanks a lot Ghangas, that was a huge help! Problem solved. –  DeeMac Oct 30 '12 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. –  Drew Lanclos Nov 1 '12 at 21:07
add comment

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.