A short time ago I was asked by a customer to write a Windows Service that could send Instant Messages to users through Lync. After a bunch of research and digging through MSDN, I decided to use UCMA 3.0 (which is not that latest version) and target Lync 2010.
But, one of the issues I ran into on the project, was that there was a ton of documentation and examples that I had to read through, before I could start. That’s fine, UCMA is a big complicated deal and it is good to have a firm grasp of what is going on. Sometimes, though, you just want to write some code. It turns out, it doesn’t take much to get started.
I’m going to give you a brief pseudo-code overview of how to get things rolling.
Before You Begin
You’re going to need a few things to get started.
- An environment with Lync Server 2010 (or 2013).
- The SIP address and Windows creds for a user you can send messages as.
- A copy of the UCMA 3.0 SDK installed.
Because UCMA is completely ansychronous, many of the examples I saw used a bunch of AutoResertEvents to control program flow. This probably makes the example for clear, but isn’t very useful when you are writing a production application. In reality, you’ll need to chain a bunch of async callbacks together, so I’ve laid out my examples like that.
In your project, you’ll need to add a reference to:
When I wrote my first proof of concept, I put all of this logic into its own class, but you are free to do this however you want. Wherever the logic will live, you need to add a few things to the class file.
- Private UserEndpoint
Once you’ve gotten to this point, you are ready to start. The first thing you have to do is write the code to connect to Lync Server. This is pretty straight forward, I’ve started the chain in a method called… “Connect.”
You should note that this is pseudo-code, but this gist of it is there. For the UserEndpointSettings, you are going to need a SIP address, the FQDN of the Lync Server and you’ll need to set the Credentials property. I’ve also left out the “try…catch” statements, but each “End” call can fail for a variety of reasons (like the Lync Server is unavailable) and should be wrapped.
Once this is done, you are connected and ready to send messages (or do whatever else you want). You’ll have to track somewhere that you are connected, I set a Boolean to true and raised an event so that my calling code knew things were ready.
Here is how you would send a message:
You’ll notice there are two event handlers in there, that I’ve colored green. Welcome to UCMA. To being the process of sending a message, you first have to establish a call. Once the call established, the event for Flow Configuration will fire. The Flow class is what handles the messages that “flow” back and forth. In the configuration event, you need to bind to the StateChanged event. This will tell you lots about the connection, but in this example we are just waiting for the “flow” to become active.
Once the “flow” is active, we can send the actual instant message. Notice how the message was stored in the Call’s ApplicationContext in the first method. ApplicationContext is an object, and can be used to store whatever you need. In my production app, this actually held a class with a bunch of information about the call (overall status, any errors, original message, etc.).
And… there you go. That’s the basics for how to send a message using UCMA 3.0.