Get tasks assigned to user or to current user groups in SharePoint using CAML query

Standard

The CAML query to get the assigned to and to groups for the current user is

CAML to get assgiend to and your group tasks

CAML to get not completed tasks assigned to and your group

in a string format to copy

<Where>
<And>
<Or>
<Membership Type=’CurrentUserGroups’>
<FieldRef Name=’AssignedTo’ />
</Membership>
<Eq>
<FieldRef Name=’AssignedTo’  LookupId=’TRUE’ />
<Value Type=’Lookup’>123</Value>
</Eq>
</Or>
<Neq>
<FieldRef Name=’Status’ />
<Value Type=’Text’>Completed</Value>
</Neq>
</And>
</Where>

The “<Membership>” tag contains an attribute “type” , this attribute can contains 5 static values , below the description for each one:

1-“SPWeb.AllUsers”  :  this value is used to identify the tasks assigned to users (not groups)

2-“SPGroup” :  using this parameter  you must add the group ID like that:

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

This parameter will return list of tasks assigned to members of this group.

3-“SPWeb.Groups” :  The tasks assigned to the group site collection appear but not the tasks assigned to specific groups site.

4-“CurrentUserGroups ”  :  The tasks assigned to the current user groups will appear but not tasks assigned directly to the user.

5-“SPWeb.Users”  :  Tasks assigned to users who have received rights to the site directly (not through a group).

Important Note

Don’t put your CAML query inside the code block

SPSecurity.RunWithElevatedPrivileges(delegate() {

//CAML query will not work here in a right manner

}); 

it will not retrieve the right data because the current user will changed in this case

The MSDN documentation for that from here http://msdn.microsoft.com/en-us/library/aa544234.aspx

And thanks for this post that described all values with example  http://christopherclementen.wordpress.com/2012/08/27/caml-query-membership-attribute

hope it help you

Advertisements

3 thoughts on “Get tasks assigned to user or to current user groups in SharePoint using CAML query

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s