Introduction
Yesterday boss said: Send Lync message! Oh Lord I thought, no problem, Google is my friend. But I found no Google hits. Help me Lord, do I have to do it from scratch? I looked in Lync SDK 10 samples, and found no good code. I found some articles on MSDN on what to do: Start a conversation, choose a modality, send a message. But no working code on How do I do it? So I scratched my head and started. After two days, I achieved this...
The Starting Code
Option Explicit On
Option Strict On
Imports Ly = Microsoft.Lync.Model
Imports Lyc = Microsoft.Lync.Model.Conversation
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Imports System.Text
Imports System.Reflection
Public Class frmMain
Private ReadOnly gMessage As String = "Kilroy was here!"
Private gClient As Ly.LyncClient
Private gConversation As Ly.Conversation.Conversation
Private gContact As Ly.Contact
Private gParticipiant As Lyc.Participant
Private gSearchIsFinished As Boolean = False
The Following Code
Private Sub btnGo_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles btnGo.Click
Dim LyncUri As String = String.Empty
gSearchIsFinished = False
LyncUri = GetLyncId(ADname)
If Not String.IsNullOrEmpty(LyncUri) Then
gClient = Ly.LyncClient.GetClient()
gClient.ContactManager.BeginSearch_
(LyncUri, Ly.SearchProviders.GlobalAddressList, Ly.SearchFields.EmailAddresses, _
Ly.SearchOptions.IncludeContactsWithoutSipOrTelUri, 1, AddressOf SearchCallback, _
New Object() {gClient.ContactManager, LyncUri})
While Not gSearchIsFinished
System.Threading.Thread.Sleep(500)
End While
gConversation = gClient.ConversationManager.AddConversation()
gParticipiant = gConversation.AddParticipant(gContact)
Dim modal = DirectCast(gConversation.Modalities_
(Lyc.ModalityTypes.InstantMessage), Lyc.InstantMessageModality)
modal.BeginSendMessage(gMessage, AddressOf SendMessageCallback, Nothing)
End If
End Sub
Private Sub SendMessageCallback(ByVal r As IAsyncResult)
gConversation.End()
gClient = Nothing
End Sub
Private Sub SearchCallback(ByVal r As IAsyncResult)
Dim asyncState As Object() = DirectCast(r.AsyncState, Object())
Dim cm As Ly.ContactManager = DirectCast(asyncState(0), Ly.ContactManager)
gContact = Nothing
Try
Dim results As Ly.SearchResults = cm.EndSearch(r)
If results.AllResults.Count = 0 Then
ElseIf results.AllResults.Count = 1 Then gContact = results.Contacts(0)
End If
Catch se As Ly.SearchException
MessageBox.Show(se.Reason.ToString())
Finally
gSearchIsFinished = True
End Try
End Sub
Explanation
- Start by setting a reference to Lync DLLs. I got them by downloading Lync SDK 10.
- Import them into your program.
- Get a grab on the Lync client.
- Get the user URI, we have ours in the Active Directory.
- Ask Lync to look it up.
- A Lync URI is usually in the format sip:someone@somewhere.se.
- Wait for the callback to be done.
- Send the message.
Sample of GetLyncId
Shared Function GetLyncId(ByVal Name As String) As String
GetLyncId = String.Empty
Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://OU=Local Data,DC=XXXX,DC=local")
Dim srch As New DirectorySearcher(Entry, _
"(&(objectClass=user) (Name=" & Name.Trim & "))")
With srch
.SearchScope = SearchScope.Subtree
.SizeLimit = 100
.PropertiesToLoad.Add("msrtcsip-primaryuseraddress")
End With
Dim resEnt As SearchResult = srch.FindOne()
If Not resEnt Is Nothing Then
GetLyncId = CStr(resEnt.Properties("msrtcsip-primaryuseraddress").Item(0))
If GetLyncId.StartsWith("sip:") Then
GetLyncId = GetLyncId.Split(":"c)(1)
End If
End Function
Conclusion
What I have to do next is encapsulate it in a module (static
class if you are a C#er).
If you read this and used the code, got it working, and have ideas or suggestion/ideas for improvement, please let me know.