Tuesday, October 27, 2009

Joined Subviews with DVWP and Query Strings

Jeremy Williams, Sr. Director, Modern Workplace

I run into this problem more often than not, and it seems that I always forget the little snippet of code to add, so here goes.

Situation: There's a standard DispForm.aspx page for all lists; this page shows the detail of one particular list item. The currently displayed item is referenced by a Query String (ID). You want to make a child view based upon the information currently showing in the DispForm.aspx page. Architecturally, I have a lookup field in the 'child' data source that is looking up the Title of the 'Parent' (the DispForm.aspx that we're currently editing).  

For the sake of this post, I'm going to be using my internal (SharePoint 2007) blog as an example.  The SharePoint blog site template, by default, has a number of lists (Category, Posts, Comments, etc.).  The Posts list has a lookup to the Category list.   When I click on a particular Category in the Category List, I would like to see all of the posts that have that particular category.  In this example, the Category list is the 'parent', and the Posts list is the 'child'.  Here's a before-shot of the Category detail page:

Before view of the Category DispForm.aspx

Solution:  [Side-note: The blog site template is a bit specialized as the 'DispForm.aspx' which is found on most lists has been renamed to ViewCategory.aspx; please note that I will use ViewCategory.aspx and DispForm.aspx interchangably throughout this post]  Open up DispForm.aspx for the 'parent' list that we're working on (Category).  Be sure to create a copy of the 'clean' DispForm.aspx before you start; in case anything goes wrong, you'll be able to revert back.  Also, you should be in SharePoint Designer now, so please put Visual Studio down for just a couple minutes… 

With the DispView.aspx for Categories open, go ahead and add another Web Part Zone beneath the default List Form Web Part. To the new zone, add a DVWP (Data View Web Part) with the 'child' list (Posts) as the data source. You're going to want to filter the DVWP, so open up the Filter box by clicking the little arrow in the upper right hand side of the DVWP. Select the Lookup Column referencing the parent for the Category; and click on 'Create New Parameter' for the 'Value' field. Name the parameter whatever you like, but make it a Query String parameter of ID with a default value of 1. You should end up with something to this effect: 

 

Now, you'll have to switch to Split/Code view as the next part is where all the magic happens!

Find this tag within the DVWP: <SharePoint:SPDataSource… Now scroll over until you spot the name of the field you're looking to lookup against. In my case, it is marked below in BOLD. [Please note: your code will be slightly different from mine below...The key is to find your field in the 'selectcommand' arguement.]  Now that you've found your field, simply prepend that field with the text highlighted in Yellow in my example below.

<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" selectcommand="&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef  LookupId='TRUE' Name=&quot;PostCategory&quot;/&gt;&lt;Value...

Save your changes, then check out the page in SharePoint and... Voila! A joined subview DVWP!