Changes for page XML-RPC Java Examples

Last modified by Thomas Mortagne on 2023/10/10 14:31

From version 3.2
edited by dilipkumarj
on 2010/01/30 17:30
Change comment: Added example for retrieving a list of attachments for a Page
To version 3.3
edited by dilipkumarj
on 2010/03/14 20:15
Change comment: Added code snippet for Creating New User through XML-RPC

Summary

Details

Page properties
Tags
... ... @@ -1,1 +1,1 @@
1 -xmlrpc|java|example|page|space|authentication|create|delete|update
1 +xmlrpc|java|example|page|space|authentication|create|delete|update|attachment
Content
... ... @@ -547,3 +547,83 @@
547 547   }
548 548  }
549 549  {{/code}}
550 +
551 += User: Create A New User =
552 +
553 +This is an Administrative function in XWiki & needs Admin privileges for the User with whose credential we connect through the XML-RPC. Creating a new User involves the following steps:
554 +
555 +1. Create a Page with the desired "UserName" as the Page Name
556 +1. Create this Page in the "XWiki" space
557 +1. Set the properties of the newly created User
558 +
559 +Please read the comments in the example below to understand the detailed steps:
560 +
561 +{{code language="java"}}
562 +
563 +import java.net.MalformedURLException;
564 +import java.security.NoSuchAlgorithmException;
565 +import org.apache.xmlrpc.XmlRpcException;
566 +import org.codehaus.swizzle.confluence.Page;
567 +import org.xwiki.xmlrpc.XWikiXmlRpcClient;
568 +import org.xwiki.xmlrpc.model.XWikiObject;
569 +
570 +public class CreateUser {
571 +
572 + public static void main(String[] args) throws MalformedURLException, XmlRpcException, NoSuchAlgorithmException {
573 +
574 + //URL of the xwiki instance
575 + String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
576 +
577 + //Replace user & pass with desired xwiki username & password
578 + String user = "Admin";
579 + String pass = "admin";
580 +
581 +
582 + XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
583 + try {
584 + //Perform Login & Authentication
585 + rpc.login(user, pass);
586 +
587 + //Create a Page object & set it's three important attributes viz. Space, Title, Content
588 + //In our example, testuser is the Page Name
589 + //We set the content of the Page to automatically load XWiki.XWikiUserSheet
590 + //This sheet is used to display the User Profile in the default format
591 +
592 + Page page = new Page();
593 + page.setSpace("XWiki");
594 + page.setTitle("testuser");
595 + page.setId("XWiki.testuser");
596 + page.setContent("{{include document=\"XWiki.XWikiUserSheet\"/}}");
597 + rpc.storePage(page);
598 +
599 + //Set properties for the newly created user "testuser"
600 + //List of all properties available in the XWiki.XWikiUsers class in your wiki
601 + //Edit XWikiUsers class in class editing mode to access all properties or define new ones
602 +
603 + XWikiObject xobj = new XWikiObject();
604 + xobj.setClassName("XWiki.XWikiUsers");
605 + xobj.setPageId("XWiki.testuser");
606 + xobj.setProperty("first_name", "Test");
607 + xobj.setProperty("last_name", "User");
608 + xobj.setProperty("password","asdfjk");
609 + rpc.storeObject(xobj);
610 +
611 + //Finally, associate the user to the XWikiAllGroup
612 + //We simply associate the XWiki.testuser Page to the XWiki.XWikiAllGroup Page
613 + //Set the "member" property to the desired XWiki UserName
614 +
615 + XWikiObject xobjgrp = new XWikiObject();
616 + xobjgrp.setClassName("XWiki.XWikiGroups");
617 + xobjgrp.setPageId("XWiki.XWikiAllGroup");
618 + xobjgrp.setProperty("member","XWiki.testuser");
619 + rpc.storeObject(xobjgrp);
620 +
621 + } catch (XmlRpcException e) {
622 + System.out.println(e);
623 + } finally {
624 + rpc.logout();
625 + }
626 + }
627 +}
628 +
629 +{{code}}

Get Connected