Print This Post

javascript proxy for the SharePoint User Profile web service

I’ve updated the javascript API for the SharePoint web services to version 1.0.1. This now includes support for the User Profile web service (/_vti_bin/UserProfileService.asmx) allowing user profiles to be retrieved and updated.

The extra proxy is located in the SPAPI_UserProfile.js file. The following example demonstrates the usage of the proxy by getting the first name and preferred name of a user from their login name.

var profile = new SPAPI_UserProfile('')
var profileData = profile.getUserProfileByName('GLASGOW\\darrenj');

if (profileData.status == 200)
{
    var properties = profileData.responseXML.getElementsByTagName('PropertyData');
    var propertyValues = new Array();

    for (var i=0; i<properties.length; i++)
    {
        var propName = properties[i].getElementsByTagName('Name')[0].childNodes[0].nodeValue;
        propertyValues[propName] = properties[i].getElementsByTagName('Value');
    }

    alert('First name is: ' + propertyValues['FirstName'][0].childNodes[0].nodeValue);
    alert('Preferred name is: ' + propertyValues['PreferredName'][0].childNodes[0].nodeValue);
}
else
{
    alert('There was an error: ' + profileData.statusText);
}

The javascript above parses the response XML and creates a hash table of XML nodes keyed by the property name. Note that a property may have more than one value so the hash table stores arrays of these values.

So to get the user’s first name we use:

propertyValues['FirstName'][0].childNodes[0].nodeValue;

…and for the preferred name…

propertyValues['PreferredName'][0].childNodes[0].nodeValue;

I’ll post some more examples on the use of the profile service when I get a chance.

Post a Response