locked
getting list of organisations through c# RRS feed

  • Question

  • OrganizationDetailCollection orgs = DiscoverOrganizations(serviceProxy);

    // to get list of organizations run this loop
    for (int n = 0; n < orgs.Count; n++)
     {
          Console.Write("\n({0}) {1} ({2})\t", n + 1, orgs[n].FriendlyName, orgs[n].UrlName);
     }

    public OrganizationDetailCollection DiscoverOrganizations(IDiscoveryService service)
     {
                RetrieveOrganizationsResponse orgResponse = new RetrieveOrganizationsResponse();
                    RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();

                    orgResponse = (RetrieveOrganizationsResponse)service.Execute(orgRequest);

                    return orgResponse.Details;
                
    }

    Hello guys, i got the following code from a kind person to get the list of organisation, but the problem is that, the code is not complete, im unable to figure out how to create the "serviceProxy" in the first line of code. can any one tell me how to create the IDiscoveryService object for the above program

    Monday, August 20, 2012 7:12 AM

Answers

  • Use this

     public OrganizationDetailCollection GetOrganizations(String serverURL, String username, String password, String domainname)
            {
                IServiceConfiguration<IDiscoveryService> dinfo =
                ServiceConfigurationFactory.CreateConfiguration<IDiscoveryService>(GetDiscoveryServiceUri(serverURL));
     
                var creds = new ClientCredentials();
                creds.Windows.ClientCredential = new System.Net.NetworkCredential(username, password, domainname);
     
                DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(dinfo, creds);
                dsp.Authenticate();
                RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
                RetrieveOrganizationsResponse orgResponse = dsp.Execute(orgRequest) as RetrieveOrganizationsResponse;
     
                return orgResponse.Details;
            }
    private Uri GetDiscoveryServiceUri(string serverName)
            {
                string discoSuffix = @"/XRMServices/2011/Discovery.svc";
     
                return new Uri(string.Format("{0}{1}", serverName, discoSuffix));
            }


    Thanks Anil Chelasani http://exploringxrm.wordpress.com

    Monday, August 20, 2012 7:23 AM
  • Refer to one of the SDK samples which shows you exactly how to do it: Sample: Authenticate Users with Microsoft Dynamics CRM Web Services

    SDK\SampleCode\CS\QuickStart

    IServiceManagement<IDiscoveryService> serviceManagement =
                            ServiceConfigurationFactory.CreateManagement<IDiscoveryService>(
                            new Uri(_discoveryServiceAddress));
                AuthenticationProviderType endpointType = serviceManagement.AuthenticationType;
    
                // Set the credentials.
                AuthenticationCredentials authCredentials = GetCredentials(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];
    
                    }
                }
    

    Monday, August 20, 2012 7:30 AM

All replies

  • Use this

     public OrganizationDetailCollection GetOrganizations(String serverURL, String username, String password, String domainname)
            {
                IServiceConfiguration<IDiscoveryService> dinfo =
                ServiceConfigurationFactory.CreateConfiguration<IDiscoveryService>(GetDiscoveryServiceUri(serverURL));
     
                var creds = new ClientCredentials();
                creds.Windows.ClientCredential = new System.Net.NetworkCredential(username, password, domainname);
     
                DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(dinfo, creds);
                dsp.Authenticate();
                RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
                RetrieveOrganizationsResponse orgResponse = dsp.Execute(orgRequest) as RetrieveOrganizationsResponse;
     
                return orgResponse.Details;
            }
    private Uri GetDiscoveryServiceUri(string serverName)
            {
                string discoSuffix = @"/XRMServices/2011/Discovery.svc";
     
                return new Uri(string.Format("{0}{1}", serverName, discoSuffix));
            }


    Thanks Anil Chelasani http://exploringxrm.wordpress.com

    Monday, August 20, 2012 7:23 AM
  • Refer to one of the SDK samples which shows you exactly how to do it: Sample: Authenticate Users with Microsoft Dynamics CRM Web Services

    SDK\SampleCode\CS\QuickStart

    IServiceManagement<IDiscoveryService> serviceManagement =
                            ServiceConfigurationFactory.CreateManagement<IDiscoveryService>(
                            new Uri(_discoveryServiceAddress));
                AuthenticationProviderType endpointType = serviceManagement.AuthenticationType;
    
                // Set the credentials.
                AuthenticationCredentials authCredentials = GetCredentials(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];
    
                    }
                }
    

    Monday, August 20, 2012 7:30 AM
  • Hi,

    I would agree with Sweta. Kindly refer the sdk projects provided by Microsoft. This will help you take correct approach to do any CRUD operations related to plugin code(c# or vb) as well as javascripts in Microsoft Supported way. You may download the latest sdk from this link

    CRM 2011

    http://www.microsoft.com/en-in/download/details.aspx?id=24004

    CRM 4.0

    http://www.microsoft.com/en-us/download/details.aspx?id=38

    Let me know in case you face any difficulty further.

    Thanks & Regards,


    Anubhav Bajpai

    • Proposed as answer by Anubhav Bajpai Monday, August 20, 2012 8:02 AM
    Monday, August 20, 2012 8:02 AM