Environment: SP2010 Enterprise, VS2012

I've been using C# to programmatically create alerts for various users and implementing the alert.Filter property to assign filters that point to specific directories. It's simple enough to create a variable for the path to insert into the filter, but when it comes to the username, passing a variable in its place does not seem to work using any method I have tried. CAML (or at least the pseudo-CAML used in filters) likes the user's name to actually be the name field (in this case, pulled from AD) not the account name. So after a little playing around I figured out how to turn that into a variable to pass into the filter like this:

string userName = vUser.Name;
string oFilter = ("<Query><And><Or><Eq><FieldRef Name='ItemFullUrl'/><Value type='string'>" + fullPath + "</Value></Eq><BeginsWith><FieldRef Name='ItemFullUrl'/><Value type='string'>" + fullPath + "/</Value></BeginsWith></Or><Neq><Value type='string'>" + userName + "</Value><FieldRef Name='Editor/New'/></Neq></And></Query>)");
oAlert.Filter = oFilter;

The user.Name property outputs a name that matches the format in the CAML filter of a working alert I examined. If I type the name directly into the filter it works fine, but when I try to pass it as a variable it breaks, even if I explicitly set the variable to "User Name".

Is there some part of how the filter/string works that I'm not accounting for? Every search I've done online has said this method should work, in theory. My goal is simply to make the alert apply only to changes made by other users, is there perhaps another way to accomplish that?

Any advice is greatly appreciated!

3 Answers 3


Maybe the problem is in the order of Value and FieldRef elements in your query. Also, if you can, try CAML query, based on SPUser Id instead of user login name, like this one:

    <FieldRef Name='Editor/New' LookupId='TRUE' />
    <Value Type='Integer'>"+ userId + @"</Value>

The first thing that stands out to me is that string should be Text, and you have an extra slash and an extra parenthesis in there (malformed query):

string userName = vUser.Name;
string oFilter = "<Query>" +
                   "<And>" +
                     "<Or>" +
                       "<Eq>" +
                         "<FieldRef Name='ItemFullUrl'/>" +
                         "<Value Type='Text'>" + fullPath + "</Value>" +
                       "</Eq>" +
                       "<BeginsWith>" +
                         "<FieldRef Name='ItemFullUrl'/>" +
                         "<Value Type='Text'>" + fullPath + "</Value>" +
                       "</BeginsWith>" +
                     "</Or>" +
                     "<Neq>" +
                       "<FieldRef Name='Editor/New'/>" +
                       "<Value Type='Text'>" + userName + "</Value>" +
                     "</Neq>" +
                   "</And>" +

oAlert.Filter = oFilter;

You can find the valid field types in CAML at Field Element (List) on MSDN.

  • Good call on the paren and string, I will fix that and see what happens. I get the feeling that's exactly what was I was doing wrong. The slash is there because of how I went about getting the filter to begin with. I created an alert with the exact properties I needed via the GUI and then examined it in SharePoint Manager, and copied the entire filter into my program. I'm not sure why the slash is there, or why it is only present in one of the paths, but being unfamiliar with CAML syntax I assumed it was necessary and just accounted for it when I replaced the path with a variable.
    – thanby
    Dec 20, 2012 at 21:00
  • @rjcup3, when SPAlert object persists, it automatically changes type attribute of value tag from Text to string for reasons, I dare not know. So maybe this is not the problem here. Dec 21, 2012 at 6:44
  • @rjcup3 Okay so I've tried the following in order: (1) Removed extra parenthesis; (2) Replaced string with Text inside query; (3) Completely replaced my query with the one you wrote. All of these continue to create non-functional alerts. I will keep experimenting.
    – thanby
    Dec 21, 2012 at 13:30
  • Alerts are managed by a timer job. There is no "user" per se. The current user account will be the identity of the timer job, not the logged in user. I'm not sure how you're retrieving your user object, but that is likely the issue. Dec 21, 2012 at 14:28
  • 1
    The other thing to try, if Editor/New is an actual User column in SharePoint, is use the user ID: "<FieldRef Name='Editor/New' LookupId='TRUE'/><Value Type='Integer'>" + vUser.ID + "</Value>". User IDs change per site collection, so you'll have to query the user information list of the site collection that the list/library is in to get the correct instance of the user. Dec 21, 2012 at 14:35

I'm not familiar with the filters specifically, but, a big issue with SPQuery is that you don't need the tag, and you would need instead. Are you certain that that is the correct syntax?

Two other things that stand out are the casing on type, and using type='string' rather than the more typical Type="Text" - though in my experience you can put nearly anything into the Type value and it will use some sort of default value for the field type.

Your Answer

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

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