2

I have a field of type hyperlink in a blog list and I want the title field to link to that URL if it's populated but to default to the list item URL if not.

I added the display (and internal) name "Link" to the CQWP link field and was pretty sure it worked...but somehow I must have deluded myself into thinking MSFT had actually made something easy because now when I remove the URL from the link field, it defaults to the URL of the page I'm on.

Is there a way using jQuery or other scripts, to do something like

If link field is empty, render the item URL;if not, render the link in the link field.

0

The recommended solution is to modify/create an Item Style in the ItemStyle.xsl file located at Site Content > Style Library > XSL Style Sheets.

It requires that you are experienced with XSLT.

If we say that you want the CQWP to just show a list of links to blogs, a suggested solution could look something like this:

<xsl:template name="BlogListItem" match="Row[@Style='BlogListItem']" mode="itemstyle">
    <xsl:variable name="SafeLinkUrl">
        <xsl:call-template name="OuterTemplate.GetSafeLink">
            <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        </xsl:call-template>
    </xsl:variable>

    <!-- Variable holding the value of the blog items link field -->
    <xsl:variable name="AlternativeLinkUrl">
        <xsl:call-template name="OuterTemplate.GetSafeLink">
            <xsl:with-param name="UrlColumnName" select="'Custom_Internal_Link_Column_Name_Here'"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="DisplayTitle">
        <xsl:call-template name="OuterTemplate.GetTitle">
            <xsl:with-param name="Title" select="@Title"/>
            <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        </xsl:call-template>
    </xsl:variable>
    <div class="item link-item">
        <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>

        <!-- Insert anchor start tag depending on whether or not the Link field is set. -->
        <xsl:choose>
            <!-- If it's not set, insert the default title link -->
            <xsl:when test="string-length($AlternativeLinkUrl) = 0"> 
                <a href="{$SafeLinkUrl}" title="{@LinkToolTip}">
            </xsl:when>
            <!-- otherwise insert the Link field url -->
            <xsl:otherwise>
                <a href="{$AlternativeLinkUrl}" title="{@LinkToolTip}">
            </xsl:otherwise>
        </xsl:choose>

            <xsl:if test="$ItemsHaveStreams = 'True'">
                <xsl:attribute name="onclick">
                    <xsl:value-of select="@OnClickForWebRendering"/>
                </xsl:attribute>
            </xsl:if>
            <xsl:if test="$ItemsHaveStreams != 'True' and @OpenInNewWindow = 'True'">
                <xsl:attribute name="onclick">
                    <xsl:value-of disable-output-escaping="yes" select="$OnClickTargetAttribute"/>
                </xsl:attribute>
            </xsl:if>
            <xsl:value-of select="$DisplayTitle"/>
        </a>
    </div>
</xsl:template>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.