Pages Menu
TwitterRssFacebook

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

Learn Skype Web SDK Day 7 : Setting Your Own Presence

Learn Skype Web SDK Day 7 : Setting Your Own Presence

[contemplate-1]
View Demo

We know how to get our own presence, but what about setting it? It’s easy!

We can use the same mePerson object that we used to get presence, by passing the new presence state to the state() method, shown in this example where we set the presence to Do Not Disturb:

var client;
<!-- do sign-in process here -->
client.personsAndGroupsManager.mePerson.state('DoNotDisturb');

Valid presence states you can pass are (and must be typed exactly):

  • Online
  • Busy
  • DoNotDisturb
  • BeRightBack
  • Away
  • OffWork*

*The OffWork state is a slightly odd one because although you can set it, you never see it coming back as a presence state. When you set it, what you see is a presence state of Away, and an activity status of off-work.

In the example I’ve expanded the code from the Get Presence example, and added a drop-down box to allow the setting of presence:

<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>

<div class="form-horizontal">
<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>

<hr/>

<div class="form-horizontal">
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Change Presence:</label>
<div class="col-sm-10">

<select id="newPresence" class="form-control">
<option disabled="disabled" selected="selected">Change Status...</option>
<option value="Online">Online</option>
<option value="Busy">Busy</option>
<option value="DoNotDisturb">Do Not Disturb</option>
<option value="BeRightBack">Be Right Back</option>
<option value="Away">Away</option>
<option value="OffWork">Off Work</option>
</select>
</div>
</div>
</div>

<div>
<span id="loginStatus">...</span><br/>
<span id="presenceStatus">...</span><br/>
<span id="activityStatus">...</span><br/>
</div>

<script type="text/javascript">

$(function () {

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!');
SubscribeToPresenceChanges();

}, 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);
});
});

$('#newPresence').on('change',function(){
client.personsAndGroupsManager.mePerson.status(this.value);
});

function SubscribeToPresenceChanges()
{
client.personsAndGroupsManager.mePerson.status.changed(function(newStatus) {
$('#presenceStatus').text("Presence:" + newStatus);
});

client.personsAndGroupsManager.mePerson.activity.changed(function(newActivity){
$('#activityStatus').text("Activity:" + newActivity);
});
client.personsAndGroupsManager.mePerson.subscribe();
}

});

</script>

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.

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.