Global Tasks View Web Part
Some time ago, someone asked on Microsoft Forum how he can create the Global Tasks list, which will be showing all the tasks assigned to the current user from all the sites. This “Blog” entry has been created in response to his question. Hope you will enjoyed.
Steps:
Open the main site, and from Site Actions select Edit Page

Click Add a Web Part, then select Tasks from the list and click Add button

Site with Web Part should look like on the following screenshot

Open SharePoint Designer
Select File | Open Site and in Site Name type http://localhost and click Open
Double click on Default.aspx site, so it will open for edit (select Split view mode) – if you like you can check out the file before opening or even do the copy of the it and open the copied one so you will not mess with default.aspx file.

In Design view, right click on the Tasks web part, and select Convert To XSLT Data View

This operation will convert ListViewWebPart to DataFormWebPart, and will generate XSLT code for showing tasks from Tasks list on the main site.
In the Code view find following lines of code
<SharePoint:SPDataSource runat="server" IncludeHidden="true" SelectCommand="<View><Query><OrderBy><FieldRef Name="Modified" Ascending="FALSE"/></OrderBy><Where><Or><Neq><FieldRef Name="Status"/><Value Type="Text">Completed</Value></Neq><IsNull><FieldRef Name="Status"/></IsNull></Or></Where></Query></View>" id="datasource1" DataSourceMode="List" UseInternalName="true">
<InsertParameters>
<asp:Parameter DefaultValue="{F6532524-58C1-4A47-83B2-AE2C87C6E89F}" Name="ListID"></asp:Parameter>
</InsertParameters>
<UpdateParameters>
<asp:Parameter DefaultValue="{F6532524-58C1-4A47-83B2-AE2C87C6E89F}" Name="ListID"></asp:Parameter>
</UpdateParameters>
<DeleteParameters>
<asp:Parameter DefaultValue="{F6532524-58C1-4A47-83B2-AE2C87C6E89F}" Name="ListID"></asp:Parameter>
</DeleteParameters>
<SelectParameters>
<asp:Parameter Name="ListID" DefaultValue="{F6532524-58C1-4A47-83B2-AE2C87C6E89F}"/>
<asp:QueryStringParameter Name="RootFolder" querystringfield="RootFolder" type="String"/>
<asp:Parameter Name="MaximumRows" DefaultValue="20"/>
</SelectParameters>
</SharePoint:SPDataSource>
SPDataSource specify list data source. From the SPDataSource we will need to change two properties: SelectCommand and DataSourceMode.
Change DataSourceMode to CrossList
New SelectCommand should look like this:
<View>
<Query>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE"/>
</OrderBy>
<Where>
<Or>
<Neq>
<FieldRef Name="Status"/>
<Value Type="Text">Completed</Value>
</Neq>
<IsNull>
<FieldRef Name="Status"/>
</IsNull>
</Or>
</Where>
</Query>
</View>
If you would like to change it/modified it, best would be to change it now. Do change it in Design View click on the little arrow in the top right corner of web part and select Filter:....

New window will be showed in which you can specify more advanced filter.

Ok, so if you’re done with filter let’s do final changes the Select Command; however If you changed the filter, please copy the new Select Command to Notepad and keep it for reference.
Now, we need to add two tags just after a View tag of Select Command:
<Webs Scope="Recursive"></Webs>
<Lists ServerTemplate="107" BaseType="0"></Lists>
Webs tag with Scope attribute define where to look for the data:
· FilesOnly — Show only the files of a specific folder.
· Recursive — Show all files of all folders.
· RecursiveAll — Show all files and all subfolders of all folders.
Lists tag define what type of list we should query. More information about server templates can be found here (BaseType) and here (Templates).
So, the SelectCommand should look like this now
<View>
<Webs Scope="Recursive"></Webs>
<Lists ServerTemplate="107" BaseType="0"></Lists>
<Query>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE"/>
</OrderBy>
<Where>
<Or>
<Neq>
<FieldRef Name="Status"/>
<Value Type="Text">Completed</Value>
</Neq>
<IsNull>
<FieldRef Name="Status"/>
</IsNull>
</Or>
</Where>
</Query>
</View>
Now we need to change every <, > and “ to <, >, &qout; and just in case, delete line breaks, so the Select Command will look like this
<View><Webs Scope="Recursive"></Webs><Lists ServerTemplate="107" BaseType="0"></Lists><Query><OrderBy><FieldRef Name="Modified" Ascending="FALSE"/></OrderBy><Where><Or><Neq><FieldRef Name="Status"/><Value Type="Text">Completed</Value></Neq><IsNull><FieldRef Name="Status"/></IsNull></Or></Where></Query></View>
Now, if you save the Default.aspx file, and you will still not see any Tasks, you should update Data Set. Therefore, in SharePoint Designer open task Pane with current data sources.

