Learn Skype Web SDK Day 7 : Setting 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
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.
Good to Know
- Before you can run this code, make sure you've read my post on all the prerequisites you need to have in place.
- You don't have to copy the code from here - it's all on GitHub!
- If you liked this post, there are more! Check out the dedicated Skype for Web SDK Collection page, or signup to the newsletter
- Need a Skype for Web SDK solution, but not in a position to build yourself? Hire me!