HTML Lists and the Content Query Web Part

by 11. January 2009 19:57

The Content Query Web Part (CQWP) makes it easy to create a list of things to display to your users. Unfortunately, the CQWP does not actually make a list, instead it renders out tables that only look like a list. There are numerous reasons why you may want to use an HTML unordered (UL) or ordered (OL) list instead (accessibility, readability of code, conformance with modern web development standards), but the primary reason is that the UL/OL is semantically correct in most situations. Fortunately, convincing the CQWP to render a UL/OL is easy, and relatively straightforward.

If you’ve ever modified ItemStyle.xsl and the *.webpart file to add custom fields to the CQWP, you are ready to dive right in. If not, you may want to check out some other articles on modifying these files first.

The first thing we need to do is create copies of ContentQueryMain.xsl and ItemStyle.xsl (both files are located in /Style Library/XSL Style Sheets). By working with a copy of these files we can be sure that no future WSS\MOSS updates will overwrite our work. You can use SharePoint Designer (SPD) for this, but I prefer to use Windows Explorer.

To start, we will want to get a CQWP up and running using our copied files. This will enable us to make a change in the XSL, then immediately view its’ impact on the rendered CQWP. We need to update two properties to point to our custom XSL files: MainXslLink and ItemXslLink. It should look like this:
<property name="MainXslLink" type="string">/Style Library/XSL Style Sheets/ContentQueryMainCustom.xsl</property>
<property name="ItemXslLink" type="string">/Style Library/XSL Style Sheets/ItemStyleCustom.xsl</property>

Go ahead and upload your .webpart file, and view your CQWP. At this point it should be functional, and look unchanged.

Next, open up your copy of ContentQueryMain.xsl and locate the template OuterTemplate. Inside this template you will notice opening and closing table and row tags. Replace these tags with corresponding UL or OL tags: ContentQueryMainCustom.xsl

Next, locate the template OuterTemplate.Body, which should be a few lines down from the template we were just working on. There is a lot of garbage in this template which is used for rendering multiple columns (find references to BeginColumn and EndColumn). There is also some code which checks to see if a new group header row needs to be written, if you’re not using groups you can remove that as well. Here is the template, stripped down the bare essentials:
ContentQueryMainCustom.xsl (OuterTemplate.Body)

Finally, we need to modify our custom ItemStyle.xsl. All we are going to do here is update the XSL to render a LI, and remove the divs:
ItemStyleCustom.xsl

Now we can save the files and view the rendered CQWP. It should look something like this:
CQWP in the browser

Download the files used in this article (2.82 kb)

Tags: ,

Anonymous access and the Content by Query Web Part (CQWP): Fixing CopyUtil.aspx

by 11. July 2008 00:27

As we all know, the CopyUtil class which provides the link functionality for the CQWP does not override the AllowAnonymousAccess property of the UnsecuredLayoutsPageBase class. This results in anonymous users being prompted to login when they click on any links generated by the CQWP. A common method of getting around this is to strip out the GUID of the item, and hard-code a link to the display form (here is how to do this). This works great if your CQWP is querying only one list, but it does not work at all if you are querying multiple lists throughout your site collection.

If you are querying multiple lists, the only workaround is to write a custom class which inherits from CopyUtil. Here are the steps you can follow to get this working:

  1. Log in to your MOSS Web front end server, and navigate to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS
  2. Rename CopyUtil.aspx to CopyUtil_.aspx (Alternatively: If you don't want to modify any SharePoint files you can rename the CopyUtil.aspx which comes with this post, but be sure to update your ItemStyle.xsl to point to the new name)
  3. Download CopyUtil.zip below, it comes with two files: CopyUtil.aspx and CopyUtil.aspx.cs, unzip these files into the LAYOUTS folder:

That's it, and don't forget to disable the ViewFormPagesLockDown feature if you are running a publishing portal.

CopyUtil.zip (887.00 bytes)

Tags: , ,

About the author

Something about the author

Month List