When customizing Data View Web Parts (DVWPs) in SharePoint Designer, the field FileRef can be used to get the URL of an item. While most list items would be displayed by sending the user to a page containing a List Form Web Part in “Display” mode (e.g. DispForm.aspx), it can sometimes be useful to send the user directly to the item itself, especially in cases of Document Libraries.
In SharePoint 2007, the FileRef field value is provided as a server-relative url. Therefore, a reliable link to the list item could be established by setting the href attribute of an anchor tag to the FileRef field value like so:
<a href=”{@FileRef}”>
<xsl:value-of select=”@Name”/>
</a>
In such an example, the href attribute value could be something like (note: FileRef field value underlined):
http://MySite/MySubSite/MyDocumentLibrary/MyDocument.docx
However, SharePoint 2010 provides this field value as a psuedo server-relative URL (for lack of a better term); it does not include the initial forward slash generally used to distinguish the server-relative nature of a URL, therefore providing a relative URL. The href attribute could thus be something like (note: FileRef field value underlined):
http://MySite/MySubSite/MyDocumentLibrary/Forms/MySubSite/MyDocumentLibrary/MyDocument.docx
This subtle difference could break many a DVWP should the environment ever be upgraded from SharePoint 2007 to SharePoint 2010. While such an issue is easy to resolve, manually updating each DVWP just before or after the upgrade process would be tedious and unreliable. Instead, it would be much easier to preemptively include handling for both environments like so:
<a href=”{@FileRef}”>
<xsl:if test=”substring(@FileRef, 1, 1) != ‘/’”>
<xsl:attribute name=”href”>
<xsl:value-of select=”concat(‘/’, @FileRef)” />
</xsl:attribute>
</xsl:if>
<xsl:value-of select=”@Name” />
</a>
Alternatively, an xsl:choose element could be used to accomplish this resolution.
One Comment
2 questions. Should “@FileRef” be “$thisNode/@FileRef” in 2010?
How do you account for “$thisNode/@FileRef” working in SPD 2010, yet when you go to the browser ti shows the root of the page you are on? That is renders “\sites\MySite\MyLibrary\Document name” but in IE8 (IE9 and Firefox too) it renders to”\sites\Mysite\MyCurrentdirectory\MyWebpage”, this being the page you have your DVWP on.
I know you can solve this by adding/display a second column in the DvWp with the value of @fileref, but