It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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 Apr 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

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?

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

How is your team having fun and bonding, remotely, utilizing Confluence?

Thanks everyone for answering last week’s question. The winner of the random drawing from those who commented is: @LarryBrock I’ll contact you separately with your prize details. This wee...

327 views 9 7
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you