示例#1
0
        /// <summary>
        /// Demonstrates how to programmatically install and uninstall the Microsoft
        /// Dynamics CRM sample data records.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">Not applicable for this sample.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                // Connect to the Organization service.
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();
                    //<snippetImportOrRemoveSampleData1>

                    // Prompt user to install/uninstall sample data.
                    Console.WriteLine("Would you like to:");
                    Console.WriteLine("1) Install sample data for Microsoft Dynamics CRM?");
                    Console.WriteLine("2) Uninstall sample data for Microsoft Dynamics CRM?");
                    Console.Write("Press [1] to Install, [2] to Uninstall: ");
                    String answer = Console.ReadLine();

                    // Update the sample data based on the user's response.
                    switch (answer)
                    {
                    case "1":
                        Console.WriteLine("Installing sample data...");
                        InstallSampleDataRequest request =
                            new InstallSampleDataRequest();
                        InstallSampleDataResponse response =
                            (InstallSampleDataResponse)_serviceProxy.Execute(request);
                        Console.WriteLine("Sample data successfully installed.");
                        break;

                    case "2":
                        Console.WriteLine("Uninstalling sample data...");
                        UninstallSampleDataRequest request2 =
                            new UninstallSampleDataRequest();
                        UninstallSampleDataResponse response2 =
                            (UninstallSampleDataResponse)_serviceProxy.Execute(request2);
                        Console.WriteLine("Sample data successfully uninstalled.");
                        break;

                    default:
                        Console.WriteLine("Neither option was selected. No changes have been made to your records.");
                        break;
                    }
                }
                //</snippetImportOrRemoveSampleData1>
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }
        /// <summary>
        /// Demonstrates how to programmatically install and uninstall the Microsoft
        /// Dynamics CRM sample data records.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">Not applicable for this sample.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                // Connect to the Organization service. 
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();
                    //<snippetImportOrRemoveSampleData1>

                    // Prompt user to install/uninstall sample data.
                    Console.WriteLine("Would you like to:");
                    Console.WriteLine("1) Install sample data for Microsoft Dynamics CRM?");
                    Console.WriteLine("2) Uninstall sample data for Microsoft Dynamics CRM?");
                    Console.Write("Press [1] to Install, [2] to Uninstall: ");
                    String answer = Console.ReadLine();

                    // Update the sample data based on the user's response.
                    switch (answer)
                    {
                        case "1":
                            Console.WriteLine("Installing sample data...");
                            InstallSampleDataRequest request =
                                new InstallSampleDataRequest();
                            InstallSampleDataResponse response =
                                (InstallSampleDataResponse)_serviceProxy.Execute(request);
                            Console.WriteLine("Sample data successfully installed.");
                            break;
                        case "2":
                            Console.WriteLine("Uninstalling sample data...");
                            UninstallSampleDataRequest request2 =
                                new UninstallSampleDataRequest();
                            UninstallSampleDataResponse response2 =
                                (UninstallSampleDataResponse)_serviceProxy.Execute(request2);
                            Console.WriteLine("Sample data successfully uninstalled.");
                            break;
                        default:
                            Console.WriteLine("Neither option was selected. No changes have been made to your records.");
                            break;
                    }

                }
                //</snippetImportOrRemoveSampleData1>
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }
        [STAThread] // Added to support UX
        static void Main(string[] args)
        {
            CrmServiceClient service = null;

            try
            {
                service = SampleHelpers.Connect("Connect");
                if (service.IsReady)
                {
                    #region Sample Code
                    #region Set up
                    SetUpSample(service);
                    #endregion Set up
                    #region Demonstrate
                    // Prompt user to install/uninstall sample data.
                    Console.WriteLine("Would you like to:");
                    Console.WriteLine("1) Install sample data for CDS?");

                    Console.WriteLine("2) Uninstall sample data for CDS?");
                    Console.Write("Press [1] to Install, [2] to Uninstall: ");
                    String answer = Console.ReadLine();

                    // Update the sample data based on the user's response.
                    switch (answer)
                    {
                    case "1":
                        Console.WriteLine("Installing sample data...");
                        InstallSampleDataRequest request =
                            new InstallSampleDataRequest();
                        InstallSampleDataResponse response =
                            (InstallSampleDataResponse)service.Execute(request);
                        Console.WriteLine("Sample data successfully installed.");
                        break;

                    case "2":
                        Console.WriteLine("Uninstalling sample data...");
                        UninstallSampleDataRequest request2 =
                            new UninstallSampleDataRequest();
                        UninstallSampleDataResponse response2 =
                            (UninstallSampleDataResponse)service.Execute(request2);
                        Console.WriteLine("Sample data successfully uninstalled.");
                        break;

                    default:
                        Console.WriteLine("Neither option was selected. No changes have been made to your records.");
                        break;
                    }
                }
                #endregion Demonstrate
                #endregion Sample Code
                else
                {
                    const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Dynamics CRM";
                    if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR))
                    {
                        Console.WriteLine("Check the connection string values in cds/App.config.");
                        throw new Exception(service.LastCrmError);
                    }
                    else
                    {
                        throw service.LastCrmException;
                    }
                }
            }
            catch (Exception ex)
            {
                SampleHelpers.HandleException(ex);
            }

            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }

                Console.WriteLine("Press <Enter> to exit.");
                Console.ReadLine();
            }
        }