Thursday, 21 February 2013

Content Query Web Part - Sort by two columns

OK, this is a problem I have had to solve twice now so I am blogging it for my own reference - and for anyone else out there who might be interested.

SharePoint Content Query Web Parts only allow users to Sort By a single field.

In my case, I wanted to sort a news items listing by whether it was marked "Priority News" (a Yes/No field) and then by the Publishing Date.

There are other/smarter/more complex ways of achieving this. Indeed, to sort any more than two columns it will be necessary to look at some fancy XSLT or CAML.  But for now, this is a hack solution that a content administrator can do on their own without any more than a tiny bit of config and a single line of CSS (which I have provided :)).

In the Properties of the Content Query Web Part, select "Group By" and add your Priority News Yes/No field to it.  As you want the "Yes" values to appear first, select "Show groups in Descending Order".
Under Sort By, include the next field you would like to sort by.
Then (and here comes the trick), under Group Style, select "Whitespace".

All looking OK but you may still notice a bit of padding below each group.
As such, in your Custom CSS file, simply add the following line:

.whitespace { padding-bottom:0px !important; }


1 comment: