SDK - Embedding a TRIM saved search

  • KM507211
  • 20-Sep-2008
  • 20-Sep-2008

Archived Content: This information is no longer maintained and is provided "as is" for your convenience.

Reference

Embedding a TRIM saved search

Return to WCM Articles page

Overview

A saved search is the most efficient way to include a list of TRIM records in a WCM page. The saved search can be used to either * link to the electronic document attached to that record, or * display information contained in the records properties and fields.

Related articles

The article URI usage in WCM describes in more detail the structure of the URL used to link to a TRIM electronic document.

Steps to embed a save search

Add field to page template

Add a ‘TRIM Saved Search selector custom field’ to a WCM page template.

Example

Create a page

Create a page using the above page template. Select a saved search and any record properties and fields you wish to be included in your saved search.

Example

Preview the page XML

To ensure that the saved search (and web service) are operating correctly preview the page as XML.

Example

Add the XSL to display the saved search

Add the required XSL to the XSL stylesheet used to render this page (or create a new stylesheet and assign it to this page). You can write your own XSL to transform the saved search XML, or use the sample XSL.

Example

<!-- This statement belongs in the main page stylesheet.  The '@type=' filter an be replaced with an -->
<!-- '@name=' filter if desired.                                                                     -->

<xsl:apply-templates select="customFields/customField[@name='SavedSearch']//TrimObject" />

<!-- Put this template either after the main xsl:template in your main page stylesheet. Or in a -->
<!-- library stylesheet.                                                                        -->

<xsl:template match="TrimObject">
          <li>
          <xsl:if test="Values/Value[@Name='recIsElectronic']/@Val = 'true'">
                 <a>
                      <xsl:attribute name="href">
                                  <xsl:value-of select="/wcm:wcm/docAdmin/applicationPath" />		
                                  <xsl:text>_extdocuments/</xsl:text>
                                  <xsl:value-of select="ancestor::TrimResponse/@TrimLocation" />
                                  <xsl:text>/</xsl:text>
                                  <xsl:value-of select="Values/Value[@Name='recSuggestedFileName']/@Val" />
                                  <xsl:text>?trimid=</xsl:text>
                                  <xsl:value-of select="@Uri" />
                                  <xsl:if test="Values/Value[@Name='HashedUri']/@Val">
                                      <xsl:text>&amp;key=</xsl:text>
                                      <xsl:value-of select="Values/Value[@Name='HashedUri']/@Val" />
                                  </xsl:if>
                      </xsl:attribute>
                      <xsl:value-of select="Values/Value[@Name='recTitle']/@Val" />
                 </a>
          </xsl:if>
          </li>
</xsl:template>

Explanation of example XSL

Component Explanation
xsl:apply-templates
Selects all of the ‘TrimObject’ elements in the saved search XML and tells the XSL parser to attempt to find an XSL template to apply to them. Position this line of code at the point in your main XSL stylesheet where you wish the saved search to be inserted.
xsl:template
The template that will be applied to each TrimObject. This can be copied into your main XSL style sheet (after end of the main xsl:template) or be kept in a library style sheet.
xsl:if
Checks each TrimObject and only proceeds if it has an electronic document attached. The xsl:if can be ommitted if you wish to display all TrimObjects.
<xsl:value-of select="/wcm:wcm/docAdmin/applicationPath" />
Extracts the path of the current web site from the WCM xml (e.g. /mysite/)
<xsl:value-of select="ancestor::TrimResponse/@TrimLocation" />
The name of the WCM TRIM connection is contained in the TrimResponse element. This must be included in the URL to allow WCM to find the correct TRIM dataset.
<xsl:value-of select="Values/Value[@Name='recSuggestedFileName']/@Val" />
One property auto-included in the XML is the suggested file name. This can be used in the URL to give it a friendly appearance. The file name is not used to actually identify the record so any text could be used here.
<xsl:text>?trimid=</xsl:text>  <xsl:value-of select="@Uri" />
These two lines insert the query string ?trimid=<record URI>. The record URI is used to locate the correct record in TRIM.
<xsl:if test="Values/Value[@Name='HashedUri']/@Val">
If the option ‘Use secure URL to access TRIM documents?’ is selected in the WCM TRIM connection the property HashedUri will be included in the saved search XML. These must be included in the query string (in the format &key=<hash> for WCM to validate the TRIM URI.
 <xsl:value-of select="Values/Value[@Name='recTitle']/@Val" /> 
Inserts the record title as the text in the HTML A element.

Customising the XSL

The XSL above produces a fairly standard HTML unordered list. You can of course modify this to create any HTML structure you desire as long as the links to the electronic documents are in the correct format.

Properties that are auto-included in the Saved Search XML

There are several record properties that are usually required in a saved search which are, therefore, auto-included, you do not need to select these in the ‘Select properties’ checkbox list. These are:

  • recTitle
  • recIsElectronic
  • recExtension
  • recSuggestedFileName

XSL to list record properties only

Overview

As mentioned above a saved search does not necessarily have to link to an electronic document, it may only display the values of record properties or fields. This example assumes a saved search selector has been inserted and the property Notes has been selected. (Note: The saved search selector treats properties and TRIM user defined fields identically, they are both selected through the properties selector.)

This sample includes an entire XSL style sheet (based on Your second XSLT stylesheet). To use it just create an XSL style sheet in WCM and paste in the entirety of the XSL. The XSL assumes that you have a Saved search selector custiom field called ‘SavedSearch’ in your page template.

Sample XSLT

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:wcm="http://www.towersoft.com/ns/wcm/1.0"
exclude-result-prefixes="wcm">

<xsl:output 
	method="html" 
	doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
	doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" />

<xsl:template match="/wcm:wcm">
	<html>
		<head>
		<!-- CSS borrowed from http://alistapart.com/articles/flexiblelayouts/	-->
			<style type="text/css" media="all">
				body {
					margin: 0;
				}

				#hd {
					position:relative;
					left:0;
					top:0;
					background-color:#C3D9FF;
				}

				#bd {
					position:relative;
					left:0;
					top:0;
					min-height:400px; /* min-height does not work in IE 6 or below */
				}

				#ft {
					position:relative;
					left:0;
					top:0;
					background-color:#C3D9FF;
				}

				#sidebar {
					position:absolute;
					top:0;
					left:0;
					width:20%;
				}

				#content {
					position:relative;
					top:0;
					left:25%;
					width:60%;
				}
		
				#sidebar li {
					list-style-type: none;
				}

			</style>

		<!-- This is just some IE 6 (and below) specific code to make sure the footer does not overwrite 	-->
		<!-- the menu when there is not enough content in the body to push the footer down.		-->
			<!--[if lte IE 6]>
			<style type="text/css" media="all">
				#bd {
					height:400px;
				}
			</style>
			<![endif]-->
		</head>
		<body>
			<div id="hd">This is the heading</div>
			<div id="bd">
				<div id="sidebar">
					<xsl:apply-templates select="customFields/customField[@name='menu']/sections" />
				</div>
				<div id="content">
					<xsl:apply-templates select="customFields/customField[@name='content']" />
					<xsl:apply-templates select="customFields/customField[@name='SavedSearch']/TrimResponse/FetchResult" />
				</div>
			</div>
			<div id="ft">This is the footer</div>				
		</body>
	</html>
