Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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:

@"<Neq>
    <FieldRef Name='Editor/New' LookupId='TRUE' />
    <Value Type='Integer'>"+ userId + @"</Value>
</Neq>"
share|improve this answer
add comment

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.

share|improve this answer
add comment

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>" +
                 "</Query>";

oAlert.Filter = oFilter;

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

share|improve this answer
    
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 '12 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. –  Egor Emelianov Dec 21 '12 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 '12 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. –  RJ Cuthbertson Dec 21 '12 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. –  RJ Cuthbertson Dec 21 '12 at 14:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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