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