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>

  • Prateek Kulkarni

    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.

  • Anonymous

    This comment has been removed by a blog administrator.

  • Anonymous

    This comment has been removed by a blog administrator.

  • Anonymous

    really helped me out!

  • Anonymous

    Nice. Very useful nugget of knowledge.

  • SabbeRubbish

    Hi Martin,

    Are you sure this works for Active Directory groups as well?
    I’m trying this in an on prem environment, and when I assign a task to an AD group, and use the CurrentUserGroups filter, that task doesn’t appear in my view, whereas all the tasks assigned to SP groups I am a member of do.

    • Martin Hatch

      I thought it did. Pretty sure I tested this, not only with AD Groups but also NESTED AD Groups (i.e. a group within a group)

      • SabbeRubbish

        Thanks for the quick reply.
        I must be doing something wrong then 🙂
        Or maybe it has something to do with security groups vs distribution groups?

        • Martin Hatch

          Ahh, well it won’t work with a “distribution” group. It has to be a Security Group, as distribution groups don’t resolve for AD based ACLs

          • SabbeRubbish

            Thank you, that was indeed the problem! 🙂