/// <summary>
        /// Creates or updates a Crm connection
        /// </summary>
        /// <param name="isCreation">Indicates if it is a connection creation</param>
        /// <param name="connectionToUpdate">Details of the connection to update</param>
        /// <param name="connectionFile">List of connections where the connection is edited from</param>
        /// <returns>Created or updated connection</returns>
        public ConnectionDetail EditConnection(bool isCreation, ConnectionDetail connectionToUpdate, ConnectionFile connectionFile = null)
        {
            var cForm = new ConnectionWizard2(connectionToUpdate)
            {
                StartPosition = FormStartPosition.CenterParent
            };

            if (cForm.ShowDialog(innerAppForm) == DialogResult.OK)
            {
                if (isCreation)
                {
                    if (connectionFile == null)
                    {
                        if (ConnectionManager.Instance.ConnectionsList.Connections.FirstOrDefault(
                                d => d.ConnectionId == cForm.CrmConnectionDetail.ConnectionId) == null)
                        {
                            ConnectionManager.Instance.ConnectionsList.Connections.Add(cForm.CrmConnectionDetail);
                        }
                    }
                    else
                    {
                        var connections = CrmConnections.LoadFromFile(connectionFile.Path);
                        if (connections.Connections.FirstOrDefault(
                                d => d.ConnectionId == cForm.CrmConnectionDetail.ConnectionId) == null)
                        {
                            connections.Connections.Add(cForm.CrmConnectionDetail);
                        }

                        connections.SerializeToFile(connectionFile.Path);
                    }
                }
                else
                {
                    if (connectionFile == null)
                    {
                        ConnectionManager.Instance.ConnectionsList.Connections
                        .Where(x => x.ConnectionId == cForm.CrmConnectionDetail.ConnectionId)
                        .ToList()
                        .ForEach(x => x.UpdateAfterEdit(cForm.CrmConnectionDetail));
                    }
                    else
                    {
                        var connections = CrmConnections.LoadFromFile(connectionFile.Path);
                        foreach (ConnectionDetail detail in connections.Connections)
                        {
                            if (detail.ConnectionId == cForm.CrmConnectionDetail.ConnectionId)
                            {
                                detail.UpdateAfterEdit(cForm.CrmConnectionDetail);
                            }
                        }

                        connections.SerializeToFile(connectionFile.Path);
                    }
                }

                return(cForm.CrmConnectionDetail);
            }

            return(null);
        }
