Using ASP.NET ServerVariables in a SharePoint Data View Web Part

Web Design & Development

Quite often I find myself needing to use a server variable in a SharePoint data view web part. These are very useful for getting information that would otherwise not be available to use in a DVWP. There are several resources that explain how to use server variables, so check those out if you haven’t used them before.

Instead of building the server variable parameters using the Common Data View Tasks menu in SharePoint Designer, I find it faster to just type them in when I need to use several of them. With that in mind, I decided to create a quick reference for the most common ASP.NET server variables I use so I can just copy/paste them from this page rather than look them up on MSDN or w3schools.com. There are other server variables, but this is my blog, so I’m only posting the ones most useful to me :).

Parameter Bindings

These go in the <ParameterBindings> element of the DVWP.

<ParameterBinding Name="HTTP_HOST" Location="ServerVariable(HTTP_HOST)" DefaultValue="" />
<ParameterBinding Name="LOGON_USER" Location="ServerVariable(LOGON_USER)" DefaultValue="" />
<ParameterBinding Name="PATH_INFO" Location="ServerVariable(PATH_INFO)" DefaultValue="" />
<ParameterBinding Name="QUERY_STRING" Location="ServerVariable(QUERY_STRING)" DefaultValue="" />
<ParameterBinding Name="SERVER_NAME" Location="ServerVariable(SERVER_NAME)" DefaultValue="" />
<ParameterBinding Name="URL" Location="ServerVariable(URL)" DefaultValue="" />

XSL Parameters

These go in the <xsl:stylesheet> element of the DVWP, usually just before the first <xsl:template>.

<xsl:param name="HTTP_HOST" />
<xsl:param name="LOGON_USER" />
<xsl:param name="PATH_INFO" />
<xsl:param name="QUERY_STRING" />
<xsl:param name="SERVER_NAME" />
<xsl:param name="URL" />

Once the parameters are added to the DVWP, they can be used just like any other parameter or variable. I often create dynamic links to the editform.aspx or dispform.aspx page for list items using a combination of list item values and server variables to keep the DVWP as flexible and re-usable as possible.

<xsl:variable name="SourceParameter">
	<xsl:choose>
		<xsl:when test="string-length($QUERY_STRING) != 0">
			<xsl:value-of select="ddwrt:UrlEncode(concat($URL, '?', $QUERY_STRING))" />
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="ddwrt:UrlEncode($URL)" />
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>
<a href="/{@FileDirRef}/EditForm.aspx?ID={@ID}&amp;Source={$SourceParameter}" title="Update/edit {@Title}">Edit</a>

If you have used server variables in your DVWPs, I’d love to hear about it in the comments!