Activate applications programmatically

Applies to: Skype for Business 2015

Activating a Microsoft Unified Communications Managed API 5.0 application against Skype for Business Server 2015 typically involves setting a trust relationship between the application and Skype for Business Server 2015, and setting up routing-specific configurations. PowerShell-based programmable activation enables application developers to easily integrate the details of UCMA 5.0 activation into their overall application activation.

The cmdlets needed to create and manage Skype for Business Server 2015 trusted application pools, trusted applications, and trusted application endpoints can be invoked from C# and other CLI-compliant languages, just as other Windows PowerShell cmdlets can be. For information about creating a PowerShell Runspace and executing PowerShell cmdlets.

The following example demonstrates how to invoke a Skype for Business Server 2015 trusted application cmdlet. Note that this code snippet should be compiled as a 64-bit application. Running Skype for Business Server 2015 Manageability cmdlets from 32-bit instances of Powershell is unsupported. If a 32-bit application is required, care must be taken to enable the execution of scripts for 32-bit PowerShell instances.

using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Microsoft.Rtc.Collaboration.Sample.ApplicationActivation
{
  class Program
  {
    static void Main(string[] args)
    {
      // Get the operating environment to run commands.
      InitialSessionState iss = InitialSessionState.CreateDefault();
      iss.ImportPSModule(new[] { "SkypeForBusiness" });
      using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
      {
        myRunSpace.Open();

        // Execute the Get-CsTrustedApplication cmdlet.
        using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
        {
          powershell.Runspace = myRunSpace;
          powershell.AddCommand("Get-CsTrustedApplication");

          Collection<PSObject> results = null;
          Collection<ErrorRecord> errors = null;
          try
          {
            results = powershell.Invoke();
            errors = powershell.Streams.Error.ReadAll();
          }

          catch (Exception ex)
          {
            // An error occurred running the cmdlets.
            // TODO (Left to the reader): Error handling code.
            throw;
          }

          if (errors.Count != 0)
          {
            // Errors were reported by the cmdlets.
            // TODO (Left to the reader): Error handling code.
          }

          foreach (PSObject result in results)
          {
            Console.WriteLine("Identity\t: " + result.Members["Identity"].Value);
            Console.WriteLine("ApplicationId\t: " + result.Members["ApplicationId"].Value);
          }
          Console.WriteLine("Press any key to exit...");
          Console.ReadKey();
        }
      }
    }
  }
}