Wednesday, January 24, 2007

Teach the Content Query Web Part how to display a link list

A small problem with the content query web part is when you connect it to a link list is that it doesnt know what is the title of the link, and the link it is rendering point to the list item view page and not to the target of the link:

The web part renders (blank) as title

Connecting a CQWP to a link list

Connecting a CQWP to a link list

The web part renders (blank) as title, and the link points to the item

To fix this, we need to do two things:

  • Teach the web part to get the "URL" field from the list

  • Define the style for a link list that will render the link properly using the URL field

  • Lets start.

    Begin by connecting a query webpart to a link list like shown in the images above. Make sure there are some links in the list so you can track the change.
    After this, you should get the same look that you saw in the second image above.

    Export the web part - open the edit menu of the web part, and choose export to save it:

    After you save it to the hard disk, open it for editing in your text (or xml) editor of choice (notepad is ok):

    In the file, find the property "CommonViewFields". It should look like this:

    <property name="CommonViewFields" type="string" />

    Change it to include the URL field:

    <property name="CommonViewFields" type="string">URL,text</property>

    Save and import the file into the page (you can delete the web part you had on the page):

    Import web part menu

    Click the "Page" menu and select add webparts - import

    upload the web part file

    Browse to the file you just saved, and click Upload. drag and drop the web part to the page.

    So now the web part "knows" about the URL column. Now we have to "teach" it to show it properly.

    Open the site in SharePoint Designer. Under the root, find the "Style Library" folder, and then the "XSL Style Sheets" folder. right click the "ItemStyle.xsl" file and choose "checkout":

    After checking it out, open it. We are looking for the "NoImage" template. It should start with a line like this:

    <xsl:template name="NoImage" match="Row[@Style='NoImage']" mode="itemstyle">

    Copy the entire template (from the "<xsl:template" to "</xsl:template>") and paste it underneath. This is where we will create our own style - just for links.
    In the new section, change the code using the following rules (full code to follow):

    1. Rename "No Image" to "LinkList"
    2. Change the "href" attribute to point to the URL column, the text before the comma.
    3. Change the link text to point to the URL column, the text after the comma

    Here is the code:

    <xsl:template name="LinkList" match="Row[@Style='LinkList']" mode="itemstyle">
    <xsl:variable name="DisplayTitle">
    <xsl:call-template name="OuterTemplate.GetTitle">
    <xsl:with-param name="Title" select="@URL"/>
    <xsl:with-param name="UrlColumnName" select="'URL'"/>
    <xsl:variable name="LinkTarget">
    <xsl:if test="@OpenInNewWindow = 'True'" >_blank</xsl:if>
    <div id="linkitem" class="item" >
    <div class="bullet link-item">
    <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
    <xsl:attribute name="href">
    <xsl:value-of select="substring-before($DisplayTitle,', ')">
    <xsl:attribute name="title">
    <xsl:value-of select="@Description">
    <xsl:value-of select="substring-after($DisplayTitle,', ')">

    Save the file and publish it.
    Warning - if you paste it wrong or delete some character by mistake, all your content query web parts may stop working in the site collection. If that happens, either check out the file and use the undo function (if it's still in memory) or right click the file and use the version history to roll back. You may also want to download the file as a backup.

    Go back to the web part page with the uploaded webpart that has a referance to the URL field (the one we imported earlier), and open the web part properties. This may take a while, but under "Presentation" - "Styles" - "Item Style" you should have the "LinkList" style to select. Select it and click ok.

    The web part should now display the titles of the list items, and the link should point at the link target. Also, the description of the link should appear as a tool tip when you hover over the link:

    note - my site has a modified style sheet, so bullets look like ">". You'r links should appear like regular bullets.


    Anonymous said...

    Great Article Ishai.

    Can we also display sitedirectory list using CQWP ?

    What list type I am suppose to select ?


    Anonymous said...

    Great article. Saved me tons of time and headache.

    Anonymous said...

    thanks a lot for these very usefull information,,, really your article helped me a lot... thanks a lot again...

    Anonymous said...

    thank you for this information. i have been trying all morning to get something like this working. you've been a big help!

    sadalit said...

    So helpful, and really clearly written and illustrated. Thank you so much for providing this information.

    tommyboy said...

    good post! quick question for you...within what context in MOSS/WSS 3.0 would the CQWP not show up in the web part list? On one site of ours it does; on the other, it doesn't. very odd, but i'm guessing (hopefully ;) that there's a very easy explanation.


    Anonymous said...

    This is a great method yes. I also used it to create an extra style in the summary link web part.
    But what I would like to know is how can I change the real style of it.
    Like the Title only or Title and description or Image on right, I created my own. But I cannot change the real style of it, like if I do a copy of the title only, but I want to change the size of the font.
    Does anyone have an idea on how to do this ??

    Anonymous said...

    Is there any way for custom list styles to have spaces in their names? It seems that native MOSS syles can have some sort of alias like "Image on left" but any custom style needs to be "StoredLikeThis".

    Ishai Sagi [SharePoint MVP] said...

    Good question - I have never found that one out. If I find this I will post about it.

    Anonymous said...

    Damn!! I was googling for this solution for more than a week!! Awesome...u made my day!!

    Delta-N said...

    And when you want to add such a content query webpart in a pagelayout (instead of in a webpart zone), you just add:


    inside the opening tag:
    PublishingWebControls:ContentByQueryWebPart runat="server"

    JoeFox said...

    This is a handy article. Thanks.

    What I'm having trouble is this: I have a list and one field in the list is a link to a page contained within the same site. When I click on the link it just brings up the page details instead of the page itself. How can I make it direct straight to the page?

    Charf said...

    Great Post!! Thanks for your help.

    Jonas said...


    Anonymous said...

    Friggin BRINLANT!

    Anonymous said...

    One of the most useful posts on Sharepoint I've read in the last while! Awesome job! Tonnes of thanks!

    Noyee said...

    Gr8, Saved my Day.

    Anonymous said...

    Very important tip after finding I couldn't import webpart back in without error.

    Paste the CommonViewFields code and you'll realise that it changes the /> before your field name to just >

    Then I looked at other lines and realised once you add values, the / is removed. The property close tag is only needed if values are present.

    I don't understand XML and it has taken me days to work out why after thinking I was following the steps I couldn't get it to work :)

    Anonymous said...

    I am new in SharePoint and I have this request, I tested and it worked as easy as it seemed, that's great! thanks a lot! I am happy! so I cannot tell you how my boss will be! hehehe
    Have a great day!

    Daniel said...

    Thanks for the article, this is the best solution I've found to this opportunity. Works as expected, but the font color shows as the default color for our theme, and not as the customized link color that all other links on the site collection show as. Interesting.

    Anonymous said...

    Very helpful. One question though, is it possible to have an add link button to allow users with the correct security settings to add new links?

    Dan said...

    Im having a weird issue with this.
    We dont have a copy of sharepoint designer, so i manually edited the page and then checked it back in to the correct spot.

    However, i am noticing that only the user who uploads the file to the style library has the new style option.

    What have i done wrong ?

    Anonymous said...

    Great how-to article! easy to follow. It has solved one of my many sharepoint problems!

    I hope microsoft fix this in their next release!

    Anonymous said...

    This appears to already be possible with a site directory. I followed this post to get it going and then found that it was possible with the site directory. I think the site directory is a MOSS only thing, so if you are using MOSS then look for the 'Top Sites' web part and sites.

    Anonymous said...

    Thankyou - this was refreshingly straight forward and clear to follow for a person with barely any programming background.

    cpace said...

    This rocks!!! You saved me I needed to filter a link list and couldn't get the display right - it was killing me. THANKS FOR SHARING!!