Querying the “Person or Group” field using SPQuery (Update)
One of the weak points of Sharepoint 2007 is documentation. If you want to query a "Person or Group" field, the documentation on MSDN is a bit sparse.
Here is how to query a person:
<where> <eq> <fieldref name="PersonFieldName"/> <value type="User">User Display Name</value> </eq> </where>
Replace PersonFieldName with the internal Name of the Field.
The two important things here: Value Type is "User", and the String to search for is the Display Name. So if you have a user called "John Doe" whose login is "mydomain\jdoe", you have to search for "John Doe", not for "mydomain\jdoe".
There is an obvious drawback to this: What if you have two users called John Doe?
I was quite disappointed that the Person field does not store the LoginName by default and can therefore not be queried for it.
There are two workarounds. The first one: Go to the properties of the field in the list, and at the bottom you can change "Show Field" to "Account".
This has the advantage that you can use the "domain\username" Syntax now. The drawback: The field looks bad in the list as it does now show the Login, not the Display Name with the neat presence status.
The second workaround: Use the undocumented LookupId property and search for the numeric ID:
<where> <eq> <fieldref name="PersonFieldName" LookupId="TRUE"/> <value type="int">UserID</value> </eq> </where>
The UserID is not the "domain\username", but the numeric internal ID (SPUser.ID). As I use this in a workflow, I can easily access this through workflowProperties.OriginatorUser.ID, which is good enough for me.
Update: The second code example was incorrect, I fixed it now.
This is exactly what i'm looking for but are you shure you've posted the correct code for the second workaround?
Oops, thank you, the second example was indeed incorrect. I fixed it now.
Excellent posts. I do have problem with a Query I've created containing a SPUser. I was just wondering if you ever has this error.
Query looks like this: "ADDPRO\\nijo_addpro"
Error message: Value does not fall within the expected range.
Code:
SPListItemCollection foundItems = list.GetItems(query);
if (foundItems.Count > 0) << This line gives the error message.
foreach (SPListItem item in foundItems)
cases.Add(item);
"Value does not fall within the expected range" is one of those very generic error messages from Sharepoint that are not really helpful 🙁 In any case, it will most likely mean that your Query is not correct, most likely a mistake on the FieldRef.
Keeping in mind that you are only able to use domain\username syntax when the field has been set up properly and that you have to replace "PersonFieldName" with the actual internal name of the field, the query should look like this:
<where>
<eq>
<fieldref name="PersonFieldName"/>
<value type="User">ADDPRO\nijo_addpro</value>
</eq>
</where>
or when escaped:
SPQuery query = new SPQuery();
query.Query = "<where><eq><fieldref name=\"PersonFieldName\"/><value type=\"User\">ADDPRO\\nijo_addpro</value></eq></where>";
Not that I disagree with the rant against poor documentation (I do so myself here: http://mo.notono.us/2008/01/moss-rant-pathetic-documentation.html) but how about helping out your fellow CAML sufferer with some Community Content on MSDN? Add your finding to (spquery) http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.aspx and (fieldref) http://msdn.microsoft.com/en-us/library/ms442728.aspx, and (where) http://msdn.microsoft.com/en-us/library/ms414805.aspx
That's actually a really good point, especially because the community content helped me in some situations as well. I did not think about that yet because usually when I run into one of those problems, I'm busy resolving them and moving on with my work. I think I'll add some stuff to it later.
Can you tell me how to query for a SharePoint group instead of a user? I tried the LookupId and what was said under MSDN, but nothing worked.
Take it back.. it worked
Will this query work for Group also ?
Can you please show some pointers
You could use a workflow to set another column in the list to the user's ID. It's not ideal, but this is one way to get around the missing UserId in the user object.
Is it possible to query a Person or Group field that has multiple values? I want my query to return all lists that contain the value John Doe in the participants column.
SPQuery query = new SPQuery();
query.Query = "John Doe";
SPList list = CurrentWeb.Lists.TryGetList("GroupChallenge");
SPListItemCollection listItems = list.GetItems(query);