Sharepoint 2013: Get all active tasks using SPSiteDataQuery for the current user (My Tasks)

My project was an intranet, a sub site monster that needed a query to show all the tasks for the current user. There were two options - recursive code (no thank you) or SPSiteDataQuery.

The key to the code is to limit the list query to the appropriate list type (see here for a list from Microsoft). In my case, it was "<Lists ListTemplate='107' />".

Then add some simple CAML (thank you Camldesigner) and you are there.

My final query was as follows:

            SPSiteDataQuery q = new SPSiteDataQuery();
            q.Webs = "<Webs Scope='SiteCollection' />";
            q.Lists = "<Lists ListTemplate='107' />";
            q.Query = "<Where><And><Or><Membership Type='CurrentUserGroups'><FieldRef Name='AssignedTo'/></Membership><Eq><FieldRef Name='AssignedTo'></FieldRef><Value Type='Integer'><UserID/></Value></Eq></Or>
<Neq>
<FieldRef Name=’Status’ />
<Value Type=’Text’>Completed</Value>
</Neq>
</And>
</Where>";
            // Always add a row limit (I may need to pass a CAF test)
            q.RowLimit = 1000;

Two things I noticed during my development:
1. Do NOT run this under elevated privileges. The script uses the context of the current user, so using elevated privileges will return the tasks assigned to the Application Pool account.
2. I had to write the CAML query in a single line. I tried using @" .... " but the runtime compiler kept complaining about invalid characters.




Comments

Popular posts from this blog

SharePoint 2013: Error updating managed account credentials

Error deploying Nintex workflow: An item with the same key has already been added