Wednesday 19 March 2008

Open Containing Folder in Search Results

I was recently asked if it was possible to display a "Open containing folder" link in search results when the returned result was a document. My initial thought was that as seen as search results are created by applying a xsl stylesheet to some xml it shouldn't be a problem. I'll just modify the xsl in the search results web part...

Ah, right, ok then ...

I became stuck when I tried to determine the url of the containing folder. I originally intended to call some script embeded in the xsl that would work out the url but then I discovered that the xsl transformation in a DataViewWebPart, from which the search results web parts inherits, uses XSLTSettings.Default which disables scripting.

Then I got an idea. I would create a ficticious element, like the ie:menuitem elements sharepoint creates for the drop down menus, called ie:link with an attribute called url that I assign the results url to. This output is ignored by the browser but then at the end of my xsl stylesheet I render some javascript onto the page that returns all the ie:link elements and creates a link to the containing folder of the url (via a bit of string manipulation) and substitutes it back into the page.

To make this link only appear when rendering documents the output of the ie:link element is rendered inside a xsl:if element that checks the isdocument element.





Download sample xsl for search results web part.

3 comments:

Anonymous said...

Stephen

Nice solution, I have been trying for the longest to get the search results to be more actionable, but with such luck.
One whish of one of my users was to be able to check out files from the search results page, but as I said, no luck on that, could you provide me with inspiration to perform such horrible action.

Stephen K said...

For inspiration I would say look at the sample to see how I identified that the result was a document and use this to create a checkout link that calls the javascript function CheckoutDocument. This function is in the core.js and ows.js file in the 12 hive \templates\layouts\1033

The biggest issue around this is determining if the current user has access to check out the file before displaying the link otherwise you may encourage them to select an option that will throw an error.

There are other methods in the ows.js and core.js files to aid this but they rely on a context object and it all gets a bit complicated.

I'm not saying that I would recommend this approach but I hope it helps you achieve what you need.

Tripwire said...

Awesome. The power of XSLT.