Exemple #2
0
        private void tsbNewConnection_Click(object sender, EventArgs e)
        {
            var cForm = new ConnectionWizard2
            {
                StartPosition = FormStartPosition.CenterParent
            };

            if (cForm.ShowDialog(this) == DialogResult.OK)
            {
                var newConnection = cForm.CrmConnectionDetail;
                hadCreatedNewConnection = true;

                var item = new ListViewItem(newConnection.ConnectionName);
                item.SubItems.Add(newConnection.ServerName);
                item.SubItems.Add(newConnection.Organization);
                item.SubItems.Add(newConnection.UserName);
                item.SubItems.Add(newConnection.OrganizationVersion);
                item.SubItems.Add(newConnection.SolutionName);
                item.Tag        = newConnection;
                item.Group      = GetGroup(newConnection);
                item.ImageIndex = GetImageIndex(newConnection);

                lvConnections.Items.Add(item);
                lvConnections.SelectedItems.Clear();
                item.Selected = true;

                lvConnections.Sort();

                if (isConnectionSelection)
                {
                    BValidateClick(sender, e);
                }

                // If the connection id is not found and the user want to save
                // the connection (ie. he provided a name for the connection)
                if (ConnectionManager.Instance.ConnectionsList.Connections.FirstOrDefault(d => d.ConnectionId == newConnection.ConnectionId) == null &&
                    !string.IsNullOrEmpty(newConnection.ConnectionName))
                {
                    ConnectionManager.Instance.ConnectionsList.Connections.Add(newConnection);
                    RefreshComboBoxSelectedConnection();
                }
            }
        }
        private void tsbUpdateConnection_Click(object sender, EventArgs e)
        {
            if (lvConnections.SelectedItems.Count == 1)
            {
                ListViewItem item = lvConnections.SelectedItems[0];

                var cForm = new ConnectionWizard2((ConnectionDetail)item.Tag)
                {
                    StartPosition = FormStartPosition.CenterParent
                };

                if (cForm.ShowDialog(this) == DialogResult.OK)
                {
                    item.Tag = cForm.CrmConnectionDetail;
                    item.SubItems[0].Text = cForm.CrmConnectionDetail.ConnectionName;
                    item.SubItems[1].Text = cForm.CrmConnectionDetail.ServerName;
                    item.SubItems[2].Text = cForm.CrmConnectionDetail.Organization;
                    if (item.SubItems.Count == 4)
                    {
                        item.SubItems[3].Text = cForm.CrmConnectionDetail.OrganizationVersion;
                    }
                    else
                    {
                        item.SubItems.Add(cForm.CrmConnectionDetail.OrganizationVersion);
                    }
                    item.Group = GetGroup(cForm.CrmConnectionDetail);

                    lvConnections.Refresh();

                    var updatedConnectionDetail = ConnectionManager.Instance.ConnectionsList.Connections.FirstOrDefault(
                        c => c.ConnectionId == cForm.CrmConnectionDetail.ConnectionId);

                    ConnectionManager.Instance.ConnectionsList.Connections.Remove(updatedConnectionDetail);
                    ConnectionManager.Instance.ConnectionsList.Connections.Add(cForm.CrmConnectionDetail);

                    ConnectionManager.Instance.SaveConnectionsFile();
                }
            }
        }
        private void tsbUpdateConnection_Click(object sender, EventArgs e)
        {
            if (lvConnections.SelectedItems.Count == 1)
            {
                ListViewItem item = lvConnections.SelectedItems[0];

                var cd = (ConnectionDetail)item.Tag;

                if (cd.IsFromSdkLoginCtrl)
                {
                    var ctrl = new CRMLoginForm1(cd.ConnectionId.Value, true);
                    ctrl.ShowDialog();

                    if (ctrl.CrmConnectionMgr.CrmSvc?.IsReady ?? false)
                    {
                        cd.Organization               = ctrl.CrmConnectionMgr.ConnectedOrgUniqueName;
                        cd.OrganizationFriendlyName   = ctrl.CrmConnectionMgr.ConnectedOrgFriendlyName;
                        cd.OrganizationDataServiceUrl =
                            ctrl.CrmConnectionMgr.ConnectedOrgPublishedEndpoints[EndpointType.OrganizationDataService];
                        cd.OrganizationServiceUrl =
                            ctrl.CrmConnectionMgr.ConnectedOrgPublishedEndpoints[EndpointType.OrganizationService];
                        cd.WebApplicationUrl =
                            ctrl.CrmConnectionMgr.ConnectedOrgPublishedEndpoints[EndpointType.WebApplication];
                        cd.ServerName          = new Uri(cd.WebApplicationUrl).Host;
                        cd.OrganizationVersion = ctrl.CrmConnectionMgr.CrmSvc.ConnectedOrgVersion.ToString();

                        item.Tag = cd;
                        lvConnections.Items.Remove(item);
                        lvConnections.Items.Add(item);
                        lvConnections.Refresh();//RedrawItems(0, lvConnections.Items.Count - 1, false);

                        var updatedConnectionDetail = ConnectionManager.Instance.ConnectionsList.Connections.FirstOrDefault(
                            c => c.ConnectionId == cd.ConnectionId);

                        ConnectionManager.Instance.ConnectionsList.Connections.Remove(updatedConnectionDetail);
                        ConnectionManager.Instance.ConnectionsList.Connections.Add(cd);

                        ConnectionManager.Instance.SaveConnectionsFile();
                    }

                    return;
                }

                var cForm = new ConnectionWizard2(cd)
                {
                    StartPosition = FormStartPosition.CenterParent
                };

                if (cForm.ShowDialog(this) == DialogResult.OK)
                {
                    item.Tag = cForm.CrmConnectionDetail;
                    item.SubItems[0].Text = cForm.CrmConnectionDetail.ConnectionName;
                    item.SubItems[1].Text = cForm.CrmConnectionDetail.ServerName;
                    item.SubItems[2].Text = cForm.CrmConnectionDetail.Organization;
                    if (item.SubItems.Count == 4)
                    {
                        item.SubItems[3].Text = cForm.CrmConnectionDetail.OrganizationVersion;
                    }
                    else
                    {
                        item.SubItems.Add(cForm.CrmConnectionDetail.OrganizationVersion);
                    }
                    item.Group = GetGroup(cForm.CrmConnectionDetail);

                    lvConnections.Refresh();

                    var updatedConnectionDetail = ConnectionManager.Instance.ConnectionsList.Connections.FirstOrDefault(
                        c => c.ConnectionId == cForm.CrmConnectionDetail.ConnectionId);

                    ConnectionManager.Instance.ConnectionsList.Connections.Remove(updatedConnectionDetail);
                    ConnectionManager.Instance.ConnectionsList.Connections.Add(cForm.CrmConnectionDetail);

                    ConnectionManager.Instance.SaveConnectionsFile();
                }
            }
        }