I was faced with the problem on how to provide a messaging system in an enterprise software that we were creating. Seeing how well Lync worked with all its functionallities and the ability to save history into Outlook folder, I decided to build my own Lync client.
Below are pieces of code to get going with Lync... and as you will see, I am using most of the object except to provide the user with a interactive chat window. What is being done is that user or an application can provide a message to send, and the Lync application will trigger the local Lync client installed on the computer and pass in the message.
To start, we need to check if Lync client is running on the local machine:
try
{
_lyncClient = LyncClient.GetClient();
return true;
}
catch (ClientNotFoundException
ex)
{
MessageBox.Show(ex.ToString());
return false;
}
catch (TypeInitializationException
ex)
{
MessageBox.Show(ex.ToString());
return false;
}
catch (NotStartedByUserException
ex)
{
MessageBox.Show(ex.ToString());
return false;
}
catch (Exception
ex)
{
MessageBox.Show(ex.ToString());
return false;
}
We can then wire up events and check the sign in status of the client. If the client is signed out, we can sign in the client:
if (_lyncClient != null)
{
_lyncClient.StateChanged += new EventHandler<ClientStateChangedEventArgs>
(LyncClient_StateChanged);
// Check if Lync is signed in
if (_lyncClient.State != ClientState.SignedIn)
{
_lyncClient.BeginSignIn(null, null, null, result
=>
{
if (result.IsCompleted)
{
_lyncClient.EndSignIn(result);
InitializeClient();
// Setup application logic
}
else
{
MessageBox.Show("Could not sign in to Lync.");
}
}
, "Local user signing in" as object);
}
else
{
// Set up ConversationManager, ContactManager,
and Self objects
// Wire up events
// Subscribe to events on my contacts
IsLyncSignedIn = true;
InitializeClient();
}
}
//Initialize Lync client
void InitializeClient()
{
if (_lyncClient == null)
_lyncClient = Microsoft.Lync.Model.LyncClient.GetClient();
_conversationManager = _lyncClient.ConversationManager;
_contactManager = _lyncClient.ContactManager;
_self = _lyncClient.Self;
_automation = LyncClient.GetAutomation();
try
{
//Load all
contacts
this.ListContacts();
}
catch (Exception)
{
throw new
Exception("Initialization
exception.");
}
}
//Send Message to selected user
public static void SendIM(String
_selectedUser, String _messageText)
{
try
{
//if ((string)SelectedDataRow[0] != null)
if (_selectedUser != "")
{
if (_messageText != String.Empty)
{
//This is shared method need this to allow automation
Automation automation = LyncClient.GetAutomation();
//Add two URIs to the list of IM addresses.
List<string> inviteeList
= new List<string>();
inviteeList.Add(_selectedUser);
//inviteeList.Add(ConfigurationManager.AppSettings["CallingUserURI"]);
//inviteeList.Add(ConfigurationManager.AppSettings["UserURI2"]);
//Specify IM settings.
Dictionary<AutomationModalitySettings,
object> mSettings = new
Dictionary<AutomationModalitySettings,
object>();
string messageText = _messageText;
mSettings.Add(AutomationModalitySettings.FirstInstantMessage,
messageText);
mSettings.Add(AutomationModalitySettings.SendFirstInstantMessageImmediately,
true);
//Broadcast the IM messages.
IAsyncResult ar = automation.BeginStartConversation(AutomationModalities.InstantMessage, inviteeList,
mSettings, null, null);
csWindow =
automation.EndStartConversation(ar);
//AutoResetEvent completedEvent = new AutoResetEvent(false);
//completedEvent.WaitOne();
//completedEvent.Set();
}
else
{
MessageBox.Show("You must
enter a message");
}
}
else
{
MessageBox.Show("You must enter a valid email");
}
}
catch (Exception)
{
MessageBox.Show("Exception");
}
}
Thats it.
Remember that this is only part of the fun. You can implement almost all the features of Lync into your custom application such as screen sharing, file transfer and more...
No comments:
Post a Comment