SharePoint: Open InfoPath Forms from Search Results using Forms Services


When InfoPath Forms appear in search results, clicking on their links attempts to open the form using the InfoPath client, rather than using Forms Services, if you’re using the InfoPath client thats great, if not, not so great.

However you can modify the XSLT used to render the search results so that InfoPath Forms are opened using the browser (Forms Services) quite easily.

Navigate to your search results page, edit the page and modify the webpart settings for the “Search Core Results” webpart (if you have multiple Search Core Results webpart instances, you want the one which renders the results which is typically in the Bottom Zone).

In the DataView Properties section click on the XSL Editor button and copy out the XSL to your favourite XSL editor (much easier than using the UI dialog) and locate the template shown below.

<!-- This template is called for each result -->
<xsl:template match="Result">
 <xsl:variable name="id" select="id"/>
 <xsl:variable name="url" select="url"/>
 <span class="srch-Icon">
	<a href="{$ifpurl}" id="{concat('CSR_IMG_',$id)}" title="{$url}">
	  <img align="absmiddle" src="{imageurl}" border="0" alt="{imageurl/@imageurldescription}" />
	</a>
 </span>
 <!-- more omitted for brevity -->
</xsl:template>

The bit of interest is where the variable url is created and set to the value of the url search result element. This variable is used is multiple places within this template to provide link targets (A href and title attributes) and link text.

You can either change how the url variable is assigned or create a new variable and use this new variable in place of url where appropriate.

This might depend on a couple of issues;

  1. If you are using a Search scope which restricts results to InfoPath Forms only, you could just change how the url variable is assigned.
  2. If you want the original Url’s preserved for display purposes but to open using Forms Services you would create a new variable.
  3. If you are not using Search scopes, or your Search scope results contain InfoPath Forms and other document types, you need to update the url variable only for InfoPath Form search results.

I’ll present a couple of examples using a new variable which you can apply using a new variable or updating the existing url variable.

First, the XSL snippet below creates a URL which opens the InfoPath Form instance using Forms Services.

<xsl:variable name="myurl">
	<xsl:value-of select="sitename"/>
	<xsl:text>/_layouts/FormServer.aspx?XmlLocation=</xsl:text>
	<xsl:value-of select="url"/>
	<xsl:text>&amp;DefaultItemOpen=1</xsl:text>
</xsl:variable>

Second, this XSL snippet creates a URL which opens an InfoPath Form instance using Forms Services, if the search result is an InfoPath Form, otherwise it uses the original URL.

<xsl:variable name="myurl">
	<xsl:choose>
		<xsl:when test="contentclass='STS_ListItem_XMLForm'">
			<xsl:value-of select="sitename"/>
			<xsl:text>/_layouts/FormServer.aspx?XmlLocation=</xsl:text>
			<xsl:value-of select="url"/>
			<xsl:text>&amp;DefaultItemOpen=1</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="url"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

If you have created a new variable, the next step is to replace uses of the original url variable with your new variable.

Having done this, copy the updated XSLT to the clipboard and paste it back into the webpart, save and publish.

The ListItem ID is not present in the search results XML schema.

Interestingly the listitem ID of the search results item is not present in the result schema, least not obviously. This is of less interest for search results which contain ListItems, rather than documents etc.

The OOTB search results XML schema is shown below;

<All_Results>
    <Result>
      <id>1</id>
      <workid>197</workid>
      <rank>271</rank>
      <title>Title Value</title>
      <author>Domain\User</author>
      <size>0</size>
      <url>http://webapp/sites/test/Lists/MyList/DispForm.aspx?ID=1</url>
      <urlEncoded>http%3A%2F%2Fwebapp%2Fsites%2Ftest%2FLists%2FMyList%2FDispForm%2Easpx%3FID%3D1</urlEncoded>
      <description></description>
      <write>01/08/2011</write>
      <sitename>http://webapp/sites/test</sitename>
      <collapsingstatus>0</collapsingstatus>
      <hithighlightedsummary>
        <c0>Title</c0> Value</hithighlightedsummary>
      <hithighlightedproperties>
        <HHTitle>
          <c0>Title</c0> Value</HHTitle>
        <HHUrl>http://webapp/sites/test/Lists/<c0>MyList</c0>/DispForm.aspx?ID=1</HHUrl>
      </hithighlightedproperties>
      <contentclass>STS_ListItem_GenericList</contentclass>
      <isdocument>0</isdocument>
      <picturethumbnailurl></picturethumbnailurl>
      <imageurl imageurldescription="Result of type: document">/_layouts/images/STS_ListItem16.gif</imageurl>
    </Result>
</All_Results>

You might think that the <id> element contains the listitem ID, however this is not so, it’s simply the id value of the search results row. The <url> element (for listitems) does contain the list item ID though, as it is the URL to show the ListItem properties using the DispForm.aspx page, which has the ID= querystring parameter at the end.

Given this then, it’s a simple matter of creating a variable to extract the ID value using an XSL function shown below;

substring-after(url, 'ID=')

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:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s