All about the Strategy, Design, Customisation, Deployment and Development of SharePoint and its related Technologies

  Administration   All Me!! Baby!!   BDC   Book Review   Business   CKS   Conferences   CQWP   Development   Duffer Moments   Email   Errors   Family   Fixes   General   Groove   How To   How To Code   InfoPath   iPhone   IRM   Longhorn   Lotus Notes   Migration   Mobility   Office System 2007   Personal Projects   Powershell   Records Management   Search Server   Security   SharePoint   SharePoint 2010   Silverlight   SQL   Tech Ed 2008   Testing   Vista   VSTO   WSS   XSL

[06/11/2007] MOSS2007 – Link Lists and the Content Query Web part
 
Categories: Development, Office System 2007, SharePoint
 

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.

 
5 Comments
 

Comments

Friday, 30 Nov 2007 10:38 by Jaap Steinvoorte
Hi Liam, Thanks for this post. I'm currently developing a new portal with several blogs. I want to display recent comments of these blogs on the main page of this portal. I tried for a week to display this but I can't find a solution. First issue I have is a URL I do not want (http://community.ada-ict.nl/blogs/virtualgenerations/Lists/Comments/3_.000) Second one is that the PostTitle (lookup) is empty. I used P: V: for displaying the queried fields(PostTitle, CommentUrl, author, Created and Body). I modified the commonviewfields, I inserted the servertemplateID (302 for the commentslist). I checked schema.xml if I used the correct names for the fields. Third one: body is empty. This one is totally odd in my opinion... I'm getting a little desperate. Do you have any suggestions, tips or ideas? (btw, I use CKS:EBE which has some extra colums Jaap

Wednesday, 13 Aug 2008 07:05 by Kakss
Hi, In your this image http://www.helloitsliam.com/Lists/Photos/110607_1143_MOSS2007Lin9.jpg where is the CommonViewFields value? I can not see this.

Friday, 14 Nov 2008 01:01 by Mehul Bhuva
Hi, I am a regular follower of your blog. Can you please send me the source code for the above mentioned style sheet.

Friday, 7 Aug 2009 08:18 by Mike Komnenous
Can you elaborate about modifying the web part CommonViewFields value. Is it supposed to change to a new property or add some children to this property?

Thursday, 1 Oct 2009 04:45 by JenF
Thanks a million--I found other articles similar to this but couldn't get their code to work, but you made it short and simple--which is what this non-developer needed!

Name:

URL:

Email:

Comments: