Learn Skype Web SDK Day 9 : Getting & Displaying Contact Photos
[contemplate-1]
View Demo
In Skype for Business, people have photos associated with them, so that they can easily be identified. Often these are administered in Active Directory alongside other information such as job title, department etc.
In Skype for Web API these photos can be accessed. They are a property of the person object, which means they can get accessed with a get() comamnd. The property name is avatarURL. To get the value you would first get a handle to the person object (which can be yourself, or another contact) and then call get() on the property:
[code language=”javascript”]
person.avatarUrl.get().then(function(value) {
//do something with the photo
});
[/code]
The return object isn’t the actual photo – it’s a URL describing the photo. For instance, you might get back this URL:
https://lyncweb.thoughtstuff.co.uk/ucwa/oauth/v1/applications/114079659624/photos/[email protected]
This URL has been crafted together for us by Skype for Web API (well, actually UCWA but it doesn’t matter) and if you were to load that in a browser, you’d see the image.
This means that you can provide the URL as the source attribute to a HTTP image container you have created, or do something else with it in JavaScript etc. To set it as the source for an image you might do this:
[code language=”javascript”]
$(‘#imgContactPhoto’).attr(“src”,value);
[/code]
Here’s a full code sample which demonstrates these functions:
[code language=”javascript”]
var client; $(function () { 'use strict';
Skype.initialize({ apiKey: 'SWX-BUILD-SDK', }, function (api) { client = new api.application();
// whenever client.state changes, display its value client.signInManager.state.changed(function (state) { $('#loginStatus').text("Login State: " + state); });
}, function (err) { alert('Error loading Skype Web SDK: ' + err); });
$('#btnLogIn').click(function () {
// start signing in client.signInManager.signIn({ username: $('#username').val(), password: $('#password').val() }).then(function () { //log in worked! alert('Logged in!'); GetOwnPhoto();
}, function (error) { //Something went wrong. alert(error); }); });
$('#btnLogOut').click(function () { // start signing out client.signInManager.signOut() .then(function () { //log out worked! alert('Logged out!'); }, function (error) { //Something went wrong. alert(error); }); });
$('#btnChangeContactPhoto').click(function () { GetContactPhoto($('#contact').val()); });
function GetOwnPhoto() { var me = client.personsAndGroupsManager.mePerson;
me.avatarUrl.get().then(function (value) { $('#imgPhoto').attr("src", value); });
}
function GetContactPhoto (contactSIP) { var query = client.personsAndGroupsManager.createPersonSearchQuery(); query.text(contactSIP); query.limit(1); query.getMore().then(function (results) { results.forEach(function (result) { var person = result.result; person.avatarUrl.get().then(function(value) { $('#imgContactPhoto').attr("src",value); }); }); }) }
});
[/code]
Demo Online
You can try this code out against your own Skype for Business environment by going to the demo page. From here you can also download the code from GitHub if you want to host it locally, or take it and use it in your next project.
[contemplate-2]


