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

How to get page id in Confluence programmatically?

Matti Kiviharju
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 17, 2017

How to get page id in Confluence programmatically?

3 answers

0 votes
George Lewe (Lewe) September 24, 2017

Here is how I was able to resolve this:

import com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
...

public class MyClass (...
...
public String execute(...
ContentEntityObject contentEntityObject = conversionContext.getEntity();
System.out.println("pageId : " + contentEntityObject.getId());
...
}
}
Kafka On The Shore November 29, 2018

Hi, George!

Could you show the whole code of page id execution, please? 

George Lewe (Lewe) November 30, 2018

Here is a template that might help:

public String execute(Map<String, String> map, String body, ConversionContext conversionContext) throws MacroExecutionException {

   pageBuilderService.assembler().resources().requireWebResource("com.xxx.confluence.plugins.myplugin:webresources");
   AttachmentManager attachmentManager = (AttachmentManager) ContainerManager.getComponent("attachmentManager");

   //
   // Get page ID
   //
   ContentEntityObject contentEntityObject = conversionContext.getEntity();
   Long contentId = contentEntityObject.getId();
   String pageId = contentId.toString();

   //
   // Get Base URL
   //
   BootstrapManager bootstrapManager = (BootstrapManager) ContainerManager.getComponent("bootstrapManager");
   String baseUrl = bootstrapManager.getBaseUrl();

   //
   // Build output and return it
   //      
   String output = "<p>Page ID: " + pageId + "</p><p>Base URL: " + baseUrl + "</p>";
   return output;
}
Like Kafka On The Shore likes this
Kafka On The Shore December 3, 2018

George, thank you very much! you are my hero!

0 votes
George Lewe (Lewe) September 24, 2017

Hi Matti,

did you figure this one out? I have the same challenge, getting the page ID in a plugin macro. How can I retrieve it in the Java class file?

Best regards,
George

Edit: Sorry, this should have been a reply to the first answer.

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 17, 2017

The answer to that depends on where you are starting from!  Imagine asking someone "how do I get to London?".  When they don't know where you are now.

Where is your code running basically?  A script?  A user macro?  A search result function? An add-on that's adding some form of interface (like the admin fuctions)?  Another way to ask that is "in what context"?

Matti Kiviharju
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 17, 2017

To the macro Java code:

<macro name='grid-user-timesheet' class='${project.groupId}.GridEditorUserMain' key='agile-application-stack-user'>
        <description>Creates a editable Timesheet to Confluence Page.</description>
        <resource type="velocity" name="help" location="templates/extra/editor/grideditoruser-help.vm">
            <param name="help-section" value="advanced"/>
        </resource>        
    </macro>

 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 17, 2017

So, this is an add-on for Confluence that provides a macro?

From memory, there should be a "context" variable, which gets an id via context.entity.id

Matti Kiviharju
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 17, 2017

Yes this is an Add-on for Confluence.

Matti Kiviharju
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 17, 2017
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 17, 2017

That looks right to me.  Not tested it, but it should do it.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events