SharePoint: Referencing the Current Site URL in a Data View Web Part

You’re developing a custom Data View Web Part (Data Form Web Part) instance and within your XSL you need access to the URL of the site on which the web part is being accessed.

This post assumes that the Data View Web Part is performing a same-site query, the solution here will not work for cross-site querys.

Fortunatley the DVWP passes the Site URL value as an XSL parameter into the XSL processing engine for you, you just have to pick it up in your XSL.

Thanks to this post for the information, it refers to SharePoint Services 2003, but it still works with WSS3 MOSS 2007 and contains a lot of useful information about DVWP XSL processing and the DDWRT extension functions.

In your XSL add an xsl parameter called “HttpVDir“;

	<xsl:output method="html" indent="no"/>
	<xsl:decimal-format NaN=""/>
	<xsl:param name="dvt_apos">'</xsl:param>
	<xsl:param name="ListName"/>
             <xsl:param name="HttpVDir"/>
	<xsl:param name="dvt_debug"/>

Now, during design time in SharePoint Designer, if you dump your XSL parameters to output, you’ll see the full site URL;

XSL Parameters
XSL Parameters

However, if you publish this webpart instance to a page, the HttpVDir parameter is empty, for some reason. To fix this you have to set the value of the the DVWP “ViewFlag” property to 1, it’s 0 (zero) by default.

So now in your XSL, you can reference the HttpVDir parameter like any other XSL variable, preceding it with the $ sign.

		<xsl:if test="$dvt_debug='1'">
			ListName:<xsl:value-of select="$ListName"/><br/>
			HttpVDir:<xsl:value-of select="$HttpVDir"/><br/>
		<xsl:call-template name="dvt_1"/>

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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