locked
How to use ServerConnection class? RRS feed

  • Spørgsmål

  • I am not able to understand its code and i want to user it n even not able to find how to use it. Plz help


    spkh
    29. juli 2011 11:19

Svar

  • Hi spkh,

    Here is some sample code for you.

     

    Connection String

    public ServerConnection.Configuration CrmOnlineConnection()
            {
                ServerConnection.Configuration serverCrmConnect = null;
                try
                {
                    string email = ConfigurationManager.AppSettings["CrmEmailAddress"];
                    string pass = ConfigurationManager.AppSettings["CrmPassword"];
                    string org = ConfigurationManager.AppSettings["Organization"];
    
                    // Obtain the target organization's Web address and client logon 
                    // credentials from the user.
                    var serverConnect = new ServerConnection();
                    var credentials = new ClientCredentials();
    
                    credentials.UserName.UserName = email;
                    credentials.UserName.Password = pass;
    
                    serverCrmConnect = serverConnect.GetServerConfiguration(credentials, org);
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    Console.WriteLine("The application terminated with an error.");
                    Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
                    Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
                    Console.WriteLine("Message: {0}", ex.Detail.Message);
                    Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText);
                    Console.WriteLine("Inner Fault: {0}",
                                      null == ex.Detail.InnerFault ? "Has Inner Fault" : "No Inner Fault");
                }
    
                return serverCrmConnect;
            }
    

     

    Get Product List

    public List<Product> GetCrmProducts(ServerConnection.Configuration serverConfig)
            {
                IOrganizationService _service;
                OrganizationServiceProxy _serviceProxy;
                List<Product> product;
    
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
                                                                    serverConfig.HomeRealmUri,
                                                                    serverConfig.Credentials,
                                                                    serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();
                    _service = _serviceProxy;
    
                    // Create the OrganizationServiceContext object that will generate
                    // the IQueryable collections for LINQ calls.
                    var orgContext = new OrganizationServiceContext(_service);
    
                    product = orgContext.CreateQuery<Product>().ToList();
                }
    
                return product;
            }
    


    From your program.cs call the function.

    GetCrmProducts(Connection.CrmOnlineConnection());
    

     


    Steven Jennings | Microsoft Dynamics CRM Implementer
    (CRM Blog: http://blog.aresgrp.com/crm/)
    Certified in CRM 4.0 & CRM 2011.
    Affiliated http://www.aresgrp.com
    8. september 2011 19:36

Alle besvarelser

  • Hi spkh,

    Here is some sample code for you.

     

    Connection String

    public ServerConnection.Configuration CrmOnlineConnection()
            {
                ServerConnection.Configuration serverCrmConnect = null;
                try
                {
                    string email = ConfigurationManager.AppSettings["CrmEmailAddress"];
                    string pass = ConfigurationManager.AppSettings["CrmPassword"];
                    string org = ConfigurationManager.AppSettings["Organization"];
    
                    // Obtain the target organization's Web address and client logon 
                    // credentials from the user.
                    var serverConnect = new ServerConnection();
                    var credentials = new ClientCredentials();
    
                    credentials.UserName.UserName = email;
                    credentials.UserName.Password = pass;
    
                    serverCrmConnect = serverConnect.GetServerConfiguration(credentials, org);
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    Console.WriteLine("The application terminated with an error.");
                    Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
                    Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
                    Console.WriteLine("Message: {0}", ex.Detail.Message);
                    Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText);
                    Console.WriteLine("Inner Fault: {0}",
                                      null == ex.Detail.InnerFault ? "Has Inner Fault" : "No Inner Fault");
                }
    
                return serverCrmConnect;
            }
    

     

    Get Product List

    public List<Product> GetCrmProducts(ServerConnection.Configuration serverConfig)
            {
                IOrganizationService _service;
                OrganizationServiceProxy _serviceProxy;
                List<Product> product;
    
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
                                                                    serverConfig.HomeRealmUri,
                                                                    serverConfig.Credentials,
                                                                    serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();
                    _service = _serviceProxy;
    
                    // Create the OrganizationServiceContext object that will generate
                    // the IQueryable collections for LINQ calls.
                    var orgContext = new OrganizationServiceContext(_service);
    
                    product = orgContext.CreateQuery<Product>().ToList();
                }
    
                return product;
            }
    


    From your program.cs call the function.

    GetCrmProducts(Connection.CrmOnlineConnection());
    

     


    Steven Jennings | Microsoft Dynamics CRM Implementer
    (CRM Blog: http://blog.aresgrp.com/crm/)
    Certified in CRM 4.0 & CRM 2011.
    Affiliated http://www.aresgrp.com
    8. september 2011 19:36
  • Any questions please let me know
    Steven Jennings | Microsoft Dynamics CRM Implementer
    (CRM Blog: http://blog.aresgrp.com/crm/)
    Certified in CRM 4.0 & CRM 2011.
    Affiliated http://www.aresgrp.com
    8. september 2011 19:37