Display a Drop-down List of Viewer’s Friends in Open Social

Complete example to Display a Drop-down List of Viewer’s Friends in Open Social

var dataReqObj;
var heading = ‘’;
var friends_html;
var TheFriends = new Array();
//function called initially in XML file, this makes data request
//for friend.
 
 
function init() {
    //Create Data Request
    dataReqObj = opensocial.newDataRequest();
    //create viewer request
    var viewerReq = dataReqObj.newFetchPersonRequest(opensocial.IdSpec.
    PersonId.VIEWER);
    dataReqObj.add(viewerReq, ‘viewer’);
    // create friends of viewer request
    var idspec = opensocial.newIdSpec({
        "userId": "VIEWER",
        "groupID": "FRIENDS"
    });
    viewerReq = dataReqObj.newFetchPeopleRequest(idspec);
    dataReqObj.add(viewerReq, ‘viewerFriends’);
    //Send Data Request
    dataReqObj.send(onLoadViewerResponse);
}
//Callback function to process Viewer data requested.
 
 
function onLoadViewerResponse(data) {
    var viewer;
    //retrieve data associated with viewer request.
    try {
        viewer = data.get(’viewer’).getData();
    } catch (err) {
        heading = ‘Error‘ + err.description;
        alert(heading);
    }
    //Set up html to return to display viewer basic info
    try {
        var thumb = viewer.getField(opensocial.Person.Field.THUMBNAIL_URL);
        var profile = viewer.getField(opensocial.Person.Field.PROFILE_URL);
        heading = heading + ‘ < a href = "’ + profile +’" > < img src = "’ + thumb +
‘" > < /a>’;
} catch(err){
heading = ‘Error ‘ + err.description;
alert(heading);}
document.getElementById(’heading’).innerHTML = heading;
/ / Get Friends Information
        var viewer_friends = data.get(’viewerFriends’).getData();
        friends_html = ‘Your Friend,
        ‘;
        friends_html = friends_html + ‘ < select id = select_friend
        onchange = "getFriendStatus();" > ’;
        viewer_friends.each(function (person) {
            friends_html = friends_html + ‘ < option value = ’ + person.getId() + ‘ > ’ + person.getDisplayName() + ‘ < /option>’;
TheFriends[person.getId()] = person;
});
friends_html = friends_html + ‘</select > ’;
            document.getElementById(’friends’).innerHTML = friends_html;
        }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.