Get/display Page ID - Confluence Internal Code Edited

I am trying to display a page's ID within itself i.e. within the content of the page the page ID is displayed. 

Q/A within the community recommend using $content.getIdAsString() but that has not worked. I've tried to input this within a HTML macro and without one as well but to no avail. 

I'm trying to get the ID of the page so that an HTML code block could reference the page it is included on instead of inputting the page ID by hand every single time. 

Looking forward to your responses. 

1 answer

1 accepted

2 votes

Hi,

you have to create a user macro with that content:

## Macro title: getpageid
## Macro has a body: N

## @noparams

$content.getIdAsString()

Then, put the macro to your page. 

If you need help, creating a user macro, have a look at this article

Hi Thomas, 

Thanks for the quick reply and much appreciated. I had a hunch (which I was ignoring) that this ought to be put in a macro. In turn, could this macro then be used within an HTML code block? 

Here is what my code block looks like right now:

<input type="image" src="/download/attachments/171705685/BackReturn.png" onclick="location.href=document.referrer; return false;"> 
<input type="image" src="/download/attachments/171705685/PDFdownload.png" onclick="location.href='/spaces/flyingpdf/pdfpageexport.action?pageId=171706153';">

The last pageID (underlined) is what I am trying to get through the code/macro (dynamically) so that it doesn't require manual input every single time. Could it be something like action?pageId=getpageid

Thomas Schlegel Community Champion Sep 07, 2017

No, you can't use that in an html code block natively. This macro simply returns the id as a string. 

But you could change the macro, so that it is not only displaying the id, but returning the whole html-input-tag.

I have to leave now, so I don't have the time to try that out.

I will look at this tomorrow, but maybe you can try it meanwhile on your own. It's not so hard ;-)

Sounds like a plan. I'll give it a try. 

Thanks again :)

Thomas Schlegel Community Champion Sep 08, 2017

did you get it on you own? 

If not, here's the content of the macro:

## @noparams

<input type="image" src="/download/attachments/171705685/BackReturn.png" onclick="location.href=document.referrer; return false;">
<input type="image" src="/download/attachments/171705685/PDFdownload.png" onclick="location.href='/spaces/flyingpdf/pdfpageexport.action?pageId=$content.getIdAsString()';">

Hi Thomas, 

Thanks for the wonderful initial suggestion. I was able to create a macro that dynamically got the pageID. However, initially I only created the macro with the code you provided but quickly realized that the HTML macro doesn't accept other macros within itself. 

Therefore, I included the entire HTML code (including the pageID) within the macro and everything works as intended. All my CSS and HTML elements are now where they should be and the icons onclick events work

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
Posted Jul 10, 2018 in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

781 views 23 12
Join discussion

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