private void ReadForm()
        {
            List <UIElement>         elements = new List <UIElement>();
            List <LookupRequestItem> data     = new List <LookupRequestItem>();

            foreach (object child in MainGrid.Children)
            {
                if (child is UIElement)
                {
                    elements.AddRange(DiscoverNamedChildren(child as UIElement));
                }
            }
            foreach (UIElement elem in elements)
            {
                if (elem is FrameworkElement)
                {
                    string name  = ((FrameworkElement)elem).Name;
                    string value = String.Empty;
                    if (elem is TextBox)
                    {
                        value = ((TextBox)elem).Text;
                    }
                    else if (elem is ToggleButton && ((ToggleButton)elem).IsChecked != null)
                    {
                        value = ((ToggleButton)elem).IsChecked.ToString();
                    }
                    else if (elem is Selector && ((Selector)elem).SelectedValue != null)
                    {
                        value = ((Selector)elem).SelectedValue.ToString();
                    }
                    else if (elem is PasswordBox && ((PasswordBox)elem).Password != null)
                    {
                        value = ((PasswordBox)elem).Password;
                    }
                    //else if (elem is RichTextBox)
                    //    value = String.Empty;
                    else if (elem is Control)
                    {
                        value = elem.ToString();
                    }

                    data.Add(new LookupRequestItem(name, value));
                }
            }
            try
            {
                DynamicsCustomerRecord customerRecord = (DynamicsCustomerRecord)localSession.Customer.DesktopCustomer;
                customerRecord.MergeReplacementParameter(this.ApplicationName, data);
            }
            catch
            {
            }
        }
        private void ReadForm(RequestActionEventArgs args)
        {
            List <KeyValuePair <string, string> > lines = Utility.SplitLines(args.Data, CurrentContext, localSession);
            string clear     = Utility.GetAndRemoveParameter(lines, "clear");
            bool   clearBool = false;

            if (!String.IsNullOrEmpty(clear) && bool.TryParse(clear, out clearBool) && clearBool == true)
            {
                DynamicsCustomerRecord customerRecord = (DynamicsCustomerRecord)localSession.Customer.DesktopCustomer;
                customerRecord.ClearReplaceableParameter(this.ApplicationName);
            }
            ReadForm();
        }
        public override void NotifyContextChange(Context context)
        {
            base.NotifyContextChange(context);

            // This is a global hosted control so we need to use ActiveSession here
            if (((AgentDesktopSession)localSessionManager.ActiveSession).Customer == null)
            {
                return;
            }
            Dispatcher.BeginInvoke(new System.Action(() =>
            {
                try
                {
                    DynamicsCustomerRecord customerRec = (DynamicsCustomerRecord)((AgentDesktopSession)localSessionManager.ActiveSession).Customer.DesktopCustomer;
                    string InitialLogicalName          = "";
                    if (customerRec != null && customerRec.InitialEntity != null)
                    {
                        InitialLogicalName = customerRec.InitialEntity.LogicalName;
                    }
                    string InitialLogicalActivity = "";
                    if (customerRec != null && customerRec.StartActivity != null)
                    {
                        InitialLogicalActivity = customerRec.StartActivity.LogicalName;
                    }
                    foreach (SessionInfo si in sessionInfoEntries.Where(a => (!String.IsNullOrEmpty(a.SelectedEntityName) && (a.SelectedEntityName.Equals(InitialLogicalName, StringComparison.InvariantCultureIgnoreCase) ||
                                                                                                                              a.SelectedEntityName.Equals(InitialLogicalActivity, StringComparison.InvariantCultureIgnoreCase))) || String.IsNullOrEmpty(a.SelectedEntityName)))
                    {
                        try
                        {
                            string sessionNameCandidate = Utility.GetContextReplacedString(si.Display, context, localSessionManager == null ? null : localSessionManager.ActiveSession);
                            if (Utility.IsAllReplacementValuesReplaced(sessionNameCandidate))
                            {
                                SetSessionName(sessionNameCandidate);
                                return;
                            }
                        }
                        catch
                        {
                        }
                    }
                }
                catch
                {
                }
            }));
        }
        private void ClearData(RequestActionEventArgs args)
        {
            DynamicsCustomerRecord customerRecord = (DynamicsCustomerRecord)localSession.Customer.DesktopCustomer;

            customerRecord.ClearReplaceableParameter(this.ApplicationName);
        }