Can anyone assist creating a user macro

Can anyone assist me creating a user macro that will create a table displaying every page in a space with the following details

Page Title

Version

Last modified date

Changed By

At the moment i have the following random code, i think i need to create a do while loop that will search out each page and return with the informtion required and place it into a row in a table

## @noparams
## Macro name: DisplayChildRevision
## Generates: HTML Markup

#set($childPageTitle = $childPage.getTitle)
#set($childPageVersion = $childPage.getVersion)
#set($childPageDate = $childPage.getLastModificationDate)
#set($childPageAuthor = $childPage.getLastModifier)
#set($anyKids = $action.hasChildren())
#set($kids = $root.getSortedChildren())


<div id=”document-revision-div”>
<table class="document-revision-table">

<tr>
<th>Child Page title</th>
<th>Child Page version</th>
<th>Child Date</th>
<th>Child Changed by</th>
</tr>

<tr>
<td>$childPageTitle</td>
<td>$childPageVersion</td>
<td>$childPageDate</td>
<td>$childPageAuthor</td>
</tr>

</table>
</div>

8 answers

1 accepted

Here you go:

## Macro title: Page Data
## Macro has a body: N
## Body processing: n/a
## Output: HTML
##
## Developed by: Matthew J. Horn
## Date created: 06/27/2013
## @noparams

#set ($pageListArray = [])
#set ($spaceHome = $space.getHomePage())

#macro ( process $rp )
  #set ($pagelist = $rp.getSortedChildren() )  ## returns List&lt;Page&gt;
  #foreach( $child in $pagelist )
    #set($p = $pageListArray.add( $child ) )
    #if( $child.hasChildren() )
      #process ( $child )
    #end
  #end
#end

#process ( $spaceHome )


&lt;table class="confluenceTable"&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
  &lt;th class="confluenceTh"&gt;Title&lt;/th&gt;
  &lt;th class="confluenceTh"&gt;Version&lt;/th&gt;
  &lt;th class="confluenceTh"&gt;Mod Date&lt;/th&gt;
  &lt;th class="confluenceTh"&gt;Modifier&lt;/th&gt;
 &lt;/tr&gt;

 #foreach( $child in $pageListArray)   ## child is of type Page
   &lt;tr&gt;
     &lt;td class="confluenceTd"&gt;$child.getTitle()&lt;/td&gt;
     &lt;td class="confluenceTd"&gt;$child.getVersion()&lt;/td&gt;
     &lt;td class="confluenceTd"&gt;$child.getLastModificationDate()&lt;/td&gt;
     &lt;td class="confluenceTd"&gt;$child.getLastModifierName()&lt;/td&gt;
   &lt;/tr&gt;
 #end 

&lt;/tbody&gt;
&lt;/table&gt;

You sir are a gent, If i wanted to change it so rather than searching the entire space for child pages. its only did it from the current page where the macro is applied.

How can this be done?

Would something like replacing

#set ($spaceHome = $space.getHomePage())

with

#set ($spaceHome = $context.getPage())

I cant seem to get this working.

Changing it to

#set ($currentPage = $action.page)

will allow the macro to only search child pages below the page on where the macro is applied.

Im now having trouble formatting the date to the standard format we are using

DD-MMM-YYYY (23-May-2013)

i currently have Mar 26, 2013 14:25, using the following peice of code.

#set ($PageDate = $action.dateFormatter.formatDateTime($content.lastModificationDate))

How is it possible to format the date?

To format the date the way you want it, use something like this:

$action.dateFormatter.formatGivenString("dd-MMM-yyyy", $child.getLastModificationDate())

As for just working with the current page its children (rather than the entire space), just replace the #process($spaceHome) with #process($content).

The $content object, when the macro is used directly in a page, refers to the current Page object.

hth,

matt

What is $rp, it doesnt seem to run on space export.

$rp is just the name of the argument in the process method. You could change it to whatever you want.

Matthew, how would I turn $child.getTitle() into a link that can be clicked to that page? Thanks.

You can use $child.getUrlPath() or $child.getLinkWikiMarkup() to build a link to the page in your table.

The $child var is of type Page so you can use any methods of Page or its parent classes. The full API is here:

https://docs.atlassian.com/atlassian-confluence/latest/com/atlassian/confluence/pages/Page.html

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

849 views 3 12
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you