locked
How to Connect to CRM 4.0 using 2011 SDK RRS feed

  • Question

  • Hi,

    I am able to use the latest 2011 CRM SDK to connect to CRM Online as well as CRM On-Premise as long as they are 2011 (CRM 5.0).

    I have CRM 4.0 On-Premise server installed, and I am not able to connect to it. I am using the Discovery service to find all the Organizations.

    I am keep getting this error

    Metadata contains a reference that cannot be resolved: 'http://server/MSCRMServices/2007/AD/CrmDiscoveryService.asmx?wsdl

     public void Connect(ServerConstruct construct)
            {
    
    
                serverConfig = new Configuration();
    
                // Get the server address. If no value is entered, default to Microsoft Dynamics
                // CRM Online in the North American data center.
                serverConfig.ServerAddress = construct.ServerAddress;
                
    
                // One of the Microsoft Dynamics CRM Online data centers.
                if (serverConfig.ServerAddress.EndsWith(".dynamics.com"))
                {
                    serverConfig.DiscoveryUri =
                        new Uri(String.Format("https://dev.{0}/XRMServices/2011/Discovery.svc", serverConfig.ServerAddress));
    
                    // Set or get the device credentials. Required for Windows Live ID authentication. 
                    serverConfig.DeviceCredentials = DeviceIdManager.LoadOrRegisterDevice();
                }
                // Does the server use Secure Socket Layer (https)?
                else
                {
                    
                        if (construct.Protocol == Actions.HttpProtocol.Https)
                            serverConfig.DiscoveryUri =
                                new Uri(String.Format("https://{0}/XRMServices/2011/Discovery.svc", serverConfig.ServerAddress));
                        else
                            serverConfig.DiscoveryUri =
                                new Uri(String.Format("http://{0}/XRMServices/2011/Discovery.svc", serverConfig.ServerAddress));
                   
                    //For Active Directory Authentication (on-premise):
    
                    //http://<servername[:port]>/mscrmservices/2007/ad/crmdiscoveryservice.asmx?WSDL
    
                    //For Active Directory Authentication (IFD):
    
                    //http://<servername[:port]/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx?WSDL
    
                    //Crm Online
                    //"https://dev.crm.dynamics.com/MSCRMServices/2007/Passport/CrmDiscoveryService.asmx";
                }
                // Set the endpoint type.
                try
                    {
                serverConfig.EndpointType = GetServerType(serverConfig.DiscoveryUri);
        
                 }
                    catch
                    {
                        if (construct.Protocol == Actions.HttpProtocol.Https)
                            serverConfig.DiscoveryUri =
                                new Uri(String.Format("https://{0}/MSCRMServices/2007/AD/CrmDiscoveryService.asmx?wsdl", serverConfig.ServerAddress));
                        else
                            serverConfig.DiscoveryUri =
                                new Uri(String.Format("http://{0}/MSCRMServices/2007/AD/CrmDiscoveryService.asmx?wsdl", serverConfig.ServerAddress));
    
                        serverConfig.EndpointType = AuthenticationProviderType.ActiveDirectory;
                    }
                // Get the user's logon credentials.
                serverConfig.Credentials = GetUserLogonCredentials(construct);
    
                // Get the target organization.
                serverConfig.OrganizationUri = GetOrganizationAddress(serverConfig.DiscoveryUri, construct.OrganizationName);
    
    
            }
    
           
    
    
            private AuthenticationProviderType GetServerType(Uri uri)
            {
                return ServiceConfigurationFactory.CreateConfiguration<IDiscoveryService>(uri).AuthenticationType;
            }
    
            private Uri GetOrganizationAddress(Uri discoveryServiceUri, string organizationName)
            {
                using (DiscoveryServiceProxy serviceProxy = new DiscoveryServiceProxy(discoveryServiceUri, null, serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // Obtain organization information from the Discovery service. 
                    if (serviceProxy != null)
                    {
                        // Obtain information about the organizations that the system user belongs to.
                        OrganizationDetailCollection orgs = DiscoverOrganizations(serviceProxy);
    
                        OrganizationDetail orgDetail = FindOrganization(organizationName, orgs);
                        if (orgDetail != null)
                        {
                            return new System.Uri(orgDetail.Endpoints[EndpointType.OrganizationService]);
                        }
                        else
                        {
                            //Console.WriteLine("\nYou do not belong to any organizations on the specified server.");
                            throw new Exception("The specified organization does not exist.");
                        }
                    }
                    else
                        throw new Exception("An invalid server name was specified.");
                }
            }
    
    
            /// <summary>
            /// Discovers the organizations that the calling user belongs to.
            /// </summary>
            /// <param name="service">A Discovery service proxy instance.</param>
            /// <returns>Array containing detailed information on each organization that 
            /// the user belongs to.</returns>
            private OrganizationDetailCollection DiscoverOrganizations(IDiscoveryService service)
            {
                RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
                RetrieveOrganizationsResponse orgResponse =
                    (RetrieveOrganizationsResponse)service.Execute(orgRequest);
    
                return orgResponse.Details;
            }
    



    Ronak Patel
    Thursday, October 13, 2011 4:23 PM

All replies