Cookie CSS

Saturday, December 27, 2014

Building the Ultimate Lync Presentation Tool Helper - Part 6

Today we wrap up this series with part 6.  The last piece of the puzzle in our ULPTH tool is going to be desktop / application sharing.  Last week we showed how to share whiteboards, ppts, and files, so this week we'll extend the sharing to cover live display of something on our computer.


































I have hidden those features in the 2nd row of the toolbar under my name and status.  You can see the 3 buttons, Share primary desktop, Stop Sharing, and a pull down that contains the other monitors and running apps.

Let's first start with the code to populate the combo, I redo this on a timer, as applications you open after the first launch would not display otherwise.

Task.Factory.StartNew(() =>
            {

                System.Threading.Thread.Sleep(1000);

                this.Dispatcher.BeginInvoke((Action)delegate()
                {

                    cbSharedResources.Items.Clear();

                    try
                    {
                        _sharingModality = _conversation.Modalities[ModalityTypes.ApplicationSharing] as ApplicationSharingModality;

                        foreach (SharingResource s in _sharingModality.ShareableResources)
                        {
                            try
                            {
                                cbSharedResources.Items.Add(s.Name);
                            }
                            catch { }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                }, DispatcherPriority.Normal);

            });

The 2nd part of this is to act when we change the selected item in that combo box of applications and other screens.

 try
            {
                _sharingModality = _conversation.Modalities[ModalityTypes.ApplicationSharing] as ApplicationSharingModality;

                foreach (SharingResource s in _sharingModality.ShareableResources)
                {
                    try
                    {
                        if (s.Name == cbSharedResources.SelectedItem.ToString())
                        {
                            ShareSelectedResource(new SharingResource_Wrapper(s));
                        }
                        else
                        {
                            Console.WriteLine(s.Name);
                        }
                    }
                    catch { }
                }
            }
            catch { }

As you can see there are some functions needed to make this happen.


        private void ShareSelectedResource(SharingResource_Wrapper selectedResource)
        {

            SharingResourceType selectedResourceType = SharingResourceType.Invalid;
            SharingResource sharingResource = null;

            foreach (SharingResource s in _sharingModality.ShareableResources)
            {
                if (s.Id == selectedResource.ResourceId)
                {
                    //Get the type of resource selected by the user
                    selectedResourceType = s.Type;
                    sharingResource = s;
                    break;
                }
            }

            CanShareDetail sharingDetail;
            if (!_sharingModality.CanShare(selectedResourceType, out sharingDetail))
            {
                switch (sharingDetail)
                {
                    case CanShareDetail.DisabledByOrganizerPolicy:
                        MessageBox.Show("The conversation organizer has disallowed sharing");
                        break;
                    case CanShareDetail.DisabledByPolicy:
                        MessageBox.Show("Sharing resources is not allowed ");
                        break;
                    case CanShareDetail.DisabledByRole:
                        MessageBox.Show("Conference attendees cannot share resources");
                        break;
                }
                return;
            }
          
            if (selectedResourceType == SharingResourceType.Desktop)
            {
                _sharingModality.BeginShareDesktop((ar) =>
                {
                    _sharingModality.EndShareDesktop(ar);
                }
                    , null);

                
            }
            else if (selectedResourceType == SharingResourceType.Monitor)
            {
                _sharingModality.BeginShareResources(sharingResource, (ar) =>
                {
                    _sharingModality.EndShareResources(ar);
                }, null);

               
            }
            else
            {
                _sharingModality.BeginShareResources(sharingResource, (ar) =>
                {
                    try
                    {
                        _sharingModality.EndShareResources(ar);

                    }
                    catch (OperationException oe) { throw oe; }
                    catch (LyncClientException lce) { throw lce; }
                }
                , null);

               
            }
        }

        private void ShareMonitorCallback(System.IAsyncResult ar)
        {
            try
            {
                ApplicationSharingModality sharingModality = (ApplicationSharingModality)ar.AsyncState;
            }
            catch (InvalidCastException) { }
            catch (LyncClientException) { }

        }

        private void ShareResourcesCallback(System.IAsyncResult ar)
        {
            try
            {
                ((ApplicationSharingModality)ar.AsyncState).EndShareResources(ar);
            }
            catch (OperationException) { }
            catch (LyncClientException) { }
            catch (InvalidCastException) { };

        }

        private void ShareDesktopCallback(System.IAsyncResult ar)
        {
            try
            {
                ApplicationSharingModality sharingModality = (ApplicationSharingModality)ar.AsyncState;
                sharingModality.EndShareDesktop(ar);
            }
            catch (LyncClientException) { }
            catch (InvalidCastException) { };
        }

The last 2 parts are the sharing of the primary desktop, and the stopping of all app or monitor sharing.

try
            {
                _sharingModality = _conversation.Modalities[ModalityTypes.ApplicationSharing] as ApplicationSharingModality;

                foreach (SharingResource s in _sharingModality.ShareableResources)
                {
                    try
                    {
                        if (s.Name == "Primary Monitor")
                        {
                            ShareSelectedResource(new SharingResource_Wrapper(s));
                        }
                        else
                        {
                            Console.WriteLine(s.Name);
                        }
                    }
                    catch { }
                }
            }
            catch { }


 try
            {
                if (_sharingModality != null && _sharingModality.CanInvoke(ModalityAction.Disconnect))
                {
                    _sharingModality.BeginDisconnect(ModalityDisconnectReason.None, (ar) =>
                    {
                        _sharingModality.EndDisconnect(ar);
                       
                    }, null);

                }
            }
            catch { }

This concludes our series on building the Ultimate Lync Presentation Tool Helper.  Check back for a download link for the running exe you can use in your environment.

Doug Routledge, C# Lync, SQL, Exchange, UC Developer