SharePoint Sauce

Sunday, April 17, 2011

So you’ve got a nice Links list with links to all your favorite internal and external sites and you are displaying them in a Data View web part – but you want the links to those other applications/external sites to open in a new page.  That’s a fairly reasonable request.  This post will take a look at how you can do do this on a per-item basis using SharePoint Designer, a DVWP, an extra column and a little XSL. 

THE INGREDIENTS

  • 1 Data View Web Part with your Links list as the data source



  • 1 New Site Column of the Yes/No checkbox variety (call it NewWindow)



  • XSL magic



  • THE RECIPE


    Let’s start this party by getting a few things in place.  First, we need to create a new column that will store the Yes/No to decide to open in a new window or not.  Create this column using the Yes/No checkbox and add it to your Links list.  Update a few items by checking the box if you want the link to appear in a new window, and leave it empty if not. Now that we that out of the way, fire up your old friend SharePoint Designer.  I am doing this in 2010 but the idea is the same for WSS/MOSS.

    Go ahead and create a new data view web part with your Links list as the data source if you don’t have this setup already.  I am assuming you know how to do this. In any case, be sure to use an empty data view.  If you need help, leave me a comment below.

    If you are setting up this DVWP right now, you can just use the URL column and insert as a multiple item view.  If you want some quick and dirty styling, the bulleted list of titles will get the job done.  If you change to this styling, you will want to replace the line:

    <xsl:value-of select="@URL.desc" />
    

    with:

    <a href="{@URL}">
    <xsl:value-of select="@URL.desc"/>
    </a>
    

    in the rowview template.  You can hop to this line of code by clicking a Link list item in the Design view window.

    Now to tie in our new column.  In the code view, you will want to change the dvt_1.rowview template from something like (yours might be a little different):


    <xsl:template name="dvt_1.rowview">
    <li class="ms-vb">
    <a href="{@URL}">
    <xsl:value-of select="@URL.desc"/>
    </a>        
    <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
    <br /><span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view" />
    </xsl:if>
    </li>
    </xsl:template>
    




    to:

    <xsl:template name="dvt_1.rowview">
    <xsl:variable name="OpenLinkTarget">
    <xsl:if test="@NewWindow = 'Yes'">_blank</xsl:if>
    <xsl:if test="@NewWindow = 'No'">_self</xsl:if>    
    <xsl:if test="@NewWindow = ''">_blank</xsl:if>
    </xsl:variable>
    <li class="ms-vb">
    <a href="{@URL}" target="{$OpenLinkTarget}" alt="{@URL.desc}"><xsl:value-of select="@URL.desc" /></a><br />
    <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
    <br /><span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view" />
    </xsl:if>
    </li>
    </xsl:template>
    




    So what exactly did this do?  Basically we are just setting a variable called OpenLinkTarget to either _blank or _self depending on whether our NewWindow field is checked or not. Then we set the target in the anchor tag with the variable.

    Saturday, April 16, 2011

    Somehow you have stumbled onto my little blog – welcome!  I am starting this blog to contribute what I can to the SharePoint community.  One of the best things about SharePoint is how willing everyone is to SHARE how they are using the technology.

    The SharePoint Sauce is meant to provide little tips, tricks and ideas for pulling together the various components of SharePoint to present some meaningful information/functionality to the user.  It’s all about the sauce! (If you are new to the SharePoint community, you will learn that everyone has some kind of SharePoint metaphor…this is mine!)

    A little about me – I am a SharePoint consultant currently located in wonderful Minneapolis-St. Paul and having been working with SharePoint since 2008.  I am supported by my wonderful wife Jennifer, 9 month old daughter Brooklynn, and two puppies, Mandy and Lucy.

    Hopefully you will find my SharePoint Sauce helpful (and tasty? Still working on that part of the metaphor.  Stay tuned.)!