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.
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:
I did a first test, the above query works as advertised when Assigned To is a single lookup, but not when it is a multiple lookup.
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.
Christophe:
Can you explain a little more what you mean? Maybe post the CAML you are trying in the CrossList DVWP?
M.
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.
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?
Yup, that’s the point. ;-)
M.
This needs far more press! :)
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)
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.
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…
All is possible with XSL. ;+)
Does this post help? Filtering on a Value in a Multi-Select Lookup Column I could have sworn that I posted this as a reusable template somewhere, but I can’t find it. I’ll put it into SPXSLT.
M.
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.
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?
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.
Thank you for the answer Marc
But I’m pretty new in sharepoint and i don’t know how to negate the condition
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:
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
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” ?
Solved. U wouldn’t believe, but trouble was in MultiMembership Providers and wrong Login Assigment in Custom Membership GetUser() Method…
Sorry ^_^
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
you are really the best :)
thank you very much :)