tag:help-archives.hannonhill.com,2010-02-09:/discussions/how-do-i/22048-get-a-piece-of-metadata-off-the-current-pageCascade CMS: Discussion 2016-05-19T13:49:48Ztag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-17T18:25:46Z2016-05-17T18:25:46ZGet a piece of metadata off the current page<div><p>I've made a piece of custom metadata on a page that holds a date
value. I'm currently getting the create date from the page, but I
can't figure out how to get this metadata date. My plan is to check
to see if the date is there. If it is, I'll use it, if not, I'll
use the create date. How do I get it? I tried doing the trick of
creating a format and looking at the current page, but that didn't
seem to work.</p></div>webadmintag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-17T18:33:59Z2016-05-17T18:33:59ZGet a piece of metadata off the current page<div><p>Actually, I'm still having problems. I can't seem to request the
metadata that I added. I'm not sure how to ask for that. Here's the
XML:</p>
<pre>
<code><?xml version="1.0" encoding="UTF-8"?>
<system-index-block name="calling page" type="folder" current-time="1463509783049">
<calling-page>
<system-page id="627fa3740a01010200b255729d84e038" current="true">
<name>title</name>
<title>Title</title>
<author>Author</author>
<path>/blogs/news/2016/title</path>
<site>Gatekeeper</site>
<link>site://Gatekeeper/blogs/news/2016/title</link>
<dynamic-metadata>
<name>Date</name>
<value>1-1-2008</value>
</dynamic-metadata>
<system-data-structure definition-path="News/Post">
<Image>
<path>/</path>
</Image>
<Default />
</system-data-structure>
</system-page>
</calling-page>
</system-index-block></code>
</pre>
<p>I see how to get things like date and author. How do I get my
metadata date?</p></div>webadmintag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-17T19:01:15Z2016-05-17T19:01:15ZGet a piece of metadata off the current page<div><p>Hi,</p>
<p>To confirm, are you referring to the <strong>created-on</strong>
timestamp? If so, check your Index Block and make sure the
<strong>System Metadata</strong> option is checked.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-17T19:18:03Z2016-05-17T19:18:03ZGet a piece of metadata off the current page<div><p>No, I can get the created-on timestamp. I can't figure out how
to get the date value out of the dynamic-metadata item (in this
case, 1-1-2008).</p></div>webadmintag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-17T19:32:15Z2016-05-17T19:32:15ZGet a piece of metadata off the current page<div><p>Ah, I see.</p>
<p>You can use the XPath Tool to obtain the value of the field:</p>
<pre>
<code>#set ($date = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/dynamic-metadata[name = 'Date']/value"]))
$date.value</code>
</pre>
<p>Alternatively, you can use the Cascade API and bypass the Index
Block all-together:</p>
<pre>
<code>$currentPage.getDynamicMetadataField("Date").textValue</code>
</pre>
<p>Note: if you need to do any sort of formatting (based on either
example above), you will need to convert the string into a Date
object using the Date Tool:</p>
<pre>
<code>#set ($dateObj = $_DateTool.toDate("M-d-yyyy", $date))
$_DateTool.format("MMM dd, yyyy", $dateObj)</code>
</pre>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-17T19:58:36Z2016-05-17T19:58:36ZGet a piece of metadata off the current page<div><p>Neither seem to be working. You have an extra ] in the first set
of code, but I removed it. In both cases, all I get for output is
the variable name itself.</p></div>webadmintag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-17T20:03:21Z2016-05-17T20:03:21ZGet a piece of metadata off the current page<div><p>My apologies for the extra square bracket in the snippet I
provided.</p>
<p>The date formatting snippet I provided was meant to be an
example, so it won't work if you are using the XPath snippet. To
get it working, try the following:</p>
<pre>
<code>#set ($date = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/dynamic-metadata[name = 'Date']/value"))
#set ($dateObj = $_DateTool.toDate("M-d-yyyy", $date.value))
$_DateTool.format("MMM dd, yyyy", $dateObj)</code>
</pre>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-17T20:06:41Z2016-05-17T20:06:41ZGet a piece of metadata off the current page<div><p>It's still not working, but I think I see why. I'm trying to get
the data out of the current page WITHOUT having an index associated
with it. When I get the image URL, for example, I don't have to
assign an index page to it. I'm able to access it directly by this
code:</p>
<pre>
<code>#set ($content = $_XPathTool.selectSingleNode($contentRoot, '//system-data-structure'))
#set ($imgsrc = "nothing")
#set ($imgsrc = $content.getChild('Image').getChild('link').value)</code>
</pre>
<p>But when I use the code you gave me, it looks like it's not
returning anything.</p></div>webadmintag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-17T20:15:47Z2016-05-17T20:15:47ZGet a piece of metadata off the current page<div><p>Ah, I see.</p>
<p>You will need to attach an Index Block to the region in order to
access the rest of the data. The DEFAULT region is slightly
different in that if you do not apply an Index Block the
<code>$contentRoot</code> variable will content the page's
Structured Data, or <code>page-xhtml</code> if there is no Data
Definition applied.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-18T19:46:03Z2016-05-18T19:46:03ZGet a piece of metadata off the current page<div><p>So, does this mean that I'll have to attach an index to each
page? Will it have to be one made for that page, or can I attach
one for the whole site?</p></div>webadmintag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-18T20:22:33Z2016-05-18T20:22:33ZGet a piece of metadata off the current page<div><p>Hi,</p>
<p>Correct, if you would like to re-use this code you would need to
ensure the region(s) that use the Format have an Index Block
applied.</p>
<p>If you would like to avoid needing to use an Index Block, you
can use the <code>$currentPage</code> and Cascade API to get the
field. The following should give you the value of the field:</p>
<pre>
<code>$currentPage.metadata.getDynamicField("Date").textValue</code>
</pre>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-18T21:12:46Z2016-05-18T21:12:46ZGet a piece of metadata off the current page<div><p>The $currentPage thing is what I'd like to do, but it doesn't
seem to be working. I'm attempting to use these lines of code to
read in the "Date" field:</p>
<pre>
<code>#set ($date = $currentPage.metadata.getDynamicField("Date").textValue)
Date is $date <br /></code>
</pre>
<p>All it's outputting is "Date is $date".</p></div>webadmintag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-19T11:37:13Z2016-05-19T11:37:13ZGet a piece of metadata off the current page<div><p>My apologies, I was getting structured data fields mixed up with
metadata fields.</p>
<p>When you have a moment, change <code>.textValue</code> to
<code>.value</code>.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-19T12:54:50Z2016-05-19T12:54:50ZGet a piece of metadata off the current page<div><p>Perfect. That was it! Thanks!</p></div>webadmintag:help-archives.hannonhill.com,2010-02-09:Comment/399050662016-05-19T13:49:48Z2016-05-19T13:49:48ZGet a piece of metadata off the current page<div><p>Thank you for following up, I am glad to hear the adjustment did
the trick.</p>
<p>I'm going to go ahead and close this discussion, please feel
free to comment or reply to re-open if you have any additional
questions.</p>
<p>Have a great day!</p></div>Ryan Griffith