How can i create a new page from a users template using rest api

Mykhailo Klimchuk
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 16, 2019

Hi, I want create a new page from a users template. How can i do that using rest api?
If you can give me example on Python, I will be very grateful!
I will also be glad for any help!

5 answers

1 accepted

3 votes
Answer accepted
Stephen Sifers
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 23, 2019

Hello Михаил and welcome to the Community!

To use the REST API to create a page from a users template you will need to complete a few steps. You will need to take this as an approach to find the template, extract the content as a storage value and then pass this through to your REST API call to create a page. Here is how it would look in steps:

  1. Locate an example page with the template applied.
  2. Scrub that page via a REST API call using the following endpoint: GET /rest/api/content (This will allow you to get the storage format).
  3. From this, pass the storage format to your create call using this endpoint: POST /rest/api/content

While these steps are simplified, this process should allow you to create a page based off a user template.

Regards,
Stephen Sifers

Volker Weinreich
Contributor
August 4, 2020

Hello Stephen,

I have the same question and am very grateful for the instructions. But I get stuck on the first step already. What does "Locate an example page with the template applied." mean?

Well, I have a user template. It has a title and an entityid, but I can't get both via GET /rest/api/content. Therefore I need type=page and a "title". If I first create an example page manually, I can of course query this, but that's exactly what I don't want.

Thanks,

Volker

Vir
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 6, 2021

Hi Sephen,

I'm trying to reproduce the workflow that you explain it, but I don't get the storage info in the API Rest call : 

This is my request in Postman :
Captura de pantalla 2021-07-06 a las 11.44.44.png

 

Body response : 

{ "results": [ { "id": "393224", "type": "page", "status": "current", "title": "Template Test Notes", "macroRenderedOutput": {}, "extensions": { "position": 123 }, "_expandable": { "container": "/rest/api/space/PAC", "metadata": "", "restrictions": "/rest/api/content/393224/restriction/byOperation", "history": "/rest/api/content/393224/history", "body": "", "version": "", "descendants": "/rest/api/content/393224/descendant", "space": "/rest/api/space/PAC", "childTypes": "", "operations": "", "schedulePublishDate": "", "children": "/rest/api/content/393224/child", "ancestors": "" }, "_links": { "self": "https://packlink-locally.atlassian.net/wiki/rest/api/content/393224", "tinyui": "/x/CAAG", "editui": "/pages/resumedraft.action?draftId=393224", "webui": "/spaces/PAC/pages/393224/Template+Test+Notes" } } ], "start": 0, "limit": 25, "size": 1, "_links": { "base": "https://packlink-locally.atlassian.net/wiki", "context": "/wiki", "self": "https://packlink-locally.atlassian.net/wiki/rest/api/content?id=393224" }}

 How can I get that info?? 

 

Thankss!!

Volker Weinreich
Contributor
February 4, 2022

Hello Maria,

to get the content in the storage format you have to use the expansions in the REST API.
The request in this case would be


{server}/rest/api/content/{id}?expand=body.storage


This will give you the content of the page with the id in the storage format.

After that you can update a page.

Greetings

Volker

Like Pete Rodgers likes this
0 votes
Lukas Gotter _ Meetical
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
January 19, 2024

We can create basic pages by using the storage format the template API returns. But this feature is important for us to also include images and any other attachments from templates when creating new pages. Is there any workaround? An API to get all images referenced on a certain Confluence template?

0 votes
ant werp
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 21, 2022

Hello

Is it possible to achieve the above via curl ?
If so, how ?

 

thank you

A

0 votes
Dan_Hyman
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 31, 2020

Upvote this so we can get the api method! 


https://jira.atlassian.com/browse/CONFSERVER-11393

0 votes
Lucas Rodrigues de Oliveira
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.
September 20, 2019
 function criaPagina() {
 
      //This creates a page in a space.
      var paginaano = $('#ano :selected').val();
      var demanda = $("#demanda");
      var link = $("#link");
      var jsondata = {
          "type": "page",
          "title": demanda.val(),
          "ancestors": [{
              "id": paginaano
            }],
          "space": {
              "key": AJS.params.spaceKey
          },
          "body": {
              "storage": {
                  "value": "<p>Clique no link abaixo para acessar a demanda no Clarity:</p>" + "<a class='009' href='" + encodeURIComponent(link.val())+"'>Demanda no Clarity</a>" + buscaTemplate(),
                  "representation": "storage"
              }
          }
      };
 
      $.ajax({
          type: "POST",
          url: "/rest/api/content/",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          async: false,
          headers: {
                "Authorization": "Basic " + UserBase64
           },
          xhrFields: {
          withCredentials: false
          },
          data: JSON.stringify(jsondata),
          success: function (response) {
              console.log('Page saved!');
              atualizaPagina(response["id"]);
          },
          error: function (xhr, errorText) {
              console.log('Error ' + xhr.responseText);
alert("Algo deu errado, consulte o administrador da ferramenta para mais detalhes");           }
      });
  }
 
  function buscaTemplate() {
      var dataToReturn = "Error";
      AJS.$.ajax({
          url: "/rest/api/content/" + homePgId + "?expand=body.storage",
          type: "GET",
          dataType: "JSON",
          async: false,
          success: function (msg) {
              dataToReturn = (msg["body"]["storage"]["value"]);
          },
error: function (xhr, errorText) {
              console.log('Error ' + xhr.responseText);
alert("Algo deu errado, consulte o administrador da ferramenta para mais detalhes");           }
      });
      return dataToReturn;
  }
Ashish Kshirsagar
Contributor
October 30, 2019

Is it possible to update template variables while creating new page with the template ? I have a template with 5-6 variables and need to update those to new values when creating new page. 

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events