private void btnOk_Click(object sender, EventArgs e)
        {
            CrmConnections cc = new CrmConnections(txtConnectionName.Text);
            cc.SerializeToFile(txtFilePath.Text);

            ConnectionsList.Instance.Files.Add(new ConnectionFile { Name = txtConnectionName.Text, Path = txtFilePath.Text, LastUsed = DateTime.Now });
            ConnectionsList.Instance.Save();

            CreatedFilePath = txtFilePath.Text;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            CrmConnections cc = new CrmConnections(txtConnectionName.Text);

            cc.SerializeToFile(txtFilePath.Text);

            ConnectionsList.Instance.Files.Add(new ConnectionFile {
                Name = txtConnectionName.Text, Path = txtFilePath.Text, LastUsed = DateTime.Now
            });
            ConnectionsList.Instance.Save();

            CreatedFilePath = txtFilePath.Text;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
Example #3
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            CrmConnections cc   = new CrmConnections(txtConnectionName.Text);
            var            file = new ConnectionFile(cc)
            {
                Path     = txtFilePath.Text,
                Name     = txtConnectionName.Text,
                LastUsed = DateTime.Now
            };

            if (ConnectionsList.Instance.Files.Any(f => f.Name == cc.Name))
            {
                int    cloneId = 1;
                string newName = file.Name;

                while (ConnectionsList.Instance.Files.FirstOrDefault(f => f.Name == newName) != null)
                {
                    var rule = new System.Text.RegularExpressions.Regex(".* \\(" + cloneId + "\\)$");
                    if (rule.IsMatch(newName))
                    {
                        cloneId++;
                        newName = $"{cc.Name.Replace($" ({cloneId - 1})", "")} ({cloneId})";
                    }
                    else
                    {
                        newName = $"{newName} ({cloneId})";
                    }
                }

                file.Name = newName;

                MessageBox.Show(this, $@"A connection file with this name already exists!\n\nIt has been renamed to '{newName}'", "Warning",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            cc.SerializeToFile(txtFilePath.Text);
            ConnectionsList.Instance.Save();

            CreatedFilePath = txtFilePath.Text;

            DialogResult = DialogResult.OK;
            Close();
        }
Example #4
0
        static void Main(string[] args)
        {
            var username = "******";
            var password = ""; // not needed for online connections

            var clientId            = "51f81489-12ee-4a9e-aaae-a2591f45987d";
            var replyUri            = new Uri("app://58145B91-0C36-4500-8554-080854F2AC97");
            var discoveryServiceUrl = new Uri("https://globaldisco.crm.dynamics.com/api/discovery/v2.0/Instances");

            var connectionFileName  = username;
            var connectionDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"MscrmTools\XrmToolBox\Connections");
            var connectionFilePath  = Path.Combine(connectionDirectory, connectionFileName + ".xml");

            var credentials = new ClientCredentials();

            credentials.UserName.UserName = username;
            credentials.UserName.Password = password;

            /* There seems to be some sort of bug in LinqPad where it gets MethodMissing for
             * DiscoverGlobalOrganizations even though it can find it intellisense, and the
             * same code works in a console app. Bizarrely reflection does work. When moved
             * into a real VS solution, the commented out connection code is the way to go (or
             * when the LP bug is fixed)
             *
             *
             * var organizations = CrmServiceClient.DiscoverGlobalOrganizations(discoveryServiceUri: discoveryServiceUrl,
             *                                                               clientCredentials: credentials,
             *                                                               user: null,
             *                                                               clientId: appId,
             *                                                               redirectUri: appReplyUri,
             *                                                               // if you change the prompt behaviour to auto, you'll want to use a different cache for each credential
             *                                                               // otherwise it will keep reusing the last successful connection
             *                                                               tokenCachePath: "",
             *                                                               isOnPrem: false,
             *                                                               authority: string.Empty,
             *                                                               promptBehavior: Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior.SelectAccount,
             *                                                               useDefaultCreds: false);
             */

            var organizations = (OrganizationDetailCollection)typeof(CrmServiceClient)
                                .GetMethod(nameof(CrmServiceClient.DiscoverGlobalOrganizations), BindingFlags.Static | BindingFlags.Public)
                                .Invoke(null, new object[] {
                discoveryServiceUrl,
                credentials,
                null,
                clientId,
                replyUri,
                "",
                false,
                string.Empty,
                Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior.SelectAccount,
                false,
            });

            var connections = organizations.Select(o =>
            {
                var url = new Uri(o.Endpoints[EndpointType.WebApplication]);

                var detail = new ConnectionDetail(createNewId: true)
                {
                    ConnectionId               = o.OrganizationId,
                    UserName                   = username,
                    UserDomain                 = credentials.Windows.ClientCredential.Domain ?? "",
                    AuthType                   = GuessAuthType(credentials, clientId, o),
                    NewAuthType                = GuessNewAuthType(credentials, clientId, o),
                    ConnectionName             = $"{o.FriendlyName}",
                    ServerName                 = url.Host,
                    Organization               = o.UniqueName,
                    OrganizationUrlName        = o.UrlName,
                    OrganizationFriendlyName   = o.FriendlyName,
                    OrganizationVersion        = o.OrganizationVersion,
                    OrganizationServiceUrl     = o.Endpoints[EndpointType.OrganizationService],
                    OrganizationDataServiceUrl = o.Endpoints[EndpointType.OrganizationDataService],
                    WebApplicationUrl          = o.Endpoints[EndpointType.WebApplication],
                    ServerPort                 = url.Port,
                    Timeout       = TimeSpan.FromMinutes(20),
                    EnvironmentId = o.EnvironmentId,
                    OriginalUrl   = url.AbsoluteUri,
                    ReplyUrl      = replyUri.AbsoluteUri,
                };

                if (detail.NewAuthType == AuthenticationType.OAuth && string.IsNullOrEmpty(password))
                {
                    // if we have no password we want it to use the oauth token cache instead
                    // this is signaled by saying we are multifactor
                    detail.UseMfa = true;
                }

                if (Guid.TryParse(clientId, out var azureAppId))
                {
                    detail.AzureAdAppId = azureAppId;
                }

                if (!string.IsNullOrEmpty(password))
                {
                    if (detail.NewAuthType == AuthenticationType.ClientSecret)
                    {
                        detail.SetClientSecret(credentials.UserName.Password);
                    }
                    else
                    {
                        detail.SetPassword(credentials.UserName.Password);
                    }
                }

                switch (DiscoverPurpose(o))
                {
                case OrganizationPurpose.Production:
                    detail.EnvironmentColor     = Color.Red;
                    detail.EnvironmentTextColor = Color.White;
                    detail.EnvironmentText      = $"👎 - {o.FriendlyName}";
                    break;

                case OrganizationPurpose.Test:
                    detail.EnvironmentColor     = Color.Yellow;
                    detail.EnvironmentTextColor = Color.Black;
                    detail.EnvironmentText      = $"✋ - {o.FriendlyName}";
                    break;

                case OrganizationPurpose.Development:
                    detail.EnvironmentColor     = Color.Green;
                    detail.EnvironmentTextColor = Color.White;
                    detail.EnvironmentText      = $"👍 - {o.FriendlyName}";
                    break;
                }

                return(detail);
            });
            var connectionFile = new CrmConnections(connectionFileName);

            connectionFile.Connections.AddRange(connections);
            connectionFile.SerializeToFile(connectionFilePath);

            Console.WriteLine($"Connection file generated successfully: \nPATH: {connectionFilePath}");
            Console.WriteLine("Press any key to exit: ");
            Console.ReadKey();
        }
Example #5
0
        /// <summary>
        /// Adds the ToolStripMenuItems representing connections to the
        /// ToolStripDropDownButton
        /// </summary>
        /// <param name="btn">ToolStripDropDownButton where to add connections</param>
        private void AddActionsList(ToolStripDropDownButton btn)
        {
            var list       = new List <ToolStripItem>();
            int filesCount = ConnectionsList.Instance.Files.Count;

            if (filesCount == 0)
            {
                var defaultFilePath = Path.Combine(new FileInfo(ConnectionsList.ConnectionsListFilePath).DirectoryName, "ConnectionsList.Default.xml");

                CrmConnections cc = new CrmConnections("Default");
                cc.SerializeToFile(defaultFilePath);

                ConnectionsList.Instance.Files.Add(new ConnectionFile(cc)
                {
                    Path = defaultFilePath, LastUsed = DateTime.Now
                });
                ConnectionsList.Instance.Save();
            }

            foreach (var file in ConnectionsList.Instance.Files)
            {
                var connections = CrmConnections.LoadFromFile(file.Path);
                connections.Connections.Sort();

                var fileItem = new ToolStripMenuItem(file.Name);
                fileItem.Tag = file;
                if (!mergeConnectionFiles && filesCount > 1)
                {
                    list.Add(fileItem);
                }

                foreach (var cDetail in connections.Connections)
                {
                    ToolStripMenuItem item = new ToolStripMenuItem();
                    item.Text = cDetail.ConnectionName;
                    item.Tag  = cDetail;

                    if (cDetail.UseOnline)
                    {
                        item.Image =
                            RessourceManager.GetImage(
                                "McTools.Xrm.Connection.WinForms.Resources.CRMOnlineLive_16.png");
                    }
                    else if (cDetail.UseOsdp)
                    {
                        item.Image =
                            RessourceManager.GetImage(
                                "McTools.Xrm.Connection.WinForms.Resources.CRMOnlineLive_16.png");
                    }
                    else if (cDetail.UseIfd)
                    {
                        item.Image =
                            RessourceManager.GetImage(
                                "McTools.Xrm.Connection.WinForms.Resources.server_key.png");
                    }
                    else
                    {
                        item.Image =
                            RessourceManager.GetImage(
                                "McTools.Xrm.Connection.WinForms.Resources.server.png");
                    }

                    BuildActionItems(item);
                    if (!mergeConnectionFiles && filesCount > 1)
                    {
                        fileItem.DropDownItems.Add(item);
                    }
                    else
                    {
                        list.Add(item);
                    }
                }

                if (!mergeConnectionFiles && filesCount > 1)
                {
                    if (fileItem.DropDownItems.Count > 0)
                    {
                        fileItem.DropDownItems.Add(new ToolStripSeparator());
                    }
                }

                var newConnectionItem = new ToolStripMenuItem();
                newConnectionItem.Text   = "Create new connection";
                newConnectionItem.Image  = (Image)resources.GetObject("server_add");
                newConnectionItem.Click += newConnectionItem_Click;

                if (!mergeConnectionFiles && filesCount > 1)
                {
                    fileItem.DropDownItems.Add(newConnectionItem);
                }
            }

            if (mergeConnectionFiles || filesCount == 1)
            {
                if (list.Count > 0)
                {
                    list.Add(new ToolStripSeparator());
                }

                var newConnectionItem = new ToolStripMenuItem();
                newConnectionItem.Text   = "Create new connection";
                newConnectionItem.Image  = (Image)resources.GetObject("server_add");
                newConnectionItem.Click += newConnectionItem_Click;

                list.Add(newConnectionItem);
            }

            if (InvokeRequired)
            {
                Invoke(new Action(() =>
                {
                    btn.DropDownItems.Clear();
                    btn.DropDownItems.AddRange(list.ToArray());
                }));
            }
            else
            {
                btn.DropDownItems.Clear();
                btn.DropDownItems.AddRange(list.ToArray());
            }
        }