Request and Display Basic Viewer Data in Open Social

Step 1. Request data:
a. Create DataRequest object by calling opensocial.newDataRequest.
b. For each request you wish to make, create it using one of the* methods.
c. For each piece of data you want to request, add a request via DataRequest.add(request).
d. Make a request by registering the callback function, DataRequest.send(callback).

Step 2. Create a callback function to receive data:
a. Once a container gets the request and it is processed, the callback function will be executed. It will be passed the opensocial.DataResponse object that contains the response data of the processed request. In the following code example, the DataResponse object is received as a parameter to the callback function, and is called dataResponse:

function callback(dataResponse) {
//process the response data

Complete Example:

var dataReqObj;
var heading = ‘’;
//function called initially in XML file, this makes data request
//for viewer
function init() {
    //Create Data Request
    dataReqObj = opensocial.newDataRequest();
    //depending of version of OpenSocial, viewer reference changes
    var viewerReq = dataReqObj.newFetchPersonRequest
    ( opensocial.IdSpec.PersonId.VIEWER);
    dataReqObj.add(viewerReq, ‘viewer’);
    //Send Data Request
//Callback function to process Viewer data requested.
function onLoadViewerResponse(data) {
    var viewer;
    //retrieve data associated with viewer request.
        viewer = data.get(’viewer’).getData();
        heading = ‘Error ‘ + err.description;
    alert (heading);}
    //Setup html to return to display viewer basic info
        heading = ‘Hello, ‘ + viewer.getDisplayName();
        var thumb =
        var profile =
        heading = heading + ‘<a href="’ + profile +’">&lt;img src="’
        + thumb
        + ‘"&gt;</a>’;
        } catch(err){
        heading = ‘Error ‘ + err.description;
    //Display results inside XML document at the div
    //element with id heading
    document.getElementById(’heading’).innerHTML = heading;

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.