One last possible/hacky solution (incomplete - see note below) if you have the pagetree macro available and you want to dynamically add a pagetree to a Confluence page:
Some variables needed (javascript styley):
thisPageId = AJS.params.pageId;
var rootPageId = ''; //the root of the tree you want to show, e.g. the space homepage id
var spaceKey = ''; //as it appears in the URL
var thisPageDescendentIds = []; //populate this array before running the code (see note below)
string to create:
<div class="plugin_pagetree conf-macro output-inline" data-hasbody="false" data-macro-name="pagetree">
<ul class="plugin_pagetree_children_list plugin_pagetree_children_list_noleftspace">
<div class="plugin_pagetree_children">
</div>
</ul>
<fieldset class="hidden">
<input type="hidden" name="treeId" value="" />
<input type="hidden" name="treeRequestId" value="/plugins/pagetree/naturalchildren.action?decorator=none&excerpt=false&sort=position&reverse=false&disableLinks=false&expandCurrent=false&placement=" />
<input type="hidden" name="treePageId" value="' + thisPageId + '" />
<input type="hidden" name="noRoot" value="false" />
<input type="hidden" name="rootPageId" value="' + rootPageId + '" />
<input type="hidden" name="rootPage" value="" />
<input type="hidden" name="startDepth" value="0" />
<input type="hidden" name="spaceKey" value="' + spaceKey + '" />
<input type="hidden" name="i18n-pagetree.loading" value="Loading..." />
<input type="hidden" name="i18n-pagetree.error.permission" value="Unable to load page tree. It seems that you do not have permission to view the root page." />
<input type="hidden" name="i18n-pagetree.eeror.general" value="There was a problem retrieving the page tree. Please check the server log file for more information." />
<input type="hidden" name="loginUrl" value="/login.action?os_destination=%2Fpages%2Fviewpage.action%3FpageId%3D' + thisPageId + '&permissionViolation=true" />
<input type="hidden" name="mobile" value="false" />
<input type="hidden" name="placement" value="" />
<fieldset class="hidden">
<input type="hidden" name="ancestorId" value="' + thisPageId > ancestors[0].id + '" />
<input type="hidden" name="ancestorId" value="' + thisPageId > ancestors[1].id + '" />
<input type="hidden" name="ancestorId" value="' + thisPageId > ancestors[2].id + '" />
<input type="hidden" name="ancestorId" value="' + thisPageId > ancestors[...].id + '" />
</fieldset>
</fieldset>
</div>
Notes:
- Most of the time this works fine but occasionally I’ve noticed it doesn’t initialise (i.e. nothing is shown), so I still need to work out order of events on the page and see what I can do; any help appreciated
- I’ve found I don’t need to include the inner fieldset of ancestor Ids, even though they do appear in the code when the pagetree macro is in a page
PS - if you’re wondering why I would want to do this it’s because I’m building a tool to help me easily review and restructure content across various spaces so that we can re-use the content more reliably elsewhere, e.g. in a virtual web assistant and in within-webapp help popups.
PPS - I’m vaguely aware that there might be a pagetree rest api I should be using instead of what’s above, but all the examples were for something more complex that I needed and I was getting tired (!) but I’m open to suggestion.
UPDATE: Instead of working out how to initialise whatever adds nodes to the above code, I’ve realised I can populate the contents myself, i.e. the bit in between
<div class="plugin_pagetree_children">
</div>
from the code above by calling
url = '/plugins/pagetree/naturalchildren.action?decorator=none&excerpt=false&sort=position&reverse=false&disableLinks=false&expandCurrent=true&placement=sidebar&hasRoot=true&pageId=' + homepageId + '&treeId=0&startDepth=0&mobile=false'
then loop through thisPageId’s ancestorIds to add as many of these bit to the querystring as needed
url = url + '&ancestors=' + ancestor.id
then finish by adding
url = url + '&treePageId=' + thisPageId
the plugins/pagetree call returns html not json btw.