This code adds an alert for all users in a site, with a filter: The task has to be assigned to the user, the status has to be Completed and the Start date has to be less than "today". In other words: the active tasks :-)
foreach (SPUser user in newWeb.SiteUsers)
{
SPAlert alert = newWeb.Alerts.Add();
alert.Filter = "<Query><And><And><Eq><FieldRef Name=\"AssignedTo\"/><Value type=\"string\">" + user.LoginName + "</Value></Eq><Neq><FieldRef Name=\"Status\"/><Value type=\"string\">completed</Value></Neq></And><Leq><FieldRef Name=\"StartDate\"/><Value type=\"datetime\"><Today/></Value></Leq></And></Query>";
alert.User = user;
alert.AlertType = SPAlertType.List;
alert.List = newWeb.Lists["Tasks"];
alert.EventType = SPEventType.Modify;
alert.AlertFrequency = SPAlertFrequency.Immediate;
alert.Update();
}
11 comments:
hi koen,
can you tell me where you will add this code..one option is in feature but is there anyway you can add it without feature..
Hi Jakey
You can use this code where you want - You just need a reference to the Sharepoint object model (Microsoft.SharePoint.dll).
You could write a console app or a windows app to manage the alerts..
Hi knrs,
I am writting the same code,but 2 diffrences are:
1. I am not putting foreach loop, i am creating alert for a single user,
2. My filter query has just one condition and it has one where and one eq tag inside this. (query works fine in an query analyzer. i am getting exception at alert.update(), exception: fiter can not be parsed.
can u help me on this...!
one more thing, if i comment alert.update(); it works, create filter but do not send emails about alert created or new item added.
sonal,
can you please post your filter?
thanks!
My problem is that I am iterating a lisbtox populated by uses, and using this code to asign the alert to each user-item of the listbox, I don't know whay all alerts are asigned always to the same user. by debugging, I see that the user is right before creating the alert, but after the alert has been created, I can see that it has assigned to a different user that is always the same (the administrator). Anyone has an idea? thank you.
My problem is that I am iterating a lisbtox populated by uses, and using this code to asign the alert to each user-item of the listbox, I don't know whay all alerts are asigned always to the same user. by debugging, I see that the user is right before creating the alert, but after the alert has been created, I can see that it has assigned to a different user that is always the same (the administrator). Anyone has an idea? thank you.
I have integrated your code for creating alert for a list for a particular user. I want to give user a functionality for removing the alerts. How is it possible programmatically ?
Thanks! - Just what I was looking for...
Thats an excellent post Koen...
Excellent! Thanks for this - I've been looking at this feature for ages. Followed your instructions and it works a treat!
Post a Comment