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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

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

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!

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.

This is great. Thanks.

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

@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?

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>

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

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

Can you create this type of Macro for cloud confluence?

TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you