eeijlar
eeijlar

Reputation: 1342

How do I update an existing page on confluence?

I am using the following code to create a page on Confluence 4.3:

public void publish() throws IOException {

    DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    Date today = Calendar.getInstance().getTime(); 
    XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(CONFLUENCE_URI);
    try {
        rpc.login(USER_NAME, PASSWORD);
        Page page = new Page();                    
        page.setSpace(owrConf.getString(ConfigKeys.CONFLUENCE_SPACE));
        page.setTitle(owrConf.getString(ConfigKeys.CONFLUENCE_PAGE_TITLE) + "_" + df.format(today));       
        List<String> lines = Files.readAllLines(Paths.get("summary.markup"), Charset.defaultCharset());
        StringBuilder b = new StringBuilder();
        for(int i=0; i < lines.size(); i++) {
                b.append(String.format("%s%s", lines.get(i), "\r\n"));
        }  

        page.setContent(b.toString());
        page.setParentId(owrConf.getString(ConfigKeys.CONFLUENCE_PAGE_ID));            
        rpc.storePage(page);
        } catch (XmlRpcException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



}

This works fine, but I am just wondering if I can update an existing page instead of always creating a new one. I can't find API information for Confluence 4.3 to do this.

Upvotes: 0

Views: 1215

Answers (1)

Somaiah Kumbera
Somaiah Kumbera

Reputation: 7489

Take a look at this post that updates a page with Python and XMLRPC.

How can I create a new page to confluence with Python

You can also look at this page https://answers.atlassian.com/questions/316106/how-to-update-a-page-in-confluence-by-java

But really in 2015 you should be using the Confluence REST API to do this kind of stuff. Make a simple post to this url with some JSON data and your page is updated

https://docs.atlassian.com/atlassian-confluence/REST/latest/#d3e941

Update:

  XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
    try {

        //Perform Login & Authentication
        rpc.login(user, pass);

        //Create a Page object to hold our Document information
        Page page = new Page();
        //Fetch the required page. In our example, the page is in Space "demo code"
        //and the Page is "Update Page"
        page=rpc.getPage("demo code.Update Page");
        //Fetch the content of the page & store it in a string for temporary storage
        //This is the present content of the Page
        String presentContent=page.getContent();
        //Create a string that will hold the new content that is to be added to the Page
        String newContent="\\\\Some new content added";
        //Set the content of the page as: present content + new content
        //However, this page is not yet stored to XWiki. It only resides in your application
        page.setContent(presentContent+newContent);
        //Finally, store the "updated" Page to XWiki
        rpc.storePage(page);

        //Just to make sure everything saved all right, fetch the content again for the Page
        System.out.println(page.getContent());

    } catch (XmlRpcException e) {
        System.out.println("invalid username/password was specified or communication problem or ");
        System.out.println(e);
    } finally {
        rpc.logout();
    }

Got this from this page: http://extensions.xwiki.org/xwiki/bin/view/Extension/XML-RPC+Integration+Java+Examples

Upvotes: 1

Related Questions