Tuesday, January 6, 2009

Read QueryString Parameter and use in CAML to display specific blog category Posts

Well, SharePoint is full of challenges, and one day before I was stuck on this problem. I modified Blog Category page, and hence the page became customized. Now when I click a specific blog category, the page was displaying all the posts having no category defined. I was confused what is happening. Apparently I looked the CAML Query in 'SelectCommand' attribute and found the CAML as:
<View><Query><OrderBy><FieldRef Name="PublishedDate" Ascending="FALSE"/><FieldRef Name="ID" Ascending="FALSE"/></OrderBy><Where><Eq><FieldRef Name="PostCategory"/><Value Type=""><GetVar Scope="Request" Name="Name"/></Value></Eq></Where></Query></View>"

Looked Ok to me! What is wrong in there. Noticed that how <GetVar> is working here. I thought of modifying the CAML query, but thought to investigate further using SharePoint Designer. BTW, here I am modifying WebPartPages:DataFormWebPart that has my custom formatting.
  1. Now clicked this DataFormWebPart and a left arrow appears on the right of the same window, click it.
  2. The window is titled as 'Common Data View Tasks'.
  3. Click the top option - Filter.
  4. When you click this 'Filter' option, 'Filter Criteria' window will open.
  5. Click 'Click here to add a new clause...'.
  6. Select the Field Name - In my case it was 'Category'.
  7. Select Comparison value as 'Equals'
  8. Select Value field and drop down option, click the last option...Create a new parameter. As soon as you click this new parameter option from drop down list, a new window will open titled as 'Data View Properties'.
  9. Under Parameters, Type 'CategoryName' for 'Param1'. You can name anything here.
  10. For Source Column, on the right side, select 'Query String' from the 'Parameter Source' drop down combo box.
  11. As soon as you select 'Query String' from drop down, two new text boxes will appear. One is to define the query string variable name. Category page, by default, has 'Name' as Query String value. So Type 'Name' in Query String Variable Text Box.
  12. Leave 'Default Value' text box empty.
  13. Click Ok to close 'Data View Properties' window.
  14. Click Ok to close 'Filter Criteria' window.

You are done here.

And finally when you look back your CAML query, it will look like the following:

<View><Query><OrderBy><FieldRef Name="PublishedDate" Ascending="FALSE"/><FieldRef Name="ID" Ascending="FALSE"/></OrderBy><Where><Eq><FieldRef Name="PostCategory"/><Value Type="Text">{CategoryName}</Value></Eq></Where></Query></View>

And when I ran my category page again for a specific category, filtering was happening correctly.

I am relieved!!

References that I used:

http://msdn.microsoft.com/en-us/library/aa218649.aspx - This I looked to understand the parameter binding relationship and from here I got the clue to look for 'Query String' option as one of the ParameterBindings Name is 'filterParam'. It is interesting article though.