Navigation

Tuesday, 9 November 2010

Tip - CAML Query Retrieve tasks assigned to user (including both AD and SP Groups)

This is something I have seen so many people struggle with, but it really is very easy, with the help of the "<Membership>" element.

The Membership element allows you to basically check to see if the AssignedTo field is assigned to any group which the current user is a member of.

Of course, you still need to use it in conjunction with a standard FieldRef check against the user's ID (which you can get using the <UserID/> element.

Below is the CAML query to return All Tasks Assigned to the Current User, including specific assignments, and where the task is assigned to a group that contains the current user (both AD Groups and SharePoint Groups).

I suppose theoretically this should also work with groups in custom Membership Providers .. but haven't tried it.
<Where>
  <Or>
    <Eq>
      <FieldRef ID='Assigned To' />
      <Value Type='Integer'><UserID/></Value>
    </Eq>
    <Membership Type='CurrentUserGroups'>
        <FieldRef Name='AssignedTo' />
    </Membership>";
  </Or>
</Where>


5 comments:

  1. Nice. Very useful nugget of knowledge.

    ReplyDelete
  2. really helped me out!

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Hi Hatch
    I want check the current login user in the particular group. As i am not storing any values for current login users in the list.

    ReplyDelete

This blog has been moved to www.martinhatch.com

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