How to: Accept or decline requests to control a shared resource

[This is preliminary documentation and is subject to change.]

Describes how to accept or decline a request to control a locally shared resource. For an overview of resource sharing in Microsoft Lync 2013 Preview, see Resource Sharing in Lync.

The topic in this section builds on the sample application introduced in How Do I in Resource Sharing and continued in How to: Start resource sharing conversations. Figure 1 highlights the part of the sample Windows form that we discuss in this topic. The tasks in the following list are discussed in this topic and shown in the code samples.

Figure 1. Accept and Decline resource control request buttons.

Accept and Decline resource controlling request

Before your application runs the logic in this How to, a resource sharing conversation must be active with one or more participants in addition to the local user. The local user participation state must be ParticipationState.Sharing and a local sharable resource must be shared in the conversation. For information about reaching this starting application state, see How to: Get shareable resources and share them in a conversation.

Before your application can accept or decline a resource control request you must register an event callback method on the ApplicationSharingModality.ControlRequestReceived and ApplicationSharingModality.ControllerChanged events of the conversation Microsoft.Lync.Model.Conversation.Sharing.ApplicationSharingModality. When the first event is raised, a conversation participant is requesting control of the locally shared resource. When the second event is raised, control of the resource has been given to the requesting participant.

Code example

The following example registers an event callback method on the ApplicationSharingModality.ControlRequestReceived event and the ApplicationSharingModality.ControllerChanged event.

Important note Important

This example is a partial listing of the ConversationAdded event. For a complete example of a ConversationAdded event handler, see How to: Start resource sharing conversations.

        /// <summary>
        /// Handles the event raised when a new conversation is added. This sample only hosts one conversation
        /// at a time. Once this event is handled, the sample un-registers for this event. The event is registered again when
        /// this conversation is removed from the ContactManager.Conversations collection upon termination of this conversation.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ConversationManager_ConversationAdded(object sender, Microsoft.Lync.Model.Conversation.ConversationManagerEventArgs e)
        {


            //Register for the application sharing modality event on the conversation itself
            _sharingModality = (ApplicationSharingModality)_conversation.Modalities[ModalityTypes.ApplicationSharing];

            //Register to catch requests from other participants for control of the locally owned sharing resource.
            _sharingModality.ControlRequestReceived += _sharingModality_ControlRequestReceived;
            _sharingModality.ControllerChanged += _sharingModality_ControllerChanged;
        }


Note Note

You should also register an event callback method on the Modality.ActionAvailabilityChanged event. When this event is raised on the conversation or participant application sharing modality for the availability of the ModalityAction.AcceptSharingControlRequest and ModalityAction.DeclineSharingControlRequest actions, then you should enable two button controls on the UI which let the user accept or decline a control request. For information about handling the Modality.ActionAvailabilityChanged event, see How to: Get shareable resources and share them in a conversation.

The following example handles the ControlRequestReceived event by updating the UI of the form with the name of the participant who is requesting control. A Microsoft.Lync.Model.Contact object representing the requesting participant is cached in the class field _resourceControllingContact. This Contact is used in the Accept button click event handler to get the Microsoft.Lync.Model.Conversation.Sharing.ApplicationSharingModality object owned by the requesting participant.

        /// <summary>
        /// Handles the event raised when a conversation participant requests control of a locally owned resource.
        /// These requests always go to the resource owner, and not the current controller of the resource.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _sharingModality_ControlRequestReceived(object sender, ControlRequestReceivedEventArgs e)
        {
            //Get the name of the participant that is requesting control of the locally owned resource.
            string displayRequesterName = e.Requester.Contact.GetContactInformation(ContactInformationType.DisplayName).ToString();


            //Update the text of the Accept button to include the name of the requester.
            this.Invoke(new ChangeButtonTextDelegate(ChangeButtonText), new object[] { Accept_Button, "Accept " + displayRequesterName + "  Request" });

            //Store the Contact object for the requesting participant.
            _ResourceControllingContact = e.Requester.Contact;
        }


After the code in the previous example has enabled buttons on the UI that lets the user accept or decline a control request, the user can choose the button and accept the request. Accept the control request by calling the ApplicationSharingModalityBeginAcceptControlRequest(AsyncCallback, object) method on the requesting participant’s application sharing modality.

Code example