</xsl:template>

<!-- *** The following two templates insert the contents of an HTML type custom field	*** -->
<!-- *** without the annoying XML namespace information that xsl:copy inserts	*** -->
<!-- Code taken from http://www.stylusstudio.com/xsllist/200301/post40330.html	*** -->
<xsl:template match="customField[@type='Html']">
<xsl:choose>
	<xsl:when test="html">
		<xsl:apply-templates select="html/* | html/text()" mode="copy-no-ns" />
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of disable-output-escaping="yes" select="./text()"/>
	</xsl:otherwise>
</xsl:choose>

</xsl:template>

<xsl:template mode="copy-no-ns" match="*">
<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
	<xsl:copy-of select="@*"/>
	<xsl:apply-templates mode="copy-no-ns"/>
</xsl:element>
</xsl:template>


<!-- *** XSLT to build menu						*** -->
<!-- *** Wrap the top level items in a UL *** -->
<xsl:template match="customField/sections">
<ul>
	<xsl:apply-templates select="listItem[not(@isHidden='true')]" />
</ul>
</xsl:template>

<!-- *** Handle each listItem.  If it the listItem links to the current page (or one of its parents) then	*** -->
<!-- *** check to see if there are any child links to display.				*** -->
<xsl:template match="listItem">
<!-- Don't show link to a page that is the default for a section as the section link does this.		-->
<xsl:if test="not(@isDefault)"> 
<li>
	<a>
		<xsl:attribute name="href">
			<xsl:value-of select="url" />
		</xsl:attribute>
		<xsl:value-of select="menuText" />
	</a>

	<!-- Show children if the current list item is the item for the page section, 			-->
	<!-- or if the current list item is in the path (i.e. it is the current page or one of its parents. 	-->
	<xsl:if test="sectionId = /wcm:wcm/docDetails/path/listItem/id 
		or id = /wcm:wcm/docDetails/path/listItem/id">

		<!-- only create the next level UL if there is at least one page or section.  The sections 	-->
		<!-- and/or pages elements will only exist if there are sections or pages.		-->
		<xsl:if test="pages | sections">
			<ul>
				<xsl:apply-templates select="*/listItem[not(@isHidden='true')]" />
			</ul>
		</xsl:if>
	</xsl:if>
</li>
</xsl:if>
</xsl:template>

<!-- Table for the list of saved search results -->
<xsl:template match="FetchResult">
<!-- only create the table of there is one or more results. -->
<xsl:if test="@Count > 0">
	<table>
		<th>Title</th>
		<th>Notes</th>
		<xsl:apply-templates select="Objects/TrimObject" />
	</table>
</xsl:if>
</xsl:template>

<!--  create a table row for each result.  Create a column for record title and notes. -->
<xsl:template match="TrimObject">
        <tr>
<td>
                    <xsl:value-of select="Values/Value[@Name='recTitle']/@Val" />
</td>
<td>
                    <xsl:value-of select="Values/Value[@Name='recNotes']/@Val" />
</td>
        </tr>
</xsl:template>

</xsl:stylesheet>

Explanation of sample

The saved search related XSL is the line:

<xsl:apply-templates select="customFields/customField[@name='SavedSearch']/TrimResponse/FetchResult" />

in the body of the style sheet and the two xsl:templates at the bottom of the stylesheet.

You may, of course, want to combine the technique shown here to create a list of records that both links to the electronic document and displays record metadata.