[备查]使用 SPQuery 查询 "Person or Group" 字段

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.

转载于:https://my.oschina.net/dosboy/blog/668847

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值