Wednesday, January 14, 2009

Filtering Blog Posts - Fixing the Category filter

It seems that when you modify the view of a blog, it loses it's ability to filter by Categories. Unfortunately you have to get your hands dirty a little bit to fix this. It's not a hard fix just can be a little confusing with the CAML queries as they are formatted to html.

Note: because of html formatting, I had to type the <> codes with a space before the semicolon (&gt ;) so you can see it.

First, make a copy of Categories.aspx so you can replace it if something should go wrong.
Then open the Categories.aspx page (Lists > Categories) in your favorite editor. I just use SharePoint designer. View the page in Split view. Then click the Post web part on the page. It probably says something like "There are no posts in this category." This should highlight the code in the code viewer. Within that webpart code, scroll down to find the ListViewXml element. Within that element there is a bunch of html. (I like to copy the html into Notepad, then do a find and replace, changing all the &lt ; and &gt ; to <>. This makes the code much easier to read.) Find the Query element within that html. Now you may need to do a little research into CAML queries as that is what this query is.

Now I wanted to change the query to only show posts that are less than 20 days old. To do that, I modified the query element to look like this:

<Query><OrderBy><FieldRef Name="PublishedDate" Ascending="FALSE"/><FieldRef Name="ID" Ascending="FALSE"/></OrderBy><Where><And><Eq><FieldRef Name="PostCategory"/><Value Type=""><GetVar Scope="Request" Name="Name"/></Value></Eq><Geq><FieldRef Name="PublishedDate"/><Value Type="DateTime"><Today OffsetDays="-20"/></Value></Geq></And></Where></Query>

Now just change the &lt ; and &gt ; back to <> and then replace the query element. This should filter your posts by category and only show those less than 20 days old.

No comments:

Post a Comment