Get/display Page ID - Confluence Internal Code

AccountName:AIB September 7, 2017

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

4 votes
Answer accepted
Thomas Schlegel
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 7, 2017

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

AccountName:AIB September 7, 2017

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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 7, 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 ;-)

AccountName:AIB September 7, 2017

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

Thanks again :)

Thomas Schlegel
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 8, 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()';">
AccountName:AIB September 11, 2017

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

Markus Pöhler December 18, 2023

Hello Thomas,

we use this user defined makro approach. Within a page property report, where this makro is in the page property "PageID" of the property pages, in some cases more or less ramdomly the resultset just delivers the value "PageID" as text instead of the number we expect to be delivered. 

It is very strange and I could not figure out what is happening. Do you have any thoughts about this?

Thx Markus 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events