Pages Menu
TwitterRssFacebook

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

Learn Skype Web SDK Day 3 : Getting Your Own Presence

Learn Skype Web SDK Day 3 : Getting Your Own Presence

This is one post in a series of more than 30, devoted to helping you get up to speed quickly with the new Skype Web SDK. Each lesson comes with source code (in GitHub) and a working demo page so you can see what it does. Check out the full list of posts on the Skype Web SDK page.


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.

Disclaimer: This is sample code, intended to inform and educate. It is not production-ready and is lacking key components such as error handling. You use it entirely at your own risk. You should fully understand the effects, limitations and risks of the code before executing, and understand the implications of any set-up steps. By using these code examples you are using the Skype Web SDK, so you should read the Skype Software License Terms to which you are agreeing.

Good to Know

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 cookies to help make this website better. By continuing to use this site we’ll assume you’re OK with that (implied consent).