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

How do i find the ID of the page my plugin is running on?

Matt Albone October 9, 2012

I have a plugin that inserts a menu option in the 'insert' menu when you are adding a new page, as per this tutorial:

https://developer.atlassian.com/display/CONFDEV/Adding+an+Option+to+the+Editor+Insert+Menu

The plugin's task is basically to flag the page as 'Out of Date' or various other states, but my first step in this process is to get the page's ID from within the Java.

I thought there'd be something like pagemanager.getPageID(); but it doesn't exist =[

Regards, Matt

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Artur Spiguel October 10, 2012

Matt,

You would think this would be easy to do right? Anyways I'm also interested in this and have been going through different forums looking for an answer. Through some different examples I've been able to get the following to work for me. To be clear I'm developing a plugin for a 3.x version of Confluence.

In my execute() function the following code is giving me the current page title and ID:

PageContext pageContext = (PageContext) renderContext;
ContentEntityObject contentEntityObject = pageContext.getEntity();
String pageIdTest = contentEntityObject.getTitle() + " - " + contentEntityObject.getIdAsString() + "<br><br>";

Cheers!

-Art

Matt Albone October 14, 2012

Thanks Art, and sorry for the slow reply.

I actually found another method for this before I saw your response, however my solution only worked under a certain context.

My atlassian-plugin.xml file contained a web-item, which when clicked loaded a web page. The page ID is automatically stored in the xml under the varialbe '$page.id', this can then be added as '/?id=$page.id' in the link and brought through into the java using the normal get/set spring methods.

I am pretty sure you can access the $page.id variable from the xml without having to call a page though.

Adding this line to the xml:

<context-provider class="com.dneg.menu.menuoptions.MenuOptionsHelper" />

This allowed me to create a method in the MenuOptonsHelper.java file as follows:

public Map getContextMap(Map<String, Object> parameters)

from here i could get the 'Page' object for the page in question:

Page p = (Page)parameters.get("page");

and then use that to attain the page ID as follows:

String id = p.getId();

Hopefully this helps either you or someone else in the future!

Matt

Sachin Dhamale July 13, 2014

hi Artur

PageContext pageContext = (PageContext) renderContext;

how do i get renderContext object in web panel context provider class

Gilu George July 20, 2016

Hi, what libraries did you include for the PageContext class?

1 vote
Sachin Dhamale July 11, 2014

Hi Matt;

I use same code but i am getting Null Pointer Exception on

Page p = (Page)parameters.get("page"); this line
My web panel
<web-panel key="myPanel" location="atl.header">
 <param name="page" value="$page.id"></param>  
  <context-provider class="com.confluence.webpanel.CRMUserInput" />
  <resource name="view" type="velocity" location="templates/jspPage/userInput.vm"/>
  </web-panel>

CRMUserInput


import java.util.Map;

import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.ContextProvider;
import com.atlassian.confluence.pages.Page;


public class CRMUserInput implements ContextProvider{

	@Override
	public void init(Map<String, String> params) throws PluginParseException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Map<String, Object> getContextMap(Map<String, Object> context) {
		// TODO Auto-generated method stub
		Page p = (Page)context.get("page");
		String id = p.getTitle();     //  getting  Null pointer exception
		System.out.println("=="+ id);
		return null;
	}
	

}

Can you please tell me what is wrong in this?

Tadd Seiff April 7, 2016

Your context might not have the Page in the Map.  In my context, in a blueprint, I also don't have this.  I have the parent page ID in the map, but not this page object, probably because it doesn't exist yet since I'm in the process of creating the page in the blueprint.

So, as Matt mentioned, this only works in certain "contexts", meaning both the usage and the actual context object.

Also, this should be a comment, not another answer.  Please delete and repost as a comment to the other answer if possible.

TAGS
AUG Leaders

Atlassian Community Events