CAML Filter for Current Group Membership

My pal Christophe Humbert at PathToSharePoint.com forwarded me a question from one of his readers earlier today. I didn’t know the answer, but it intrigued me enough to track down the answer.

Something that a lot of people don’t realize is that every List View Web Part (LVWP) you use in your SharePoint pages contains the CAML required to render what it’s supposed to render. If you crack open the page with SharePoint Designer, you can find that CAML, copy it out, make it human readable (by changing the escaping to the real characters), and see what’s going on.

If you look at the CAML for the LVWP on the By My Groups view page in a Tasks list, you’ll see this Where clause:

<Where><Membership Type="CurrentUserGroups"><FieldRef Name="AssignedTo"/></Membership></Where>

or, formatted for humans:

<Where>
  <Membership Type="CurrentUserGroups">
    <FieldRef Name="AssignedTo"/>
  </Membership>
</Where>

If you look at the CAML schema, you can see the details for the options for Membership.  What this CAML Where clause is doing is filtering for items where the AssignedTo value exists in the the current user’s groups.  Sweet!

Frankly, this is a new one to me, but it makes sense.

Similar Posts

22 Comments

  1. Thanks for your help Marc! I am learning so much from you.

    Even better, the reference at the end of your post has exactly what I was looking for myself, a query where the user is either assigned a task based on membership in a group or assigned a task directly:

    <Or>
      <Membership Type=\"CurrentUserGroups\">
        <FieldRef Name=\"AssignedTo\"/>
      </Membership>
      <Eq>
        <FieldRef Name=\"AssignedTo\"></FieldRef>
        <Value Type=\"Integer\">
          <UserID/>
        </Value>
      </Eq>
    </Or>
    
    1. Update: it seems that multiple lookups don’t work with the crosslist DVWP or the CQWP in SP 2007. As for SP 2010, I didn’t find any information.

        1. Marc, the CAML I am referring to is the one in your post (or the one in my first comment). If “Assigned To” is a multiple lookup then the CAML won’t work in SP 2007. As for SP 2010 I don’t know.

  2. Have I read this wrong or does that mean you can show items assigned to a group the user is a member of? That would made shared task lists quite powerful…say it’s so?

    1. Nick, this is actually an OOTB feature, already available in tasks lists (as Marc says in the post).

      Marc, have you found the time to try it when “Assigned To” is a multiple lookup? (see my comment above)

      1. Christophe:

        No, I haven’t gotten to it yet. I’m hoping you aren’t waiting for me! If you have an immediate needs, let me know, and I’ll try to get to it sooner.

        M.

        1. Marc, at this point I am confident that I have the correct answer – multiple lookups don’t play well with the DVWP/CQWP.
          But you know what they say about beliefs and facts…

            1. No, not everything is possible with XSLT! And specifically filtering based on membership – what this post is about – can’t be done, which is why we need to do it in the CAML query.

  3. Hi there it is nice to filter tasks assigned to a user or to its group.
    but i want to list all tasks that are assigned to users other than the current user .
    is there any way to filter out current users tasks?

    1. You should be able to simple negate the conditions in the CAML and it’ll work. You’ll also need to make sure the ORs/ANDs are correct.

      M.

  4. Thank you for the answer Marc
    But I’m pretty new in sharepoint and i don’t know how to negate the condition

  5. Believe, it should work – the only solution in the web… Unfortunately, doesn’t work for me: may be because of FBA authentication (tokens). However, I checked 2 CAML queries:

    using (SPSite site = new SPSite("http://spsite"))
    {
    using (SPWeb web = site.OpenWeb())
    {
    SPList lTasks = web.Lists["TaskList"];
    SPQuery qry1 = new SPQuery();
    SPQuery qry2 = new SPQuery();
    string camlquery1 = "<Where><Membership Type=\"CurrentUserGroups\"><FieldRef Name=\"AssignedTo\" /></Membership></Where>";
    string camlquery2 = "<Where><Eq><FieldRef Name=\"AssignedTo\" /><Value Type=\"Text\">" + SPContext.Current.Web.CurrentUser.Groups[0].Name+ "</Value></Eq></Where>";
    qry1.Query = camlquery1;
    qry2.Query = camlquery2;
    SPListItemCollection listItemsCollection1 = lTasks.GetItems(qry1);
    SPListItemCollection listItemsCollection2 = lTasks.GetItems(qry2); 
    }
    }
    

    The trouble is, that camlquery1 doesn’t work, while camlquery2 does :( Applied to a list, The first returns 0 items, the second returns 1 item (and is SHOULD return 1 item).

    And i need to use this CAMLquery at Schema.xml for a ListView

    1. Continue researching, found the next issue:
      AssignedTo Field Value is written like:

      obj1.ID;#obj1.Name

      because it’s type is User.

      Question 1: Does

      Membership Type=”CurrentUserGroups”

      understands that type of recording GroupValue?

      Question 2: Is there any way to compare CurrentUserGroup Value with a field with type “Text” ?

  6. Solved. U wouldn’t believe, but trouble was in MultiMembership Providers and wrong Login Assigment in Custom Membership GetUser() Method…
    Sorry ^_^

  7. Hi,

    I need to filter by the author field in my list – Author’s groups = current user groups. It would be nice, if somebody could give an example of caml

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.