If you have a need to launch an application from your VB.Net app, but need to wait till the application has completed loading before you continue processing then check out the snippet below.
I have seen many ways of doing this, mainly based around wait timers or CPU levels.. However, so far I have found this one to be the most reliable..
So todays challenge was around handling the ModalityState change on the Lync 2013 client. The ModalityState change event happens during certain stages of Lync calls, and client actions. The ModalityState is part of the Microsoft.Lync.Model.Conversation namespace. We are using it to configure certain custom Lync Status’ dependant of the current state of the client.
To capture and handle these events, you need to create and event handler for the conversation manager namespace and for ModalityStateChangedEventArgs. The code below is written in VB.Net but could easily be transferred to C# etc.
In my example, I am only interested in the AudioVideo modality state changes.
Imports Microsoft.Lync.Model
Imports Microsoft.Lync.Model.Conversation
Imports Lync = Microsoft.Lync.Model.Conversation
Public Class frmMain
Private _LyncClient As LyncClient
Public WithEvents _ConversationMgr As Microsoft.Lync.Model.Conversation.ConversationManager
Public WithEvents _conv As Conversation
Private Sub MainWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
_LyncClient = LyncClient.GetClient()
_ConversationMgr = _LyncClient.ConversationManager
DisplayCurrentState()
Catch generatedExceptionName As ClientNotFoundException
MessageBox.Show("client is not running")
End Try
End Sub
Private Sub _ConversationMgr_ConversationAdded(ByVal sender As Object, ByVal e As Microsoft.Lync.Model.Conversation.ConversationManagerEventArgs) Handles _ConversationMgr.ConversationAdded
AddHandler e.Conversation.Modalities(ModalityTypes.AudioVideo).ModalityStateChanged, AddressOf AVModalityStateChanged
End Sub
Private Sub AVModalityStateChanged(ByVal sender As Object, ByVal e As ModalityStateChangedEventArgs)
Select Case e.NewState
Case ModalityState.ConnectingToCaller
'Insert your code here
Case ModalityState.Connecting
'Insert your code here
Case ModalityState.Joining
'Insert your code here
Case ModalityState.Transferring
'Insert your code here
Case ModalityState.Disconnected
'Insert your code here
Case ModalityState.Disconnecting
'Insert your code here
Case ModalityState.Forwarding
'Insert your code here
Case ModalityState.OnHold
'Insert your code here
Case ModalityState.Suspended
'Insert your code here
End Select
End Sub
So as you can see, it’s quite a simple concept – which holds lots of potential!
Microsoft Lync is Microsoft’s latest offering of a unified communications system. Sporting features you’d expect to find in a SIP/Unified Comms client, Lync is a pretty powerful piece of kit.
What’s more, Microsoft offer an SDK to enable easy development of tools and add-ons using Lync technologies. Most of the tutorials and blog posts seem to focus on either C# or Silverlight, which is fine – however, as I am primarily a VB.Net developer I decided to publish my own guide on how to intercept and respond to received Lync messages.
Firstly, I need to point out that I have only had access to Lync for a couple of months now and only downloaded the SDK earlier this month.. So although the code below is functional, I am constantly learning and may find a better way of doing things as my learning continues. To make things easier, make sure you subscribe to the post and you’ll get an update each time I adjust the code.
You will need to download and install the Microsoft Lync SDK then fire up your copy of Visual Studio, create a new windows form project. Obviously you could use any type of project here, however I have plans for a GUI in the future so I am using a windows form project.
Now, open the code view of your new form and import the following:
1
2
Imports Microsoft.Lync.Model
Imports Microsoft.Lync.Model.Conversation
Now, under your “Public Class Form1” but before your “Private Sub Form1_Load()” enter the following lines of code:
That’s all there is to it for the declarations, as you can see I have used ‘WithEvents’ to expose the ‘methods’ in the GUI and make things a little easier developing with them.
Next in the “Form_Load()” event, paste in the following code:
This essentially ties your code to the Lync client running on your machine, then using ‘automation’ it confirms the client is signed in, if not it signs in for you.
Now, your app is hooked into the Lync client, you will need to ‘capture’ the message received event and handle it appropriately.
As you can see from the post above, I am using the built in replace functionality of the string. This is because the Lync client passes line feeds after its message, which makes handling the received message quite complicated.
So, from the code above – each time an instant message is received by your Lync client the variable strRec will contain the received message.
Now, upon receipt of a message you may want to send a response – which is where my next code snippet comes in. The following Sub send’s an instant message to the participants of the conversation:
1
2
3
4
5
6
7
8
PublicSub SendIM(ByVal strMessage AsString)
Dim modal=DirectCast(LycConversation.Modalities(Lyc.ModalityTypes.InstantMessage),InstantMessageModality)
From the code above, you should see the sub requires a string to be passed to it. This is what will be relayed to the client who sent the original message.
So there you have it, a fairly simple way of receiving and sending back a Lync ‘IM’.
Obviously, you may want to improve this slightly by trimming the received message and handling it depending on the users request. So, for example – you may want to ‘serve’ a simple weather forecast based on the users request. To do this all you need to do is enhance the ‘_LocalIMModality_InstantMessageReceived’ sub to include code something like this:
29:SendIM("The weather forecast for "&locationId&" is: "&node.GetAttribute("text",ns.DefaultNamespace).ToString()&", with a temperature of "&node.GetAttribute("temp",ns.DefaultNamespace).ToString()&"°C")
30:EndWhile
Exit Sub
ErrHand:
SendIM("I can't seem to fetch the weather forecast right now "&Err.Description&" - "&Err.Number&" - "&Erl())
EndSub
As you can see from the code above, I have also built in the functionality for the Lync user to request weather for a couple of locations around the UK. This could be increased to as many as you want, or removed to only send details of one particular place.
Some other things you might want to consider would be for your ‘Bot’ to query a SQL table and pass back the results, or perhaps perform other functionality such as PING a device and send the reply details in an IM.
In a future blog post I will show you how to connect your new ‘BOT’ into a AI handler to provide an AI Bot capable of holding a ‘conversation’ of sorts.
As per my opening paragraph, I am very much in the early days of my Lync development – so if you have any suggestions on how I could improve the code above, please get in touch.
This week at work has brought with it a bit of a fresh challenge. As anyone working on large networks will be aware maintaining user accounts and security groups comes with its pitfalls.
To try and iron out some of the challenges we face I’m currently in the process of building a .Net app. Which uses the System.Directory.AccountManagement, along with several other built in functions (to be detailed later) to automate a large part of our day to day management.
As many of you will know I’m a big fan of automation, and not to do anyone out of a job.. But to free up staff to work on more pressing matters. An anyway, who wants to have to deal with 550+ security groups when maintaining AD users.
The project has taught me a lot about the frameworks. Some of which ill be building code snippets from, so watch this space!
Here it is.. My current ‘Flag Ship’ application. CRM 09 is an application I am developing for my dad, in order to speed up entry of data relating to customer visits and potential sales.
The application is built in Visual Basic.Net with an SQL Server database backend.
Screenshot’s coming soon
If you would like more information please contact me using one of the methods on the contacts page.
Current Features: (In menu order)
Built in back up and restore feature for both the dataase and application files
Creation of customer contact details
Full integration with Outlook Contacts
Full integration with Outlook Calendar
Visual ‘WYSIWYG’ report designer
Built in mobile device handling
Integrated mappingdirectionsroute planning system
Ability to sync with a handheld device (Windows Mobile)
User levels, and Security
Automatic update checksdownloadsinstallation
Features Under Development
Updating of backend database via GPRSWAP connection
Support for seperate sales areas
Generation of suggested visit lists, to ensure no customer is left without regular visits
Trial Version with number of executions limitation, unlocked on paid registration