Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Confluence 5.x API - extracting page metadata (created by, date, version #, last modified)

Gavin Fowler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 1, 2013

Hi

I've been modifying an old user-macro (written using wiki mark-up, yes I'll update it to HTML soon!) and I'd like some help on how to use the API (class / methods) within a user-macro to extract some page metadata, which will be used to create a standard page header across an entire space (the information gathered is mostly listed within tools / page information).

The page metadata I'm looking for is:

  • Created By (i.e. full user name)
  • Created Date/time (ideally formatted)
  • Page Version # (as an integer)
  • Last Modified By (i.e. Full User Name)
  • Last Modifed Date/time (ideally formatted).

I'm a little new to this, user-macro & API, but very familiar with confluence - so some gentle guidance on how to use the API within a user-macro would be very helpful. Explicit examples (or links) would be most appreciated!

FYI - I'm currently using the plugin "Page Information Tools" (by Adaptavist.com Ltd) to obtain this information, the intent being to reduce my dependency on this plugin by extracting my own metadata.

Kind Regards

Gavin.

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Remo Siegwart
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 30, 2013

Create a user macro with the following template code:

## @noparams

<ul>
  <li><strong>Created By:</strong> #usernameLink($content.getCreatorName())</li>
  <li><strong>Created Date:</strong> $action.dateFormatter.formatDateTime($content.getCreationDate())</li>
  <li><strong>Page Version:</strong> $content.getVersion()</li>
  <li><strong>Last Modified By:</strong> #usernameLink($content.getLastModifierName())</li>
  <li><strong>Last Modifed Date:</strong> $action.dateFormatter.formatDateTime($content.getLastModificationDate())</li>
</ul>

Hints:

  • $content is the current ContentEntityObject that this macro is included in (if available).
  • Find a list of objects available to user macros here.
  • Find a list of objects available in the default Velocity context here.
  • Find documentation about $dateFormatter here.

Hope this helps

Gavin Fowler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 30, 2013

Excellent - exactly what i was looking for. Many thanks!

emaringolo May 2, 2013

Remo: But what if I want to get info about other page. It is... bind $content (or other variable) to other page than the one where the macro is being executed.

Thank you!

Remo Siegwart
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 2, 2013

Just use the PageManager to get the desired page object:

Get the page by id (replace "12345" with the id of the page):

#set($otherPage = $pageManager.getPage(12345))

Get the page by title (replace "SPACEKEY" with the key of the space and "Your Title" with the title of the page):

#set($otherPage = $pageManager.getPage("SPACEKEY", "Your Title"))

And then in the template code replace all occurences of $content with $otherPage. You could also replace the static page id with a dynamic macro parameter, as explained here.

jako mercurio August 27, 2013

This is great. Thanks.

Jan de Vries March 16, 2015

Late to the party, but want to say thanks @Remo Siegwart. I created a user macro with this and it solved my requirement.

André Lundin November 13, 2015

@Remo Siegwart I tried using: ## @param ID:title=ID|type=string #set($otherPage = $pageManager.getPage($paramID)) But it does not manage to fetch the ID which I type in myself when using the macro. If I use $paramID it prints the ID fine. Just not within $pageManager.getPage($paramID)). Any advice?

Remo Siegwart
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 13, 2015

You first need to convert the ID to an integer as pageManager.getPage() expects an integer as parameter, but paramID is a string. Here is how you could do it: ## @param ID:title=ID|type=string #set($Integer = 0) #set($otherPage = $pageManager.getPage($Integer.parseInt($paramID))) <p>Page title: $otherPage.title</p>

André Lundin November 15, 2015

@Remo Siegwart Perfect! Works wonderful. The string/integer is above my code knowledge, but as long as it works I'm happy :]

0 votes
Sue Ganey October 13, 2017

Can you create this type of Macro for cloud confluence?

0 votes
emaringolo April 30, 2013

I also need this feature, did you find something else?

TAGS
AUG Leaders

Atlassian Community Events