private static IOrganizationService Connect()
        {
            var config = _configurationService.Get <XrmClientConfiguration>();


            Uri dInfo = new Uri(config.XrmUri);
            ClientCredentials clientcred = new ClientCredentials();

            clientcred.UserName.UserName = config.XrmClientCredUserName;
            clientcred.UserName.Password = config.XrmClientCredPassword;


            #region on-premise/online

            DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(dInfo, null, clientcred, null);
            dsp.Authenticate();
            RetrieveOrganizationsRequest  rosreq = new Microsoft.Xrm.Sdk.Discovery.RetrieveOrganizationsRequest();
            RetrieveOrganizationsResponse r      = (RetrieveOrganizationsResponse)dsp.Execute(rosreq);

            //get the OrganizationService
            ManagedTokenOrganizationServiceProxy _serviceproxy = new ManagedTokenOrganizationServiceProxy(new Uri(config.XrmOrgServiceProxy), clientcred);
            //In order to use the generated types when dealing with the organization service, you have to add the ProxyTypesBehavior to the endpoint Behaviors collection. This instructs the OrganizationServiceProxy to look in the assembly for classes with certain attributes to use. The generated classes are already attributed with these attributes. Simply, this makes all interactions with the organization service to be done using the generated typed classes for each entity instead of the generic Entity class we used earlier.
            _serviceproxy.ServiceConfiguration.CurrentServiceEndpoint.EndpointBehaviors.Add(new ProxyTypesBehavior());
            //Do not forget to include _serviceproxy.EnableProxyTypes();. Without this line,you will be unable to use early binding.
            _serviceproxy.EnableProxyTypes();
            IOrganizationService service = (IOrganizationService)_serviceproxy;

            #endregion
            return(service);
        }
예제 #2
0
        public static OrganizationDetailCollection GetOrganizations(Settings settings)
        {
            try
            {
                var connection = Microsoft.Xrm.Client.CrmConnection.Parse(settings.GetDiscoveryCrmConnectionString());
                var service    = new Microsoft.Xrm.Client.Services.DiscoveryService(connection);

                var request  = new Microsoft.Xrm.Sdk.Discovery.RetrieveOrganizationsRequest();
                var response = (Microsoft.Xrm.Sdk.Discovery.RetrieveOrganizationsResponse)service.Execute(request);
                return(response.Details);
            }
            catch (System.IO.FileNotFoundException e)
            {
                if (e.Message.Contains("Microsoft.IdentityModel"))
                {
                    throw new Exception("Unable to load Windows Identity Foundation 3.5.  This is a feature that can be enabled on windows 8+ or downloaded for earlier versions ->  https://www.microsoft.com/en-nz/download/details.aspx?id=17331 ", e);
                }
                else
                {
                    throw e;
                }
            }
        }