Displaying Links Lists’ URLs in a Content Query Web Part (CQWP) in SharePoint 2010
I was trying to use a Content Query Web Part in SharePoint 2010 today and display the URL column from a Links list. Oddly, there’s no out of the box style for this. A little Binging, an #SPHelp tweet, and I came to the conclusion that I needed to add a new style to ItemStyles.xsl in /Style Library/XSL Style Sheets.
This seems counter-intuitive; a Links list would seem to be a common type of list to use with CQWPs. But I guess not. This sort of thing with the CQWP is why I almost always just jump straight to the Data View Web Part (DVWP). I can build my own XSL libraries faster than trying to sort out the CQWP ones, but we’re aiming for as fully out of the box as possible in this project. So I needed to write some XSL. You know, out of the box.
Luckily, I found a post on the MSDN Forums with a nice XSL template to accomplish exactly what I wanted. Yay, InterWebs. I could have written this, but why, when it’s already been done?
<xsl:template name="LinkList" match="Row[@Style='LinkList']" mode="itemstyle"> <xsl:variable name="SafeLinkUrl"> <xsl:call-template name="OuterTemplate.GetSafeLink"> <xsl:with-param name="UrlColumnName" select="@URL"/> </xsl:call-template> </xsl:variable> <xsl:variable name="DisplayTitle"> <xsl:call-template name="OuterTemplate.GetTitle"> <xsl:with-param name="Title" select="@URL"/> <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/> </xsl:call-template> </xsl:variable> <xsl:variable name="TheLink"> <xsl:value-of select="substring-before($DisplayTitle,',')"/> </xsl:variable> <div id="linkitem"> <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/> <a href="{$TheLink}" target="_blank" title="This link opens in a new window"> <xsl:value-of select="substring-after($DisplayTitle,',')"/> </a> </div> </xsl:template>
BTW, this works the same way in SharePoint 2007. I just get more hits if I say SharePoint 2010 these days. ;+)
Can this CQWP be used in Sharepoint 2013? The other question is more based on some advice. If I have Links list that I want to show in drop-down menu in Sharepoint 2013, what s the best way – to use ClientWebPart or to use CQWP?
Thanks in advance :)
Marijana:
I haven’t tried this in 2013, but I would think it would work.
If you want to show links in a dropdown, “best” is relative. It depends on what you want to dropdown to do.
M.
Hi Marc,
i meet a question.
i am using object model instead of XSLT for defining the CQWP.
i add a site column(HyperLink type), and i link the CQWP presentation->links to the site column. but when i load the page, the URL is not working. i press edit the web part, change nothing, and press ok, the links will change to what i want.
i dont know how to realize that in code.
can you help me?
many thx in advance.
Mezzo
Mezzo:
Creating this stuff in code is really not my thing. I’d suggest posting to the MSDN forums.
M.
Hi,
what is the diff. between “Fields to Display > Link” and ” Fields to Display > Title” ?
if I have a Links list with Title (txt) and LinksToWeb (Link field – the user type the address & the descripion of this website – link) and I want the CQWP to show the descripion but when the user click it, the site (that was entered in the LinksToWeb field) will be open and NOT the List.all items
I should enter the XSL code above but what should I write in “Fields to Display > Link” and ” Fields to Display > Title” ?
Thanks alot
Tal
Tal:
I’m really not sure I’m following your question. It sounds like you’ve made some customizations you’ll need to sort out.
M.
Hi Marc,
Great catch thank you – quickly though how do you stop the links opening up in a new window?
Cheers
Matthew:
Just remove the target=”_blank” attribute.
M.
I am trying to create a tabular view of aggregated and filtered list items and have the URL title as well as a Hyperlink field within the view. Any assistance most welcome.
I have the XSLT configured thus:
So been playing around and what I’d actually like to do is simply embed the URL from the custom field into an href. Any assistance would be most appreciated. BTW, this is being displayed in a CQWP. So essentially, we’d have a table where we have title (linked), description (not linked), and custom field (linked to it’s internal URL).
This is a great post! I also need to provide Add new Item link under this CQWP. Is this possible. I am using SP2010.
MDeveloper:
Adding that link should be a simple tweak to the XSL. However, if you don’t want the link to be added every time you use the template, you could add a little script to the page to add it only where you need it.
M.
Thank you for your response. Issue is adding New Item link for Annoucement list under CQWP webpart, It doesn’t give me moral window it redirects to new annoucement ASPX page instead which is not what I want. :(
Marc,
I’m using the CQWP (Content Query Web Part) to display documents from a library located on another site and it reports the correct results. Issue I am having is that when you go to open a word/excel file it return an error but no issues opening up pdf files??
Pauline:
What’s the error? Are you sure the URL is correct?
M.
The URL is definitely correct as I looked at the properties. It appears that it has difficulty opening up any file that is not of PDF type.
Well, it’s hard to say what’s going on without knowing what the error is.
M.