Would like to create a wiki page in Confluence using javascript
Asked by Phillip Morley:

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!


Answers:
author: Davin Studer
body: 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](https://marketplace.atlassian.com/apps/1211860/confiforms-data-forms-workflows?hosting=server&tab=overview) 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.

comments: author: Phillip Morley
body: 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); } }); 
```


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


author: JP _AC Bielefeld Leader_
body: 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


Tags: confluence-server

server