Pages Menu
TwitterRssFacebook

Posted by on Jun 28, 2016 in Development, Skype for Business® (Lync®)

SkypeDevQ: Updating Skype for Business Presence with the Client SDK

SkypeDevQ: Updating Skype for Business Presence with the Client SDK

Derek contacted me recently, asking “I’m looking to build a very simple WinForms app that will change my Skype for Business status to busy when I click one button and change it to available when I click another. What’s the bare minimum I need to accomplish this (I’ve built everything except the Skype for Business integration).”

Well, the easiest way to explain is by showing, so that’s what I’m going to do here. Let’s create a simple application from scratch with two buttons: one to update Skype for Business status to Busy and one to update it to Available.

Setup

I’m going to use a Windows Forms Application for this, but you could just as easily create a WPF application or a Console application. (unfortunately not yet a UWP application).

Using Visual Studio, I do File > New Project and choose the Windows Form Application. A new form, Form1 is created for me. Onto that I’ll use the toolbox to create two buttons for each of the statuses:

2016-06-28 19_52_20-PresenceUpdateApp - Microsoft Visual Studio

Well, that’s the UI sorted! 😉 Now, before we can write any Skype for Business-specific code we need to add a reference to the Lync 2013 Client SDK.

Skype for Business vs Lync 2013: It’s confusing, but there is no Skype for Business Client SDK. However, the Lync 2013 Client SDK works just fine with the latest Skype for Business client. Over time the client will add new features which the SDK won’t know about and there will likely be a feature drift, but for today key functionality remains supported using the Lync 2013 Client SDK.

However…it’s not totally plain-sailing. When you run the installer it will complain that Lync 2013 isn’t installed. You have three options. Either uninstall the Skype for Business client, install the Lync 2013 client, install the SDK, then upgrade the client to the Skype for Business one. Secondly, copy the Microsoft.Lync.Model.dll file from another computer which does have the Lync 2013 client installed and reference it in your project. If you’re feeling adventurous however you could also probably extract the setup.exe package and pick out the DLLs from the installer.

If you haven’t already download and install the Client SDK: https://www.microsoft.com/en-gb/download/details.aspx?id=36824

Having done that, add a reference to the SDK in your project by right-clicking the References branch in the Solution Explorer and choose Add Reference. Navigate to C:\Program Files\Microsoft Office\Office15\LyncSDK\Assemblies and add a reference to Microsoft.Lync.Model.dll.

Let’s write code!

Double click the form to jump to the event handler for when the form loads and add this small piece of test code to make sure that all the references are good. Also, add a using statement for Microsoft.Lync.Model. If you get any errors at this point it’s worth fixing them before continuing:

using Microsoft.Lync.Model;

private void Form1_Load(object sender, EventArgs e)
{
var theClient = LyncClient.GetClient();
Console.WriteLine(theClient.State);
}

All this code does is get a handle to the client and output the current state to the console. When you run this, assuming you are running the client (and if you aren’t you’ll get a nasty exception – this is minimal code remember!) you should see this in the Console window of Visual studio. Right at the bottom of the Console window you can see SignedIn – this is the current state of my Skype for Business client:

2016-06-28 20_19_18-PresenceUpdateApp (Running) - Microsoft Visual Studio

We’re ready to write the code to change presence. Our two buttons do similar things, in that they change presence. Therefore, we’ll create a single function which takes the intended presence state as an argument. I’m going to use the Enum which the SDK uses to represent presence because it’s easier!

private void UpdateStatus(ContactAvailability newStatus)
{

}

Actually updating the presence is done by creating a Dictionary of new information about yourself which you then ‘publish’ to the SDK. Each entry in the dictionary is one of the values in the PublishableContactInformationType enum. You can add as many items into the dictionary as you wish – we’re only going to add one, called Availability, which represents the presence.

private void UpdateStatus(ContactAvailability newStatus)
{
var infoToPublish = new Dictionary();
infoToPublish.Add(PublishableContactInformationType.Availability, newStatus);
}

Actually publishing the new information is a two-stage process, because of the asynchronous way that Skype for Business works. I’ve written about this before so if you’re not comfortable with the Begin…End part of this code, you can read up on it. We get a handle to the SDK again, and then to selfa special object representing the logged in user. From there we can call BeginPublishContactInformation:

private void UpdateStatus(ContactAvailability newStatus)
{
var infoToPublish = new Dictionary();
infoToPublish.Add(PublishableContactInformationType.Availability, newStatus);
var theClient = LyncClient.GetClient();
var self = theClient.Self;
self.BeginPublishContactInformation(infoToPublish, EndPublishContactInformation, self);
}

private void EndPublishContactInformation(IAsyncResult ar)
{
var self = ar.AsyncState as Microsoft.Lync.Model.Self;
self.EndPublishContactInformation(ar);
}

Now that we’ve done that, all we have to do is wire this call up to our two button clicks:

private void btnSetBusy_Click(object sender, EventArgs e)
{
UpdateStatus(ContactAvailability.Busy);
}

private void btnSetAvailable_Click(object sender, EventArgs e)
{
UpdateStatus(ContactAvailability.Free);
}

Run the project again, and you should see that when you click the buttons, presence is updated in the client:

Want to know more?

If Derek’s question has interested you about using the Client SDK, why not have a look at some of the other resources I have for you:

And if you’re new to ThoughtStuff, welcome! A great place to start is the Start Here page which introduces you to everything I have to offer you to help you be awesome at Skype for Business Development.

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.

1 Comment

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.