The following example accepts a resource control request after the user chooses to accept the request.

       /// <summary>
       ///Accept another participants request to control locally owned and shared resource.
       ///The Accept button is enabled when the _sharingModality_ActionAvailabilityChanged event handler is called
       ///with the event argument that specifies the ModalityAction.AcceptSharingControlRequest action is available.
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void Accept_Button_Click(object sender, EventArgs e)
        {

            //_selectedContact is set to the Contact object of the participant who requested control of the resource. 
            //see the _sharingModality_ControlRequestReceived method in the application sharing modality event handlers region.
            ApplicationSharingModality sharingModality = (ApplicationSharingModality)_participantSharingModalities[_ResourceControllingContact.Uri];
 
            //If the requesting participant application sharing modality is available and the AcceptSharingControlRequest action can be invoked
            if (sharingModality != null && sharingModality.CanInvoke(ModalityAction.AcceptSharingControlRequest))
            {
                //Accept sharing control request.
                sharingModality.BeginAcceptControlRequest(SharingControlCallback, "accept");
            }
        }

The following example declines a resource control request after the user chooses to decline the request. Decline the control request by calling the ApplicationSharingModalityBeginDeclineControlRequest(AsyncCallback, object) method on the requesting participant’s application sharing modality.

        /// <summary>
        ///Decline another participants request to control locally owned and shared resource.
        ///The Decline button is enabled when the _sharingModality_ActionAvailabilityChanged event handler is
        ///called with the event argument that specifies the ModalityAction.DeclineSharingControlRequest action is available.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Decline_Button_Click(object sender, EventArgs e)
        {
            //_selectedContact is set to the Contact object of the participant who requested control of the resource. 
            //see the _sharingModality_ControlRequestReceived method in the application sharing modality event handlers region.
            ApplicationSharingModality sharingModality = (ApplicationSharingModality)_participantSharingModalities[_ResourceControllingContact.Uri];
            if (sharingModality != null && sharingModality.CanInvoke(ModalityAction.DeclineSharingControlRequest))
            {
                sharingModality.BeginDeclineControlRequest(SharingControlCallback, "decline");
            }

        }


The following example is invoked by the platform thread when the previous asynchronous control request actions are completed.

        /// <summary>
        /// Callback method for all asynchronous modality control methods.
        /// </summary>
        /// <param name="ar"></param>
        private void SharingControlCallback(System.IAsyncResult ar)
        {
            //Get the application sharing modality for the conversation participant to invoke controlling action on.
            ApplicationSharingModality sharingModality = (ApplicationSharingModality)_participantSharingModalities[_ResourceControllingContact.Uri];

            if (ar.IsCompleted == false)
            {
                MessageBox.Show(ar.AsyncState.ToString() +  " did not complete");
            }
            //Complete the asynchronous controlling action according to the asynchronous state string that indicates
            //which controlling action to complete.
            switch (ar.AsyncState.ToString().ToUpper())
            {
                case "ACCEPT":
                    sharingModality.EndAcceptControlRequest(ar);
                    break;
                case "DECLINE":
                    sharingModality.EndDeclineControlRequest(ar);
                    break;
            }
        }


After the user has accepted or declined another conversation participant’s request to control the locally shared resource, your application should confirm the user’s response to the request by updating the application UI with the name of the new resource controller. This is performed by handling the ApplicationSharingModality.ControllerChanged event. The event data parameter of the event callback method gives the name of the controller. Use this name property to update the value of a text control on your UI.

Code example

The following example updates the form UI with the name of the participant who is controlling the locally shared resource.

        /// <summary>
        /// Handles the event raised when the participant that is controlling the shared conversation application resource
        /// changes. This event is raised locally even when the shared resource is not locally owned.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _sharingModality_ControllerChanged(object sender, ControllerChangedEventArgs e)
        {
            this.Invoke(new ChangeLabelTextDelegate(ChangeLabelText), new object[] { ResourceControllerName_Label, e.ControllerName });

            //Store the Contact object for the conversation participant that now controls the shared conversation resource.
            _ResourceControllingContact = ((ApplicationSharingModality)sender).Controller.Contact;

        }


When your application has responded to a request for control of a shared resource, if accepted, the resource is now controlled by the requester and the ModalityAction.RevokeSharingControl action is available. If declined, the resource is not controlled and the ModalityAction.GrantSharingControl action is available.

Community Additions

Show:
© 2014 Microsoft