Share via


SharePoint: Using the CAML Membership Clause with the User Information List

There is a good documentation on Microsoft website about CAML membership clause. From this article, started to think if we can apply membership clause in other ways not specified there, like finding all groups for the current user or displaying all users from a SharePoint group. We began from the idea that people field is, in fact, a lookup field to the User Information List (hidden by default) and actually stores the ID of the user. So, what if we apply membership clause in the ID column from the user's list? 

To get all users from a SharePoint group, simply applying this CAML declaration should do the work:

<View><Query><Where><Membership Type="SPGroup" ID="108"><FieldRef Name="ID"/></Membership></Where><OrderBy><FieldRef Name="EMail"/></OrderBy></Query></View>

Number "108" should be replaced with actual ID of the group. You can find it on the page where you see members of the group:
PathToSharePointSiteRoot/_layouts/people.aspx?MembershipGroupId=IdOfTheGroup

Another request received was to display all groups for the currently logged user. Instead of creating code for this, we applied again membership clause and solved the problem:

<View><Query><Where><Membership Type="CurrentUserGroups"><FieldRef Name="ID"/></Membership></Where></Query></View>

So far, so good. Sometimes SharePoint is offering nice solutions without coding. It looks like a puzzle and it's important to put the pieces in the right places.