CAML Query – Membership attribute


Hello everyone,

Today I’ll talk about a CAML query attribute, the “membership” attribute. This attribute allows especially to query the groups the current user!

If we look at the msdn documentation, we see that there are 5 different settings possible. However, this documentation is not very detailed. The purpose of this article is to detail the different possibilities.

We will see these possibilities through tests.

Here are the parameters used for the tests:

Site structure :

RootSite based on « TeamSite »

Susbsite based on « TeamSite »

Groups and users :

The tests were performed on the subsite. The site was created by inheriting from the parent security, then security has been broken and a new group “subtestgroup” was created.

  • Groups :
    • Test Owners
    • Test Members
    • Subtestgroup
  • Users :
    •  User 1 :
      • Current user
      • Member of “Test Owners” and “subtestgroup”
  • User 2
    • rights granted directly on the subtestsite.

Query :

<Membership Type=””{0}””>

<FieldRef Name=””AssignedTo””/>

</Membership>

The 0 is replaced by different parameters.

List and tasks targeted:

Standard task list. The following tasks were created.

  • T1 assigned to user1
  • T2 assigned to test owners
  • T3 assigned to subtestgroup
  • T4 assigned to user2
  • T5 assigned to test members

Here the parameters, up to the test!

1) SPWeb.AllUsers :

This value is used to identify the tasks assigned to users (not groups)

Test result: T1 and T4

2 ) SPGroup :

Using this parameter, you must add an ID parameter with the ID of the desired group. This parameter will return the list of tasks assigned to members of this group.

The test will be performed using the ID of groups “members tests “(ID 7, empty group) and “test owners’”(ID 5, containing User 1)

If we change the query used by this one

<Membership Type=””SPGroup”” ID=”7”>

<FieldRef Name=””AssignedTo””/>

</Membership>

Test result : no tasks

Test with ID 5

Test result : T1

3 ) SPWeb.Groups :

The tasks assigned to the group site collection appear but not the tasks assigned to specific groups site

Test result : T2 and T5

4 ) CurrentUserGroups :

The tasks assigned to the current user groups will appear but not tasks assigned directly to the user.

Test result : T2 et T3

5 ) SPWeb.Users :

Tasks assigned to users who have received rights to the site directly (not through a group).

Test result : T4

Voilà !

Hope this helps!

Christopher.

3 thoughts on “CAML Query – Membership attribute

  1. Pingback: Get not completed tasks assigned to user or assigned to user groups in SharePoint using CAML query | SharePoint Development in Depth

  2. Charlie

    Chris,
    Can the membership tag help in a situation like this…

    Client wants to have a site (SP2007) with approx 1000 second-level sub-sites. They would like all users to have read access to all of the lists in the sub-sites (inherited). In addition, if the user is a member of the sub-site, they also get some extra contribute rights (sub-siteXXX membership group).

    Now, they want to do SiteCollection list queries and limit the returned data based on where the currentUser is a member of the sub-site. Out of the box, SharePoint does a good job of limiting results based on whether the user has read access to the list. But, how can I limit it to “sub-site group members” only when all users belong to an inherited group which gives everyone read access to all sub-site lists?

    bitFlinger
    bitFlinger@hotmail.com

Leave a comment