1

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://social.technet.microsoft.com/Forums/en-US/sharepointdevelopmentlegacy/thread/56f503f1-fc8e-4d4d-bee5-450e0f8726e3

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!

2
  • This is just a thought, and maybe a long shot cause I havn't tried it - but there is an event firing every time something happens on a list. So maybe, just maybe, you can subscribe to that event and when it fires do your stuff as well?!
    – Benny Skogberg
    Dec 13, 2012 at 20:41
  • 1
    Thanks for the suggestion, if the CAML query hadn't done the trick I would have tried your idea next (it sounds slightly more advanced so I was just going with the easier one first). I appreciate your help!
    – thanby
    Dec 14, 2012 at 15:33

1 Answer 1

2

I've just created a custom alert on a sharepoint folder and looked at its properties using sharepoint manager.

The alert created by Sharepoint was created with AlertType = SPAlertType.List (without setting up a folder item).

Also, alert's Filter property was defined as follows:

<Query>
  <Or>
    <Eq>
      <FieldRef Name="ItemFullUrl"/>
      <Value type="string">siteurl1/siteurl2/libraryurl/foldername</Value>
    </Eq>
    <BeginsWith>
      <FieldRef Name="ItemFullUrl"/>
      <Value type="string">siteurl1/siteurl2/libraryurl/foldername/</Value>
    </BeginsWith>
  </Or>
</Query>

This combination of Filter and AlertType properties mean that whenever something in a list changed and that something is a folder itself or item in a folder, the email should go to user.

Also, note that this CAML-like query uses not web-relative folder url, but site-relative, or maybe server-relative url.

I think changing your code as written above should do the trick.

3
  • Thank you for the help, that looks like what I need. But there's one problem, when I try to set alert.Filter to that query (or even one copied directly from another working alert) VS refuses to build the solution. I've tried formatting it like this: newAlert.Filter = <Query>...</Query> And like this: newAlert.Filter = string.Format("<Query>...</Query>") and just about any other combination I can think of, but it keeps trying to interpret the query as if it were C# code and not a string. Do you know what I'm doing wrong?
    – thanby
    Dec 14, 2012 at 14:57
  • Oh nevermind, I figured it out. I needed to replace the " with ' inside the query. After doing that, the build succeeded and it generated the alert. I'll test it to see if it works and post back as soon as I find out.
    – thanby
    Dec 14, 2012 at 15:08
  • @thanby, you can also use \" or "" instead of " to tell C# compiler that there are quotes in string. Dec 16, 2012 at 7:41

Your Answer

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

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