0

In my list I have a field for just a regular old text field - all default. It's just a plain, default text field named [TextLink].

And in my fldtypes_custom.xsl file I have included some lines to convert that line of text into a hyperlink. In my CSS (in the MasterPage) I style up a little link icon.

The effect is (This works like a charm for callto links) in the list only the icon shows, and when you click on it, you follow the hyperlink, like this:

hyperlink icon button via xsl

My XSL

    <!-- Turn the ForumLink field into a link icon-->
    <xsl:template match="FieldRef[@Name='TextLink']" mode="Text_body">
        <xsl:param name="thisNode" select="."/>
        <xsl:variable name="currentValue" select="$thisNode/@*[name()=current()/@Name]" />
        <xsl:choose>
            <xsl:when test="$currentValue=''">
                <span></span>
            </xsl:when>
            <xsl:otherwise>
                <span><a href="{$currentValue}" class="textlink" target=""></a></span>
            </xsl:otherwise>        
        </xsl:choose>
    </xsl:template> 

My CSS

.textlink {
    background-repeat: no-repeat;
    background-position: center;
    height: 16px;
    width: 100%;
    padding: 1px 16px;
    }
.textlink {background-image: url("/_layouts/myimages/link.png"); }  

Now, my problem is that SharePoint is trying to hyperlink the text too, and it warps the hyperlink value to something like this. Notice how it starts with my portal's own url, and how the a element's tags are included in the hyperlink value:

https://my.portal.site/web/somelist/<a href="http://www.somesite.com/somepage/">http://www.somesite.com/somepage/</a>

But this is the real value of the link-icon-button that I see in Firebug:

<a target="" class="textlink" href="<a href="http://www.somesite.com/somepage/">http://www.somesite.com/somepage/</a>"></a>

Is there a way to fix this? My xsl should be what creates the hyperlink; but it seems to be fighting with SharePoint over who gets to do it. Is there another way to write the xsl?

1

Here is my update on Amal's solution:

<xsl:template match="FieldRef[@Name='TextLink']" mode="Text_body">
<xsl:param name="thisNode" select="."/>
<xsl:variable name="currentValue" select="$thisNode/@*[name()=current()/@Name]"/>
<xsl:choose>
    <xsl:when test="$currentValue=''">
        <span></span>
    </xsl:when>
    <xsl:otherwise>
     <span>
        <xsl:element name="a">
            <xsl:attribute name="href">
                <xsl:value-of select="substring-before(substring-after(substring-before($currentValue,'&gt;'),'&quot;'),'&quot;')" disable-output-escaping="no"/>
            </xsl:attribute>
            <xsl:attribute name="class">
                <xsl:text>textlink</xsl:text>
            </xsl:attribute>
            <xsl:text></xsl:text>
        </xsl:element>
    </span>
    </xsl:otherwise>        
</xsl:choose>

Sharepoint automatically creates a link if it thinks that your text looks like a link (if it starts with http:// mailto:// etc.) So it already puts a value of <a href="http://www.somesite.com/somepage/">http://www.somesite.com/somepage/</a> inside your actual <a>. To stop it from doing it, you set attribute for xsl:value-of for the "href" attribute to disable-output-escaping="no". This 'unrenders' the <a href="http://www.somesite.com/somepage/">http://www.somesite.com/somepage/</a> value to plain text and then you can experiment with the substring-before/after xpath formulas to obtain the original value. Mind that this solution will cause problems when you don't enter a real link value, or if you enter multiple links in one field.

If you check how Sharepoint renders text fields, it actually puts the following code:

<xsl:template name="FieldRef_Text_body.TextLink" ddwrt:dvt_mode="body" match ="FieldRef[@Name='TextLink']" mode="Text_body" ddwrt:ghost="">
    <xsl:param name="thisNode" select="."/>
    <xsl:choose>
        <xsl:when test="@AutoHyperLink='TRUE'">
    <xsl:value-of select="$thisNode/@*[name()=current()/@Name]" disable-output-escaping ="yes"/>
  </xsl:when>
        <xsl:otherwise>
    <xsl:value-of select="$thisNode/@*[name()=current()/@Name]"/>
  </xsl:otherwise>
    </xsl:choose>
</xsl:template>

See that disable-output-escaping="yes" is with value 'yes'. This is done only if @AutoHyperLink='TRUE' - I guess this is the property which is set before XSLT is rendered on the page and puts the fake code around the text. You can use this value to set custom rendering for text values which do not contain links.

  • I knew Sharepoint was hyperlinking the value before my xsl was loading, and I figured the solution was to tell it somehow not to do that. Thank you for the detailed explanation - be assured I will put the knowledge to good use. Thanks so much! – bgmCoder Jan 29 '15 at 15:44
  • @BGM have you found a solution that you could share? I'm search also for a way to prevent SharePoint from transforming a URL automatically to a hyperlink inside an XSL. – Shihan May 13 '15 at 11:00
  • Uh, this IS the solution... – bgmCoder May 13 '15 at 14:32
0

You should change your XSL as below

<!-- Turn the ForumLink field into a link icon-->
<xsl:template match="FieldRef[@Name='TextLink']" mode="Text_body">
    <xsl:param name="thisNode" select="."/>
    <xsl:variable name="currentValue" select="$thisNode/@*[name()=current()/@Name]" />
    <xsl:choose>
        <xsl:when test="$currentValue=''">
            <span></span>
        </xsl:when>
        <xsl:otherwise>
            <span>
                <xsl:element name="a">
                    <xsl:attribute name="href">
                        <xsl:value-of select="$currentValue"/>
                    </xsl:attribute>
                    <xsl:attribute name="class">
                        <xsl:value-of select="textlink"/>
                    </xsl:attribute>
                    <xsl:text></xsl:text>
                </xsl:element>
            </span>
        </xsl:otherwise>        
    </xsl:choose>
</xsl:template> 
  • Thanks for answering; that gives the same effect. I think Sharepoint is hyperlinking after the xsl is loaded. – bgmCoder Jan 28 '15 at 1:12
  • Actually, I think Sharepoint is creating a hyperlink from the text before the xslt is loaded. I'm thinking I have to fetch the url from an existing hyperlink - OR - add a class to an existing hyperlink. – bgmCoder Jan 28 '15 at 1:17

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.