Microsoft Lync and Skype for Business have a rich set of .NET APIs which make it easy to extend the platform and integrate it with other applications. This blog helps explain how to use those APIs.

Looking up a user’s SIP address

Posted: June 22nd, 2011 | Author: | Filed under: Lync Development, OCS Development | Tags: , , | No Comments »

In writing applications to extend OCS or Lync — especially applications that mimic client functionality — I’ve run into a number of situations where the application has a user’s domain and username but needs to figure out the user’s SIP URI in order to perform OCS/Lync operations on behalf of that user. There is not much documentation out there on how to do this, and it’s hard to find. So I wanted to write up a quick explanation of how to look up the SIP URI of a given domain account from Active Directory.

I’ll start with a couple of notes. First, it’s true that some organizations by convention use a SIP URI format that makes it easy to derive the SIP URI from the username with a bit of string manipulation. For instance, if all users have SIP URIs of the form sip:username@fabrikam.com, the application can put together the SIP URI itself by concatenating the username with “@fabrikam.com”. Also, in many cases SIP URIs by convention are the same as email addresses with sip: added to the front. If this is the case, an application that has the email address of the user can simply add sip: to produce the SIP URI.

The important thing to note is that while some organizations use these SIP URI formats by convention, there is nothing to prevent an administrator from giving a user a completely different SIP URI. If you are creating an application that needs to work in environments besides your own, it is potentially dangerous to assume that all SIP URIs will follow a consistent format like the ones described above.

This isn’t a huge deal, because retrieving SIP URIs for users, assuming you can connect to Active Directory to do an LDAP query, is relatively simple. The SIP URI for a user (at least, for users who are UC-enabled and have SIP URIs assigned) is stored in the msRTCSIP-PrimaryUserAddress property. There are plenty of articles out there on the Internet describing how to retrieve information from Active Directory, so I won’t go into all the details here, but you can use code that looks something like this:

public string GetSipUri(string cn, string ou)
{
 DirectoryEntry entry = new DirectoryEntry(
  "<a href="ldap://dc.fabrikam.com/cn">LDAP://dc.fabrikam.com/cn</a>=" + cn + ", ou=" + ou +
  ", dc=fabrikam, dc=com");

 PropertyCollection properties = entry.Properties;
 return properties["msRTCSIP-PrimaryUserAddress"].ToString();
}

You’ll need to reference System.DirectoryServices to do this. Basically what you are doing is retrieving the object representing the user via LDAP, and grabbing the msRTCSIP-PrimaryUserAddress property from that object.

If you want to do the reverse of this (look up the username based on a SIP URI) you can use the DirectorySearcher object, with a search filter like the following:

searcher.Filter = "(&(objectClass=user)(msRTCSIP-PrimaryUserAddress=sip:michael@fabrikam.com))";

Feel free to get in touch if you have any questions!