atlassian-python-api icon indicating copy to clipboard operation
atlassian-python-api copied to clipboard

Create a page from a template

Open kierun opened this issue 6 years ago • 6 comments

Is it possible to get a page from a template and after modifying it, create a new page?

kierun avatar Jun 05 '19 13:06 kierun

I use this library for this purpose.

My solution was to grab the XML representation of a Confluence page (which is full of Jira Macros) and simply change the JQL feeding the Macros using String.replace for each page I need to create.

mattwakeman avatar Jun 12 '19 12:06 mattwakeman

@kierun Could you provide your use case, please ?

gonchik avatar Jun 27 '19 05:06 gonchik

@gonchik Sure. I have a few hundred documents that need importing into Confluence. I have a script that reads the meta data for those, creates a confluence page (via REST API), and upload the document to said page. Obviously, I need to add the meta (author, why it was created, a note saying it was imported, yadda, yadda, …) to the page properties. This can easily be done via a template manually. However, I could not find a way to do it via the REST API apart from creating a blank page, getting the HTML from that (note it is space aware!!!), and using that as a page body.

kierun avatar Jun 27 '19 07:06 kierun

+1 for this feature. It'd be a game-changer.

I want to create a robust template page in Confluence to handle all document structure and styling, then just pass information into that template when creating a new page through python.

Thanks!

AkinBilgic avatar Feb 09 '20 07:02 AkinBilgic

I'm testing this right now and finding that when I get and update a page with representation='storage' the macros don't render - it ends up being static HTML.

source = confluence.get_page_by_id(source_page_id, expand='body.view')
newbody = source['body']['view']['value']
dest =confluence.update_or_create(parent_page_id, 'Clone test', newbody, representation='storage')

I do see multiple representation options documented by Atlassian over here and it says that storage is "our XML storage format".

I also see the supported representations here: https://github.com/atlassian-api/atlassian-python-api/blob/master/atlassian/confluence.py#L35

After some intense brow-furrowing I found an example that instead used expand='body.storage' and realized I was requesting the "VIEW (HTML representation for viewing in a web page)" on the initial call.

Here's the working clone/copy command:

source = confluence.get_page_by_id(source_page_id, expand='body.storage')
newbody = source['body']['storage']['value']
dest = confluence.update_or_create(parent_page_id, 'Clone test', newbody)

As for the templating part, I'm going to throw some identifiable placeholders into the template and do some search-replacing before the update_or_create().

emcniece avatar May 08 '20 00:05 emcniece

body = confluence.get_content_template(TEMPLATE_ID)["body"]["storage"]["value"]
page = confluence.create_page(WIKI_SPACE, "Some title", body)

I've got nothing for replacing placeholders (yet), but this is the basic create-a-page-from-a-template move.

agurgel-te avatar Apr 06 '22 23:04 agurgel-te