Changes for page XML-RPC Java Examples

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

From version 1.1
edited by Vincent Massol
on 2010/01/22 22:36
Change comment: There is no comment for this version
To version 1.2
edited by dilipkumarj
on 2010/01/26 04:08
Change comment: Added example to store a new Page in a particular space

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -xwiki:XWiki.VincentMassol
1 +xwiki:XWiki.dilipkumarj
Content
... ... @@ -5,7 +5,7 @@
5 5  The following libraries should be added to your application classpath:
6 6  
7 7  {{info}}
8 -Most of the libraries can be easily found in the WEB-INF/lib folder of your XWiki instance. As mentioned at [[platform:Features.XMLRPC]], version numbers of these libraries for the most part are irrelevant.
8 +Most of the libraries can be easily found in the WEB-INF/lib folder of your XWiki instance. As mentioned at [[platform:Features.XMLRPC]], version numbers of these libraries for the most part are irrelevant. In the examples listed here, XWiki version 2.1.1 is used.
9 9  {{/info}}
10 10  
11 11  |=Library|=Source
... ... @@ -257,4 +257,78 @@
257 257  
258 258  A page is probably the most important unit of any wiki around which everything else is built. Spaces/Categories, Attachments, Comments, etc are more or less meaningless without a supporting page in a wiki system.
259 259  One of the most widely performed action on any wiki has to be searching for pages. Searches may happen through search boxes provided in a wiki or may be through following links from one page to another. On the XMLRPC side too, searching pages has been given a lot of importance and quite a few methods are available for the same.
260 -Since, this topic is an important one, all the page retrieval/search examples can be found [[here>>XMLRPCJavaExamples2]]
260 +Since, this topic is an important one, all the page retrieval/search examples [[can be found here>>XMLRPCJavaExamples2]]
261 +
262 +== Page: Create A Page ==
263 +
264 +Now for the moment of truth. Addition of Pages to a Wiki is as simple as searching for them. Thankfully, the XWiki XMLRPC api has just the tools you need to do your job.
265 +At the very least, you would require the following three parameters to create a Page:
266 +
267 +1. Space - The Space where the Page is to be stored
268 +1. Title - The title for the Page
269 +1. Content - The content to be displayed inside the Page
270 +
271 +{{warning}}
272 +Please beware that using a title for an existing Page will overwrite all the contents. Make sure no Page exists in the same Space before you attempt to use the create Page functionality of XMLRPC.
273 +{{/warning}}
274 +
275 +In our example below, we would use
276 +
277 +|=Parameter|=Value
278 +|Space|demo code
279 +|title|New Page
280 +|Content|New Page Created
281 +{{info}}This is XMLRPC Test{{/info}}
282 +
283 +{{code language="java"}}
284 +import java.net.MalformedURLException;
285 +import org.apache.xmlrpc.XmlRpcException;
286 +import org.codehaus.swizzle.confluence.Page;
287 +import org.xwiki.xmlrpc.XWikiXmlRpcClient;
288 +
289 +public class CreatePage {
290 +
291 + public static void main(String[] args) throws MalformedURLException, XmlRpcException {
292 +
293 + //URL of the xwiki instance
294 + String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
295 +
296 + //Replace user & pass with desired xwiki username & password
297 + String user = "Admin";
298 + String pass = "admin";
299 +
300 +
301 + XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
302 + try {
303 +
304 + //Perform Login & Authentication
305 + rpc.login(user, pass);
306 +
307 + //Create a Page object & set it's three important attributes viz. Space, Title, Content
308 + //Observe how the \\\\ has been used to create a new line in the final wiki Page
309 + //Also, XWiki syntax can be passed as it is. Here, we passed the info macro
310 + //The info macro would get rendered an info box in the Page
311 + Page page = new Page();
312 + page.setSpace("demo code");
313 + page.setTitle("New Page");
314 + page.setContent("New Page Created \\\\ {{info}}This is XMLRPC Test{{/info}}");
315 +
316 + //Also set the parent Page to "demo code.WebHome" so that the "New Page" we created is not
317 + //an orphan Page
318 + page.setParentId("demo code.WebHome");
319 +
320 +
321 + //Store the page object into XWiki
322 + rpc.storePage(page);
323 +
324 +
325 + } catch (XmlRpcException e) {
326 + System.out.println("invalid username/password was specified or communication problem or ");
327 + System.out.println(e);
328 + } finally {
329 + rpc.logout();
330 + }
331 + }
332 +}
333 +
334 +{{/code}}

Get Connected