For most of its products, Microsoft releases an SDK, using which we can automate a few things on the client side. The Microsoft Lync 2013 SDK is designed for software developers who build custom Microsoft Lync 2013 applications. It is also useful to those developers who embed the collaboration functionality in line-of-business (LOB) applications, which interoperate with other custom Lync SDK clients or with Lync 2013 and Microsoft Lync Server 2013.
However, this is not limited only to developers; IT professionals can also perform tasks using Windows PowerShell. In this topic, we will cover both:
To download the LYNC client SDK refer to the following link—Read the installation documents and choose the 32 bit or 64 bit based on your LYNC client.
https://www.microsoft.com/en-us/download/details.aspx?id=36824
The LYNC 2013 SDK installation is just two steps as shown in the following image.
That's it, we are done with SDK Installation, now we can browse to the default location C:Program Files (x86)Microsoft OfficeOffice15LyncSDKAssembliesDesktopMicrosoft.Lync.Model.dll
So, using the Import-Module
cmdlet, let us load the DLL, as follows:
Import-Module -Name 'C:Program Files (x86)Microsoft OfficeOffice15LyncSDKAssembliesDesktopMicrosoft.Lync.Model.dll'
Now, we will explore the LYNC client SDK in PowerShell:
$client = [Microsoft.Lync.Model.LyncClient]::GetClient() $client | GM -Force
The following figure is the output of the command $client
| GM
| Out-GridView
:
We will see a series of examples to explore the LYNC/S4B (Skype for Business) client.
Retrieve the groups created using the following command:
$client = [Microsoft.Lync.Model.LyncClient]::GetClient() $client.ContactManager.Groups.InnerObject.Name
The output of this command is illustrated in the following image:
Execute the following command:
$client = [Microsoft.Lync.Model.LyncClient]::GetClient() $client.ContactManager | GM
This command returns the members of the ContactManager
which is Microsoft.Lync.Model.ContactManager
that has the BeginAddGroup
method accepting three overloads.
$client = [Microsoft.Lync.Model.LyncClient]::GetClient() $client.ContactManager.BeginAddGroup
The output of this command is illustrated in the following image:
Execute the following command:
$client = [Microsoft.Lync.Model.LyncClient]::GetClient() $client.ContactManager.BeginAddGroup($GroupName, $null,$null)
For now, we will use the $GroupName
parameter and the $null
parameter for the other two overloads.
Function Create-LyncClientGroup{ param([Parameter(Mandatory = $true,ValueFromPipeline = $true)] [String[]]$GroupName ) $client = [Microsoft.Lync.Model.LyncClient]::GetClient() $client.ContactManager.BeginAddGroup($GroupName, $null,$null) } "Test1" | Create-LyncClientGroup
The above code creates a new group in the client called Test1
as shown in the following figure with no contacts:
We haven't started the extensibility API yet, so before that let us see few more client settings options. The below code shows more properties which can be viewed, as well as methods to invoke and perform tasks as shown below, even without a single click!
Execute the following command to check the active audio device:
#To check the Active Audio Device $client.DeviceManager.ActiveAudioDevice
The output of this command is illustrated in the following image:
Execute the following command to check self URI:
#To Check Self URI $client.InnerObject.Self.Contact.Uri
The output of this command is illustrated in the following image:
Execute the following command to check sign-in configuration:
#To Check SigninConfiguration $client.SignInConfiguration
The output of this command is illustrated in the following image:
Execute the following command to check photo display settings:
#To Check if Photo Display Settings $client.Self.PhotoDisplayed
This command returns True
or False
.
Following is the demo code to change the IM Client status:
Import-Module -Name 'C:Program Files (x86)Microsoft OfficeOffice15LyncSDKAssembliesDesktopMicrosoft.Lync.Model.dll' $Client = [Microsoft.Lync.Model.LyncClient]::GetClient() $self = $Client.Self $contactInfo = New-object 'System.Collections.Generic.Dictionary[Microsoft.Lync.Model.PublishableContactInformationType, object]' $contactInfo.Add([Microsoft.Lync.Model.PublishableContactInformationType]::Availability,6500) $ar = $self.BeginPublishContactInformation($contactInfo, $null, $null) $self.EndPublishContactInformation($ar)
This code will change your IM client status to BUSY
. 6500
is the value of BUSY
. Let us make another piece of code to retrieve the values of IM client statuses—So that we can build a custom function for a quick status change without a click!
[Microsoft.Lync.Model.ContactAvailability]::Away.value
To retrieve all values, use the following code:
[enum]::GetValues([System.Globalization.NumberStyles]) | %{ "{0,3} {1}" -f $([int]$_),$_ }
We know that PowerShell is built on .NET framework and we can consume the cmdlets in C#. We have seen a demo in the previous chapter, by applying the same concepts, let us build a module which will have just one cmdlet Get-LyncStatus
.
In PowerShell the code is as follows:
$client.SigninConfiguration.SigninIntranet
This command returns True
or False
—Boolean value. In C# we will build a binary module using LYNC SDK and PowerShell.
C# code is as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Management.Automation; using System.Management.Automation.Runspaces; using Microsoft.Lync.Model; namespace LyncAPIDemo { [Cmdlet(VerbsCommon.Get, "LyncStatus")] public class LyncAPIDemo : PSCmdlet { protected override void ProcessRecord() { var client = LyncClient.GetClient(); bool status = client.SignInConfiguration.SignedInFromIntranet; if(status == true) { Console.WriteLine("You are signed in!"); } else { Console.WriteLine("Singed Off!"); } } } }
Once you have compiled the code in class library, import the module in PowerShell and make use of the cmdlet using the following command:
Import-Module 'c:TempLyncAPIDemoLyncAPIDemoinDebugLyncAPIDemo.dll' Get-LyncStatus
The output of this command is illustrated in the following figure:
Points marked in the figure are explained as follows:
You are signed in!
Very often we may do test audio calls in Lync/Skype for Business. How about automating this? Not only to avoid clicks, but also start audio conversations with others.
The following code is very basic and just makes a test call:
Import-Module 'C:Program Files (x86)Microsoft OfficeOffice15LyncSDKAssembliesDesktopMicrosoft.Lync.Model.dll' $LyncClient = [Microsoft.Lync.Model.LyncClient]::Getclient() $Conversation = $LyncClient.ConversationManager.AddConversation() [Void]$Conversation.AddParticipant($LyncClient.Self.TestCallEndpoint) [Void]$Conversation.Modalities['AudioVideo'].BeginConnect({},0)
Following is the screenshot of the test call:
After the execution of this code, the audio test service begins and we need to manually disconnect. To completely automate this, the following code will help:
#region Import-Module 'C:Program Files (x86)Microsoft OfficeOffice15LyncSDKAssembliesDesktopMicrosoft.Lync.Model.dll' $Client = [Microsoft.Lync.Model.LyncClient]::GetClient() $TestCall = { $Conversation = $this.ConversationManager.AddConversation(); [void]$Conversation.AddParticipant($this.Self.TestCallEndpoint); [void]$Conversation.Modalities['AudioVideo'].BeginConnect({}, 0); Add-Member -InputObject $Conversation -MemberType ScriptMethod -Name EndCall -Value { [void]$this.Modalities['AudioVideo'].BeginDisconnect([Microsoft.Lync.Model.Conversation.ModalityDisconnectReason]::None, {}, 0); } -PassThru } Add-Member -InputObject $Client -MemberType ScriptMethod -Name TestCall -Value $TestCall -Force; #endregion #region $Conversation = $Client.TestCall() Start-Sleep 15 $Conversation.EndCall() #endregion
The call will disconnect after 15 seconds—during the audio test service, the IM service will not work, as shown in the following image:
We can start IM conversation using LYNC SDK and PowerShell. Why? Why not use the IM client? Imagine a scenario where you need to ping a few users for testing purposes, or greet them! There are no concrete reasons for automating IM conversations, but it's good to know the flavors of .NET.
So, to do this we can use the following PowerShell code:
Function Send-SelfIM{ $LyncClient = [Microsoft.Lync.Model.LyncClient]::GetClient() $ConversationManager = $LyncClient.ConversationManager; $Conversation = $ConversationManager.AddConversation(); [void]$Conversation.AddParticipant($LyncClient.Self.Contact) $Conversation.Modalities['InstantMessage'].BeginSendMessage("Okay! This Works",$null,$null); } Send-SelfIM
Well! You can parameterize the text link shown as follows:
Function Send-SelfIM{ Param([Parameter(Mandatory = $true)] [String[]]$Greet ) $LyncClient = [Microsoft.Lync.Model.LyncClient]::GetClient() $ConversationManager = $LyncClient.ConversationManager; $Conversation = $ConversationManager.AddConversation(); [void]$Conversation.AddParticipant($LyncClient.Self.Contact) [Void]$Conversation.Modalities['InstantMessage'].BeginSendMessage($Greet,$null,$null); } Send-SelfIM -Greet "Hi, I am parameterized!"