As most of you have probably found when you use link lists within SharePoint and then try to consume them within a CQWP, they do not display correctly. Normally when you try to consume a list or library within a CQWP the "Title" field is rendered by default. However a link list within SharePoint does not have a title field as this is a hidden field.
When you add the web part is renders as shown below:
If there was a title field this would then show the value and link direct to the item itself. To correct this issue we need to create a custom style within the "ItemStyle.xsl" file. For ease and convenience simply copy the default one and rename as appropriate. In this demo we will simply call it "DemoLinks". The XSL code before the changes looks as shown below:
The changes we will make are to strip out the majority of the code and then some XSL functions to strip out the correct values. When using the URL field from the "Links" list, the value it returns contains not only the URL value but also the description you types in. The format is:
http://www.helloitsliam.com, My Blog Site
If we just tell the XSL to simply render the URL field it will render in the format as shown above. Instead we use the "substring-after" and "substring-before" functions to slice up the value above into chunks and then render the parts we need. The CQWP by default will generate the link for the item but it won't actually be to the URL link you filled in, but to the exact list item for that entry. In the new code block shown below we remove all of the variables created by default for such things as "$SafeLinkURL". We simply replace the hyperlink code with the value we needed after chopping up the value using the substring functions. The completed code looks like this:
Notice in the following the code block the substring functions and where we use the comma as the separator to chop the value into the parts we need.
Now once we apply this style to our web part, it should render as shown below:
Notice that when you hover over the item the URL link is to the actual URL not the list item URL. This was achieved with the code above by using the "substring-before" function which as it is called takes everything before the separator that you specify, the "substring-after" works the other way around.
As a side not in order for this to work you must first tell the CQWP to render the URL field or custom field you are using. This is done by exporting the web part down and modifying the "CommonViewFields" value to something like below:
You will then need to upload this web part and then apply the style as shown above.
NOTE: Make sure you check the customised "ItemStyle.xsl" in or it may not render at all.