Friday, July 27, 2018

Retrieve All Organizations in CRM

using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.ServiceModel.Description;

namespace GetAllOrganizations
{
    static class Program
    {
        private static string DiscoverServiceURL = ConfigurationManager.AppSettings["DiscoverServiceURL"];
        private static string UserName = ConfigurationManager.AppSettings["UserName"];
        private static string Password = ConfigurationManager.AppSettings["Password"];
        private static ClientCredentials credentials = new ClientCredentials();

        public static OrganizationServiceProxy GetOrganizationService(Organization selectedOrganization)
        {
            credentials.UserName.UserName = UserName;
            credentials.UserName.Password = Password;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(new Uri(selectedOrganization.OrgServiceURL), null, credentials, null);
            return serviceProxy;
        }

        public static List<Organization> GetOrganizations()
        {
            credentials.UserName.UserName = UserName;
            credentials.UserName.Password = Password;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            using (var discoveryProxy = new DiscoveryServiceProxy(new Uri(DiscoverServiceURL), null, credentials, null))
            {
                discoveryProxy.Authenticate();

                // Get all Organizations using Discovery Service
                RetrieveOrganizationsRequest retrieveOrganizationsRequest = new RetrieveOrganizationsRequest()
                {
                    AccessType = EndpointAccessType.Default,
                    Release = OrganizationRelease.Current
                };

                RetrieveOrganizationsResponse retrieveOrganizationsResponse =
                (RetrieveOrganizationsResponse)discoveryProxy.Execute(retrieveOrganizationsRequest);

                if (retrieveOrganizationsResponse.Details.Count > 0)
                {
                    var orgs = new List<Organization>();
                    foreach (OrganizationDetail orgInfo in retrieveOrganizationsResponse.Details)
                    {
                        string orgServUrl = string.Empty;
                        orgInfo.Endpoints.TryGetValue(EndpointType.OrganizationService, out orgServUrl);
                        orgs.Add(new Organization()
                        {
                            OrgFriendlyName = orgInfo.FriendlyName,
                            OrgUniqueName = orgInfo.UniqueName,
                            OrgServiceURL = orgServUrl
                        });
                    }
                    return orgs;
                }
                else
                    return null;
            }
        }
    }

    public class Organization
    {
        public string OrgFriendlyName { get; set; }
        public string OrgUniqueName { get; set; }
        public string OrgServiceURL { get; set; }
    }
}

No comments:

Post a Comment