Pages

Thursday, November 19, 2009

SharePoint: User ID/Name in SPQuery

If you want to use user ID or name in SPQUery then there are few ways you can pass user information. You can use user name to filter or you can use user id. You can download U2U CAML Query builder to build query.

1. Query with user name

If you have user name and you need to query a list with that user name then you can do so by using the following CAML query:

<Query>
   <Where>
      <Eq>
         <FieldRef Name='SalesPerson' />
         <Value Type='User'>john</Value>
      </Eq>
   </Where>
</Query>

In the above CAML the value type user means the that the filed contains user data. If you use domain account then user name value will be domain\username. For domain you also need to set the show field option to account as shown below:

image

 

2. Query with User ID

If you have user ID instead of user name then you need to modify the above CAML query a bit. If you even use U2U CAML Query builder, it doesn't provide the support of User ID. The following CAML will result all list items with SalesPerson ID  7.

 

<Query>
   <Where>
      <Eq>
         <FieldRef Name='SlaesPerson' LookupId='true' />
         <Value Type='User'>7</Value>
      </Eq>
   </Where>
</Query>

In the above CAML query LookupId means to compare ID instead of user name.

FYI, CAML is case sensitive.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.