Click on the tasks, the new window will pop up

Click on Fields

You should see many fields on the left, now click twice ok, and the current data source will refresh. Now you will see the tasks in web part! Cooool :)

However, we still have few issues:
1) Link to the tasks is not pointing to the proper site.
2) All tasks are showed (not only tasks assigned to current user).
3) Tasks list should still exists on the main site – however we should not need it.
Firstly let’s solve number 2; secondly we will dive into XSLT code to solve number 1 and finally we will make our global tasks web part independent of existing lists:)
To solve issue no 2, we need to click on the little arrow in the top right corner of the web part in design view and select following option

In Filter add new filter with And option (I’ve deleted Status IS Null criterion):

Click OK. Save the file and see if the changes works :)
Now it’s time solve issue with the links! For this, we will need to dive into XSLT code. Find lines with the code for ctx.displayFormUrl and ctx.editFormUrl (displayFormUrl and editFormUrl should exist twice in the code):
ctx.displayFormUrl = "<xsl:value-of select="$URL_DISPLAY" />";
ctx.editFormUrl = "<xsl:value-of select="$URL_EDIT" />";
Change dose lines to:
ctx.displayFormUrl = "<xsl:value-of select="@EncodedAbsUrl" /><xsl:value-of select="substring-after(@FileDirRef, '#')" />/DispForm.aspx";
ctx.editFormUrl = "<xsl:value-of select="@EncodedAbsUrl" /><xsl:value-of select="substring-after(@FileDirRef, '#')" />/EditForm.aspx";
Now find line with code:
<a onfocus="OnLink(this)" href="{$URL_Display}?ID={@ID}" onclick="GoToPage('{$URL_Display}?ID={@ID}');return false;" target="_self">
and change it to:
<a href="{@EncodedAbsUrl}{substring-after(@FileDirRef, '#')}/DispForm.aspx?ID={@ID}" target="_self">
<xsl:attribute name="onclik">
GoToPage("<xsl:value-of select="@EncodedAbsUrl" /><xsl:value-of select="substring-after(@FileDirRef, '#')" />/DispForm.aspx?ID=<xsl:value-of select="@ID" />"); return false;
</xsl:attribute>
Save the file and we’re done with another issue :) If you like you can test it. Create the task on the subsite, assign yourself to the task and go to the main page and – click on the task – it should transfer you to the sub site :)
Ok, let’s solve our last issue – tasks list on the main site. Before we began, please copy the code of the webpart to notepad to have a copy of it – just in case :)
From SPDataSource tag, remove following lines:
<SelectParameters>
<asp:Parameter DefaultValue="{F6532524-58C1-4A47-83B2-AE2C87C6E89F}" Name="ListID"></asp:Parameter>
<asp:Parameter DefaultValue="20" Name="MaximumRows"></asp:Parameter>
</SelectParameters>
<UpdateParameters>
<asp:Parameter DefaultValue="{F6532524-58C1-4A47-83B2-AE2C87C6E89F}" Name="ListID"></asp:Parameter>
</UpdateParameters>
<InsertParameters>
<asp:Parameter DefaultValue="{F6532524-58C1-4A47-83B2-AE2C87C6E89F}" Name="ListID"></asp:Parameter>
</InsertParameters>
<DeleteParameters>
<asp:Parameter DefaultValue="{F6532524-58C1-4A47-83B2-AE2C87C6E89F}" Name="ListID"></asp:Parameter>
</DeleteParameters>
Now, search for the GUID (in my case F6532524-58C1-4A47-83B2-AE2C87C6E89F), and replace it to empty string (this GUID should be listed in two places only).
Now save the file :) and we’re done :) we can delete the Tasks list on the main site and the web part will still works :)
