Tuesday, August 31, 2010

Extracting Static Lists from Site Studio Datafile

I had a customer that needed to display the information of a static lists in the page template of the section rather than the region template.
Getting the contents of a static lists in the region template is a pretty straight forward exercise.

<!--$wcmListStart("myStaticList")-->
<!--$wcmListIndex = 0-->
<!--$wcmListNumRows = wcmListRowCount()-->
<table><!--$loopwhile wcmListIndex < wcmListNumRows-->
<tr>
<td><!--$wcmListElement("Heading_Element", wcmListIndex)--></td>
<td><!--$wcmListElement("Description_Element", wcmListIndex)--></td>
</tr>
<!--$wcmListIndex = wcmListIndex + 1-->
<!--$endloop--></table>
<!--$wcmListEnd("myStaticList")-->

However I couldn't find any useful information frrom Oracle about doing this from out-side the region context.

The main function to do extract the content of a datafile is ssIncludeXml you need to know the dDocName of the datafile you want to use, in my case, it was the same datafile of the repleasable region in the template so I used the idoc function ssDocName but you can use any content ID of a datafile in your system.

The second parameter is the XPATH expression to point to the appropiate place in the XML file. There are several examples out there of how to do this for common elements.

for example if I have a wysiwyg element named body the way to include it anywhere in my site (i.e. fragments, sub-templates, page templates) will be:
ssIncludeXml(dDocName, "wcm:root/wcm:element[@name='Element Name']/text()")
ssIncludeXml("DATAFILE123", "wcm:root/wcm:element[@name='BODY_WYSIWYG']/text()")

So to do the same with a static list element is just a matter of getting the appropiate XPATH.
ssIncludeXml(dDocName,"wcm:root/wcm:list[@name='Name of lists']/wcm:row[row number]/wcm:element[@name='Name of lists element']
For example:
ssIncludeXml("DATAFILE123","wcm:root/wcm:list[@name='myStaticList']/wcm:row[1]/wcm:element[@name='Heading_Element']

here is an example for the same datafile as the first example..(a datafile with a static lists element containing two columns):
<!--$wcmListNumRows = 0-->
<!--Get the number of records in the lists-->
<!--$strNumRecords=ssGetXmlNodeCount(ssDocName, "wcm:root/wcm:list[@name='RelatedInformationWidgetLinks']/wcm:row")-->
<!--$wcmListNumRows=toInteger(strTrimWs(strNumRecords))-->
<!--$nPos=1-->
<!-- Only display this block if there are items in the lists -->
<!--if wcmListNumRows gt 0-->
<table>
<!--$loopwhile nPos <= wcmListNumRows-->
<td><!--$ssIncludeXml(ssDocName,"wcm:root/wcm:list[@name='myStaticList']/wcm:row["&nPos&"]/wcm:element[@name='Heading_Element']--></td>
<td><!--$ssIncludeXml(ssDocName,"wcm:root/wcm:list[@name='myStaticList']/wcm:row["&nPos&"]/wcm:element[@name='Description_Element']--></td>
</tr>
<!--$nPos=nPos+1-->
<!--$endloop-->
</table>

12 comments:

Anonymous said...

Thanks helped me a lot!

cmstechie said...

Hi Juan,
Thanks for the post. I was looking for some thing like this to work with.
But for some strange reason it is not working for me. Below is the way I am using it let me know if I am doing any thing wrong.

Anonymous said...

Exactly what I was looking for and couldn't find anywhere! Thanks a lot.

Sarah said...

Thanks a bunch!

David said...

Thanks this was very helpful.

Small correction to your code. You left out /text() and closing brackets for some of the ssIncludeXml functions.

Sadia Khan said...
This comment has been removed by the author.
Sadia Khan said...

hi

I have dynamic list of documents and i want to display each document's title and dIDate meta data fields.

I tried this but does not worked.

<$if wcmDynamicList("test_DL")$>
<$executeService("SS_GET_SEARCH_RESULTS")$>
<$ loop SearchResults $>
<$xml(dDocTitle) $>

<$ ssIncludeXml(dDocName,"wcm:root/wcm:list[@name='test_DL']/wcm:row["&nPos&"]/wcm:element[@name=dInDate]$>
<$endloop$>
<$endif$>

Kindly help me out to solve this issue.
Need urgent help.
Thanks Sadia

Homer said...

Sadia,

The sample code is for Static List, if you want to use a dynamic list then you can simple execute the normal search and loop through the results, a dynamic list is just a query...
<!--$if wcmDynamicList("My_Dynamic_List")-->
<!--$SortField="dInDate"-->
<!--$SortOrder="ASC"-->

<!--$executeService("GET_SEARCH_RESULTS")-->

<!--$if SearchResults AND SearchResults.#numRows GT 0-->
<!--$loop SearchResults-->
<p>
<!--$dDocTitle-->
-
<!--$formatDateWithPattern(dInDate, "dd/MM/yyyy, h:mm a")-->
</p>
<!--$endloop-->
<!--$endif-->
<!--$endif--

When loading the dynamic list it should take the querytext specified in the element, otherwise you can cahnge the first line and just set the query text.

Hope it helps...

Sadia Khan said...

Thanks homer,

What if my content files are based on sum region definition that have elements like event_title , event_date, and event_detail and i only want to display title and date elements from that content file.
This is what i actually wanted to ask.

Sadia

Sadia Khan said...
This comment has been removed by the author.
Sadia Khan said...

The problem solved:

<$ ssIncludeXml(dDocName,
"wcm:root/wcm:element[@name='myElementName']/text()") $>

Sadia Khan said...

hello homer,

How can i access the image type element from a UCM file.

I tried this:

<$ ssIncludeXml(dDocName,
"wcm:root/wcm:element[@name='myElementName']/text()") $>

Its working fine for text element but not for image.

Kindly guide.

Sadia