URI encode Source attribute in SharePoint 2007 Data View Web Part calling an InfoPath form

In the previous post, Javascript in Data View Web Part XSLT, I showed how to use javascript to do things that XSLT alone cannot do in the SharePoint 2007 Data View Web Part.

The primary motivation for investigating this was wanting to

  • add a DVWP that showed items from a Forms Library
  • include in that DVWP a link that would open the InfoPath web form
  • direct the user back to the DVWP when he/she closed the InfoPath web form (including any querystring that could be used to filter the DVWP)

Creating a link to an InfoPath web form is tricky enough because of the syntax, but the real challenge was URI encoding the current page URL so that it could be used as the Source attribute and direct users back to the DVWP.

This approach calls a javascript function from the link column in the DVWP that constructs the InfoPath friendly URL and redirects the browser to that address.

This function uri encodes both the url and the query string and then inserts them as the Source attribute.

trgt = @FileDirRef

function uses trgt to calculate the subsite address for the /_layouts/FormServer.aspx url using trgt.substring(0,trgt.lastIndexOf(“/”))

(typically the DVWP will be showing the contents of the form library so this will work)

fn = @FileRef

This is the full file name of the form that is to be opened

Javascript in Data View Web Part XSLT

Javascript in Data View Web Part XSLT

One of the great frustrations working with XSLT in Data View Web Parts is the very limited set of functions available in XSLT 1.0 available in SharePoint 2007. However (as in so many things) javascript offers a solution.

This method is the simplest I have found and is based on posts at SharepointalistProgrammingsharepoint and  Sharepointboris.

Inserting the javascript functions

The method here works for inline scripts, but would probably also work with script references

  1. Locate the root XSLT template
  2. Insert the script within a CDATA tag

Calling the javascript functions

xsl:attribute

One method is to use xsl:attribute to build a link and add an onclick attribute

using parameters and writing results back to template

This example takes the formatted modified date, passes it through a javascript function, and writes out the result

The countup() javascript function ends with document.write(var); to output the result into the xslt

uriencode the page location for use the source element of a link

This would be particularly useful for making DVWPs portable and for working with InfoPath Form Libraries.  See URI encode Source attribute in SharePoint 2007 Data View Web Part calling an InforPath form for a solution that uses these techniques.