コード例 #1
0
        private void btn365Connect_Click(object sender, EventArgs e)
        {
            _discoveryServiceAddress = discoverySvcUrl.Text;
            _organizationUniqueName  = txtOrg.Text;

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            IServiceManagement <IDiscoveryService> serviceManagement =
                ServiceConfigurationFactory.CreateManagement <IDiscoveryService>(
                    new Uri(_discoveryServiceAddress));
            AuthenticationProviderType endpointType = serviceManagement.AuthenticationType;

            // Set the credentials.
            AuthenticationCredentials authCredentials = GetCredentials(serviceManagement, endpointType);


            String organizationUri = String.Empty;

            // Get the discovery service proxy.
            using (DiscoveryServiceProxy discoveryProxy =
                       GetProxy <IDiscoveryService, DiscoveryServiceProxy>(serviceManagement, authCredentials))
            {
                // Obtain organization information from the Discovery service.
                if (discoveryProxy != null)
                {
                    // Obtain information about the organizations that the system user belongs to.
                    OrganizationDetailCollection orgs = DiscoverOrganizations(discoveryProxy);
                    // Obtains the Web address (Uri) of the target organization.
                    organizationUri = FindOrganization(_organizationUniqueName,
                                                       orgs.ToArray()).Endpoints[EndpointType.OrganizationService];
                }
            }


            if (!String.IsNullOrWhiteSpace(organizationUri))
            {
                IServiceManagement <IOrganizationService> orgServiceManagement =
                    ServiceConfigurationFactory.CreateManagement <IOrganizationService>(
                        new Uri(organizationUri));

                // Set the credentials.
                AuthenticationCredentials credentials = GetCredentials(orgServiceManagement, endpointType);

                // Get the organization service proxy.
                using (OrganizationServiceProxy organizationProxy =
                           GetProxy <IOrganizationService, OrganizationServiceProxy>(orgServiceManagement, credentials))
                {
                    // This statement is required to enable early-bound type support.
                    organizationProxy.EnableProxyTypes();

                    // Now make an SDK call with the organization service proxy.
                    // Display information about the logged on user.
                    Guid userid = ((WhoAmIResponse)organizationProxy.Execute(
                                       new WhoAmIRequest())).UserId;
                    IOrganizationService service = null;
                    service = (IOrganizationService)organizationProxy;
                    DownloadAttachment obj1 = new DownloadAttachment(service);
                    obj1.Show();
                    this.Hide();
                    //SystemUser systemUser = organizationProxy.Retrieve("systemuser", userid, new ColumnSet(new string[] { "firstname", "lastname" })).ToEntity<SystemUser>();
                    //Console.WriteLine("Logged on user is {0} {1}.", systemUser.FirstName, systemUser.LastName);
                }
            }
        }
コード例 #2
0
        private void Connect_Click(object sender, EventArgs e)
        {
            MessageText.Text = "";
            Connect.Enabled  = false;

            if (String.IsNullOrEmpty(orgurl.Text))
            {
                MessageText.Text = "Org url is empty."; Connect.Enabled = true; return;
            }
            if (String.IsNullOrEmpty(domainname.Text))
            {
                MessageText.Text = "Domain Name is empty."; Connect.Enabled = true; return;
            }
            if (String.IsNullOrEmpty(username.Text))
            {
                MessageText.Text = "User Name is empty."; Connect.Enabled = true; return;
            }
            if (String.IsNullOrEmpty(password.Text))
            {
                MessageText.Text = "Password is empty."; Connect.Enabled = true; return;
            }


            else
            {
                MessageText.Text = "";
            }


            #region ConnecttoCRM
            IOrganizationService service = null;
            try
            {
                ClientCredentials cre = new ClientCredentials();
                cre.Windows.ClientCredential.Domain   = domainname.Text;
                cre.Windows.ClientCredential.UserName = username.Text;
                cre.Windows.ClientCredential.Password = password.Text;

                Uri serviceUri = new Uri(orgurl.Text);
                OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, cre, null);
                proxy.EnableProxyTypes();
                service = (IOrganizationService)proxy;
            }
            catch (Exception ex)
            {
                MessageText.AppendText("Error in connecting to crm instance.");
                MessageText.AppendText(Environment.NewLine);
                MessageText.AppendText(ex.Message);
                MessageText.AppendText(Environment.NewLine);
                Connect.Enabled = true;
                return;
            }
            #endregion

            if (service == null)
            {
                MessageText.Text = "Organisation service object is null. Contact administrator.";
                Connect.Enabled  = true;
                return;
            }

            // DownloadAttachment obj1 = new DownloadAttachment();
            //this.Hide();
            //obj1.Show();


            DownloadAttachment obj1 = new DownloadAttachment(service);
            obj1.Show();
            this.Hide();
        }