Update: This is solved, the solution is at the bottom!
Environment: SP2010 Enterprise, 2008R2, single-server production farm with separate SQL 2008R2 server, VS2012 on a dev server, I am the sole administrator, all of this is taking place on a single top-level site.
The short version:
Trying to develop a console application that will create alerts (about 1500 of them for starters) for various users on specific sub-folders (about 300 total) in a library. Using code examples found online I have cobbled together a program to do this, but the alerts it creates never actually trigger (where as seemingly identical ones created through the browser DO trigger).
The long version:
I've managed to use code to create list alerts that work fine, but when I modify it to make alerts for specific folders within a library, the alerts get created successfully but they never actually fire when something changes.
I have used several references, most notably these:
http://sharepointlive.blogspot.com/2008/10/how-to-create-sharepoint-alerts-for.html
http://www.sharepointology.com/development/how-to-create-alerts-programmatically/
http://sharepointalert.info/troubleshooting-sharepoint-alerts/
...along with every msdn article on the relevant classes.
So for my code, I set the SPSite to the proper URL and then the rest goes like this:
using (SPWeb web = site.OpenWeb())
{
string login = @"domain\user";
SPUser user = web.Users[login];
SPAlert newAlert = user.Alerts.Add();
SPFolder folder = web.GetFolder("http://server/site/library/folder/");
newAlert.AlertType = SPAlertType.Item;
//newAlert.EventType = SPEventType.All; -> I've read that using the eventtypeindex is a better way to do it.
newAlert.Properties["eventtypeindex"] = "0";
newAlert.AlertFrequency = SPAlertFrequency.Immediate;
newAlert.DeliveryChannels = SPAlertDeliveryChannels.Email;
newAlert.Title = "New Alert Title";
newAlert.Item = folder.Item; // -> I think this and the SPFolder above may be causing some of the trouble, but taking them out results in VS saying it can't call Update because not all the required properties have been set.
newAlert.Update();
}
The product of all this is an alert that appears correct when examined in the browser, and it sends a creation notification, but never triggers when a change is made.
Using SharePoint Manager to examine the faulty code-created alert versus a working alert created using the GUI, there are a few differences. The code-created alert has several Properties that the GUI-made alert does not, like a filterpath that points to the folder. Honestly, I can't even see where the GUI-made alert specifies the folder, but it still works.
One more thing I've noticed is that the code-created alerts send a notification saying the system account subscribed you to an alert, but the GUI-made alert notifications say YOU have subscribed yourself to an alert.
So... if you're still following me at this point, kudos to you. Any advice you can give is appreciated, and if you have suggestions for alternate ways to accomplish it, I am certainly open to exploring those.
Thoughts?
Thanks!
Edit: Clarified and made more concise.
Solution:
Using a CAML query in the Filter property solved this (Thanks Egor). I was trying to avoid that to begin with because I'm not too familiar with how to use CAML, but it ended up being exactly what I needed. I got the query by using SharePoint Manager to look at a correct, working alert (created through the GUI) and copied the Filter property right out of it. Then when creating the alert, it first had to be a List alert (not Item like I had expected) and set the Filter property like this:
string oFilter = "<Query><And><Or><Eq><FieldRef Name='ItemFullUrl'/><Value type='string'>server/site/library/folder</Value></Eq><BeginsWith><FieldRef Name='ItemFullUrl'/><Value type='string'>server/site/library/folder/</Value></BeginsWith></Or><Neq><Value type='string'>username</Value><FieldRef Name='Editor/New'/></Neq></And></Query>";
newAlert.Filter = oFilter;
The key being to replace all the " with ' inside the filter. Works like a charm now! If you want the full code let me know and I'll post it. Cheers!