Forums

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

Would like to create a wiki page in Confluence using javascript

Phillip Morley November 8, 2018

Hi, I am working within Confluence Server and would like to programmatically create a wiki page based on the values of a form I created.

What would be the best way to achieve this in Confluence? I do have the HTML macro enabled.

Should I consider JavaScript with REST API? 

Thanks for any direction here!

 

 

 

3 answers

1 accepted

0 votes
Answer accepted
Davin Studer
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.
November 8, 2018

You could certainly use the HTML macro and put some script on a Confluence page that adds a form and allows you to create a page via the REST API. I've got lots of pages in our environment where I have put custom scripting on the page. You also don't have to use the HTML macro to do this. That does open up some possible security vulnerabilities that some may not be comfortable with. In that case you could put the code in a user macro and add the macro to the page. It's the same result just maybe a little more secure.

Another option would be the  ConfiForms - Data Forms & Workflows add-on. FYI, I do not work for them. That said we also have this add-on in our environment and it is quite possible the most versatile and useful add-on we have for Confluence. You can use it to create Confluence pages/blogposts, create Jira issues, send emails based on form input, create "databases" within Confluence for holding pretty much any kind of data you can imagine. And you can do all that without having to program it ... however, if you are a programmer they have programming hooks to do more advanced projects. I feel it is pretty reasonably priced too for the power that it adds to Confluence.

Phillip Morley November 8, 2018 edited

Thanks Davin.  Below is the JS I am using to create a new page, but it errors our and returns the content from the home page. Thoughts on why it may be returning the home page? 

//This creates a page in a space. 
var username = "user";
var password = "user";
var jsondata = {"type":"page",
"title":"My Test Page",
"space":{"key":"MRH"}, "ancestors":{"id":"1"},
"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}};

$.ajax ({ type: "POST",
url: "http://localhost:8090/confluence/rest/api/content/",
contentType:"application/json;
charset=utf-8",
dataType: "json",
async: false,
headers: { "Authorization": "Basic " + btoa(username+ ":" + password) },
data: JSON.stringify(jsondata),

success: function (){ alert('Page saved!'); },
error : function(xhr, errorText){ alert('Error '+ xhr.responseText); } });
0 votes
Phillip Morley November 8, 2018

Thanks Davin. After some debugging, the issue was the POST URL for my localhost. Removed "confluence" from the path and it work as expected. 

0 votes
JP _AC Bielefeld Leader_
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.
November 8, 2018

My guess it will be hard to code some Javascript into a Confluence page using the HTML macro, as you can only access the HTML body & might have problems initializing the Javascript. The JS must not conflict with the heavy JS libs Confluence is using by itself. I would probably go for a separate static html page stored on the Confluence server, using Javascript & then generate the page by using the REST API.

Best

JP

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, likes for trees, atlassian community, social impact, tree planting campaign, community kudos, atlassian giving, environmental impact, sustainability, likes for good, atlassian social responsibility, community challenge

Make every click count—help us plant 50,000 trees! 🌳

Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!

Help us plant more trees
AUG Leaders

Atlassian Community Events