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?