Thursday, March 3, 2011

CAML Query and User Groups – Membership

While creating a Timesheet webpart I needed to list a users tasks in a task list. The tip from Martin Hatch gave the basics. and the query executed fine in U2U CAML Query Builder (2007 version works fine for 2010)

<Where>
  <Or>
    <Eq>
      <FieldRef Name='AssignedTo' />
      <Value Type='UserMulti'>User Display Name</Value>
    </Eq>
    <Membership Type='CurrentUserGroups'>
      <FieldRef Name='AssignedTo' />
    </Membership>
  </Or>
</Where>


This query will return users in groups, and multiple users specified in the field:


Users and Groups


Note:



The Membership attribute checks current user, so do not run the query with elevated rights (SPSecurity.RunWithElevatedPrivileges(…)



Unless you assign tasks to your application pool the query will not return items based on group assignments

3 comments:

Kannan K said...
This comment has been removed by the author.
Kannan K said...

This is not working for me ..

could you please advice me how can i do this..

any help would be appreciated..

find my code below





9;#user1;#10;#user2





Christian Berg-Henry said...

In the case ID;#Name try to use the PickerEntity object:

PickerEntity pe = (PickerEntity)userPicker.Entities[0]; //gets first user in list
string username = pe.Description;

Also see

http://www.sharepoint-tips.com/2007/10/using-sharepoint-people-picker.html