Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a page within a Confluence Space's Page Tree structure with ajax/jquery

Daniel Baron April 19, 2018

I can create a page in a space using the confluence api and ajax/jquery but I can't specify for it to be created at a particular part of the page tree structure. For instance, the code below will error off even though EXPERTISE/Personal+Pages is part of my Page Tree structure. The code will successfully create a page if I change the bold part to just the name of the space which is EXPERTISE, however I'd like to create the page in EXPERTISE/Personal+Pages. I've been over the documentation at this link but haven't found a clear answer https://developer.atlassian.com/server/confluence/page-tree-api-documentation/.

	// Create a Personal Page
	var new_personalpage_data = {
    	 type: "page",
    	 title: user_name_first + "+" + user_name_last, 
		 space: {
           key: "EXPERTISE/Personal+Pages"
      	 },
    	 body: {
    	   storage: {
    	     value: "<p>This is a new page</p>",
    	     representation: "storage"
    	   }
	     }
        }; 		
	    $.ajax({	
		  type:"POST",
		  url: "https://wiki.company.net/rest/api/content/",
		  data: JSON.stringify(new_personalpage_data),
		  contentType: "application/json; charset=utf-8",  
		  success: function () {alert('Personal Page Created!');},
		  error: function() {alert('Unable to Create a Personal Page for you');}
		})
 	   }

 

2 answers

1 accepted

3 votes
Answer accepted
Sarah
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 19, 2018

Hi Daniel,

I believe the problem you're experiencing is you're supplying a path to your desired page in key: { space:{ "EXPERTISE/Personal+pages"}} when the API only accepts a space key as the input.

In order to create a page as a descendant of another page, you need to supply the id of the parent page. Please see 

https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/#finding-content 

Under the heading 'Create a new page as a child of another page'.

e.g.

 // Create a Personal Page
 var new_personalpage_data = {
      type: "page",
      title: user_name_first + "+" + user_name_last,
"ancestors":[{"id":"page-id"}], space: { key: "EXPERTISE" }, body: { storage: { value: "<p>This is a new page</p>", representation: "storage" } } };  $.ajax({ type:"POST", url: "https://wiki.company.net/rest/api/content/", data: JSON.stringify(new_personalpage_data), contentType: "application/json; charset=utf-8", success: function () {alert('Personal Page Created!');}, error: function() {alert('Unable to Create a Personal Page for you');} });    

In order to get the ID of a page that you know the title and space key of, you can query the API described here:
https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/

under the heading 'Find a page by title and space key'

e.g.

curl -u admin:admin -X GET "http://localhost:8080/confluence/rest/api/content?title=myPage%20Title
&spaceKey=TST&expand=history" | python -mjson.tool

 

I hope this helps with your question.

 

Cheers

Daniel Baron April 20, 2018

Thanks a lot. I'm a little confused about syntax with quotes though. The page id wasn't hard to find but I'm getting a bad request error with the syntaxes

var new_personalpage_data = {
         title: user_name_first + "+" + user_name_last,
         ancestors:[{"id":9030060}],

Sometimes it seems like using quotes around keys is necessary and sometimes not. Does that look right to you?

Stefan Mueller
Contributor
November 2, 2019

can I use the same code in the HTML Macro and trigger it by button?

0 votes
Daniel Baron April 20, 2018

none

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events