cogShow how to build a Google gadget that can be installed in any iGoogle gadget container
Developed by


0 Votes
LicenseGNU Lesser General Public License 2.1


Note that it supports the token-based authentication mechanism.


The gadget (GadgetCode.Gadget)

This document will provide the gadget to your gadget container. It does not necessarily needs to be hosted on your wiki instance, but well... it's easy this way emoticon_wink If hosted on your wiki instance, the final URL of the gadget (that you will provide to the gadget container) will look like this : http://yourserver/xwiki/bin/view/GadgetCode/Gadget?xpage=plain. Remember to give the view right to XWikiGuest for this page, or your container will complain not being allowed to access it (401). You may also want to include this code from another page (using the includeInContext macro), in order to have a nicer URL (like http://yourserver/xwiki/bin/view/Gadget/) and a separation between the code and the page exposed (including its rights). This is up to you, really !

{pre }
<?xml version="1.0" encoding="UTF-8" ?>
<ModulePrefs title="My XWiki Gadget">
  <Require feature="dynamic-height"/>
<UserPref name="server"
   display_name="XWiki Server URL"
   datatype="string" />
<UserPref name="username"
   datatype="string" />
<UserPref name="token"
   display_name="Authorization Token"
   datatype="string" />
<Content type="html">
  <div id="content">
  <script type="text/javascript">
 var prefs = new gadgets.Prefs();
 function doRequest() {    
  var params = {};  
  params[] =;
  var args = "xpage=plain&token=" + prefs.getString("token") + "&username=" + prefs.getString("username");
  var url = prefs.getString("server") + $xwiki.getURL("GadgetCode.Service","view") + "? + args;, responseHandler, params);
 function responseHandler(obj) {  
  document.getElementById('content').innerHTML = obj.text;

{/pre }

The back-end service (GadgetCode.Service)

The code in this service page will handle the asynchronous requests made by the gadget itself. It will return HTML to display inside the gadget upon response. In this example, we will return a list of pages the gadget user has created and that has been recently modified. Replace the code between the placeholders to fit your specific gadget purpose.

* Service for handling a google gadget asynchronous requests.
* Authentication is token-based. The token is a hashcode of the concatenated
* username and password of one use.)
* @programming
* @internalapi to retrieve the user doc, because we are called as XWikiGuest.
* @ajaxservice called by a google gadget container.
{pre }
#set($token = $request.token)
#set($username = $request.username)
#if($token && $username)
 #if($validToken == $token)
$context.context.setUser("XWiki.${username}") ## set to valid user
  ## BEGIN Placeholder for the service logic code starts here. Replace the code below, until END, by yours
   #set($hql = "where doc.creator='$context.user' order by desc")
  <h2>Latests modifications on the pages I've created :</h2>
   #foreach($docName in $xwiki.searchDocuments($hql, 10, 0))
   <li><a href=$tDoc.getExternalURL() target="_new">$tDoc.fullName</a></li>
  ## END Placeholder for the service logic code starts here
 #else ## invalid token
Invalid combination of username and token.
Request parameters are wrong.
{/pre }

The token provider (GadgetCode.TokenProvider)

Finally, this page will be useful to provide the wiki users their authentication token. As for the gadget page, you might consider including this code from an other page, to have a nice separation between the logic and the UI.

#set($userdoc = $xwiki.getDocument($context.user))
#set($token = "${userdoc.fullName}${userdoc.getObject('XWiki.XWikiUsers').getProperty('password').value}")
#set($token = $token.hashCode())
#info("Token for $context.user : $token")

Google Gadget development

See for more information. In development mode, you might want to use the gadget developer sandbox. Remember to uncheck the "Cached" checkbox to see your changes when you refresh your iGoogle page.


Hereunder is the result as you can observe it in the iGoogle gadget container:

Settings view


Result view

TBD. (iGoogle gadget sandbox does not work properly today, see this thread)

Tags: google gadget

Get Connected