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. ;+)

Similar Posts

43 Comments

  1. There’s also some new xslt definitions (I don’t know where they are, maybe in the schema) for some of the compound items like url and person. You can use something like @URL.description and @URL.link (the exact names are not right, have to crack open SPD to find them) and avoid the substring calls. I stumbled over this by accident but it would be blog worthy.

  2. Actually there is no need for custom XSLT in SharePoint 2010 CQWP! You can just map the right fields in “Fields To display” (new in SP2010) and choose an existing XSLT, eg Bulleted Title, and you are done!
    In fields to display insert Url [Custom Columns]; as URL and Notes; in Title (using Notes to display link text is a hack, but I have found no way getting at the URL title itself). This will render out nicely without any custom XSLT at all.

    1. Thanks, Anders. I wish I could remember what it was, but I seem to remember that there was some other subtle difference to what I needed.

      Or, you’ve just explained an easier way!

      M.

    2. Anders, can you please elaborate on your post for me? Are you saying that:

      1. Fields to Display > Link should read “Url [Custom Columns]”

      2. Fields to Display > Title should read “Notes”

      3. Use the “Notes” field as a replacement for the Description field?

      Thanks.

  3. Hi Marck,
    Thanks for your g8 solution. I have a query, I am using the same link list XSLT in my CQWP. but I have a requirment that in last I have View All link is there. When user click on this link it will go the contact list page with Allitem.aspx. Please let me know what are the changes I need to do.

    Thanks in Advance
    Anurag

        1. Well, you’ll need to do some debugging, then. You can create a parameter which grabs the current URL using a ServerVariable or you could figure out what relative URL might work, like “../Lists/Contacts” or something, but it really depends on your site topology.

          M.

  4. A related question … how would you display the URL field of a Links List ListItem? I am trying to display the hyperlinked URL title of the current Webs Links List. I am not successful as I have only been able to display the title without the hyperlinked code.

    SPList list = web.Lists[“Links”];
    SPListItemCollection myItems = list.Items;
    for(int i = 0; i < list.ItemCount; i++)
    {
    string itemName = myItems[i].Name;
    Response.Write(itemName);
    }

    1. Alex:

      It sounds like yuo are just trying to show the links from one list in the current site. Why not just add the list to the page with the standard List View Web Part (LVWP)?

      M.

      1. Great idea, but I left out some information. I’ve created a custom navigation menu to replace the OOTB top level nav. This custom nav “MegaMenu” is built off SharePoint’s Object Model. I have broken-down Lists, Libraries, Sites,etc, however, a new requirement came to itemize the Links List from the currentWeb. So, by using the aforementioned code I have been able to iterate through the items in the Links List but NOT been able to make their URLs hyperlink-able. A few moments ago I was able to nudge a little forward with this addition:

        Response.Write(“” + itemName + ” “);

        This snippet will write the item name AND make it linkable BUT it will NOT navigate to the URL as it is injecting a “,” comma. I found another developer’s solution here http://blogs.microsoft.co.il/blogs/itaysk/archive/2007/10/24/how-to-deal-with-url-fields-in-sharepoint-lists-programmatically.aspx which references this (apparently normal) comma injection, however, I don’t know how to manipulate the code correctly.

        Any ideas?

  5. Marc,
    I tried to perform the subscript function you suggested but could not get it to work. I did, however, get a different workaround to successfully work. I used some code snippet found at http://lrathi.blogspot.com/2008/06/working-with-data-from-different.html to help breakdown the URL from the other attributes. My final code was this:

    SPList list = web.Lists[“Links”];
    SPListItemCollection myItems = list.Items;
    foreach(SPListItem item in myItems)
    {
    string itemName = item.Name;
    SPFieldUrlValue val = new SPFieldUrlValue(item[SPBuiltInFieldId.URL].ToString());
    string linkURL = val.Url;
    string linkText = val.Description;
    Response.Write(“” + itemName + ” “);
    }

    This code will iterate the Links List of your current Web and display a hyperlinked Name for the users to click. Marc, thank you for your assistance and other developers whose blog posts assisted to this solution.

    Cheers,
    AD

  6. Anders Rask and Edward Lee answered the question. No need for coding.

    1. Fields to Display > Link should read “Url [Custom Columns]”

    2. Fields to Display > Title should read “Notes”

    3. Use the “Notes” field as a replacement for the Description field

    This worked for me perfectly.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.