Pages Menu
TwitterRssFacebook

Posted by on May 5, 2015 in Learn Skype Web SDK

Learn Skype Web SDK Day 3 : Getting Your Own Presence

Learn Skype Web SDK Day 3 : Getting Your Own Presence

[contemplate-1]
View Demo

Once you’ve logged in, you can start to access information about users, including yourself.

Skype for Web API makes it easy to get at your own information, with the mePerson object. You can access this from the Application object, once you’re signed in.

From the mePerson object, the state object gives you your current presence state (but this isn’t the best way, see below).


var client = = new Skype.Web.Model.Application;
// do sign-in process here
var presenceState = client.personsAndGroupsManager.mePerson.status;

The return object is a Status Property, with a value which represents the presence state. Valid presence states (that I’ve found) are:

  • Online
  • Busy
  • DoNotDisturb
  • BeRightBack
  • Away

However, just getting the state by referencing the property isn’t the best, because there’s no guarantee that the property will be populated. If you read the property really soon after logging in, you may find that it’s null, because it’s not been filled in yet by the server.

That’s why, for all properties, you should always register a changed listener for properties. That way, your code will never end up with a null value, it will wait (asynchronously, nicely) until the property is ready, then carry on.

A better way of doing the above would be:


var client;
// do sign-in process here to instantiate the client object
client.personsAndGroupsManager.mePerson.status.get().then(function (presenceState ) {
// do something with the presence value
});

A more robust way of getting the presence is to set up an event from when it changes. This will be covered in the next post.


<div class="form-horizontal">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="username" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-default" id="btnLogIn">Log in</button>
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-default" id="btnLogOut">Log out</button>
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-default" id="btnPresence">Get Presence</button>
</div>
</div>
</div>

<div>
<span id="loginStatus">...</span>
</div>

<script type="text/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!');

}, 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);
});
});
$('#btnPresence').click(function () {

client.personsAndGroupsManager.mePerson.status.get().then(function (theStatus) {
alert(theStatus);
});
});

});

</script>

<span style="line-height: 1.5;">

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]

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

2 Comments

  1. If Skype for business is already running in our server, do we need to set the cswebserviceconfiguration to allow it into a trusted domain to try out the example?

  2. your example are very nice and looks simple
    but when I input real working account ( username and pwd) of SkypeForBusiness

    javascript return me this:

    https://pipe.int.trafficmanager.net/Client/2.0/
    The requested resource does not support http method ‘GET’.

    i have tested in different variants – no result

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.