[for future reference] use SPQuery to query the "person or Group" field

Source: Internet
Author: User

Original Address:Http://www.stum.de/2008/02/06/querying-the-person-or-group-field-using-spquery/Querying the "person or group" field Using SPQuery (Update)

One of the weak points of Sharepoint is documentation. If you want to query a ' person or Group ' field, the documentation on MSDN is a bit sparse.
Here's 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 important things Here:value Type is "User", and the String to search for is the Display Name. So if you had a user called "John Doe" whose login is "Mydomain\jdoe" and you had to search for "John Doe" and not for "Mydoma In\jdoe ".
There is a obvious drawback to this:what if you have both users called John Doe?

I was quite disappointed, the person field does not the store the LoginName by default and can therefore is not queried fo R it.
There is and workarounds. The first one:go to the the field in the list, and at the bottom you can change "Show field" to "account".
This has the advantage so you can use the ' domain\username ' Syntax now. The Drawback:the field looks bad at the list as it does now show the Login, not the Display Name with the neat presence s Tatus.

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 the workflow, I can easily access this through WorkflowProperties.OriginatorUser.ID, which is good enough f or me.

Update: The second code example is incorrect, I fixed it now.

[for future reference] use SPQuery to query the "person or Group" field

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.