Saturday, 14 July 2012

Create organization programmatically in Microsoft Dynamics CRM 2011

I've been working on automating various parts of the deployment of our new project and one of them is the creation of organizations. As I mentioned in a previous post, we are not doing a full install in any one server, which means that an organization is NOT created during the install and this is where the SDK comes in.

The method below, designed to be invoked from a WPF application, will create an Organization in MS Dynamics CRM 2011. Do bear in mind that creating an organization is a long process, it took a good 15 minutes in our development server when I tested it.

 private void CreateOrganization(DeploymentServiceClient service, string DisplayName, string UniqueName, string SqlServerName, string SrsUrl)
 {
     DeferredOperationStatus createOrgStatus;
 
     using (service)
     {
         Organization organization = new Organization
         {
             FriendlyName = DisplayName,
             UniqueName = UniqueName,
             SqlServerName = SqlServerName,
             SrsUrl = SrsUrl
         };
 
         try
         {
             BeginCreateOrganizationRequest request = new BeginCreateOrganizationRequest();
 
             request.Organization = organization;
 
             BeginCreateOrganizationResponse response = (BeginCreateOrganizationResponse)service.Execute(request);
 
             EntityInstanceId operationid = new EntityInstanceId()
             {
                 Id = response.OperationId
             };
 
             createOrgStatus = service.Retrieve(DeploymentEntityType.DeferredOperationStatus, operationid) as DeferredOperationStatus;
 
             //The process of creating new ORG is quite slow, thus the status is not checked regularly
 
             while (createOrgStatus.State != DeferredOperationState.Completed)
             {
                 Thread.Sleep(Constants.WaitInterval);
 
                 createOrgStatus = service.Retrieve(DeploymentEntityType.DeferredOperationStatus, operationid) as DeferredOperationStatus;
             }
 
             MessageBox.Show(string.Format("Organization {0} has been created.", UniqueName));
         }
         catch (Exception ex)
         {
             MessageBox.Show(string.Format("An exception has occurred while creating Organization {0}.{1}{2}.", UniqueName, Environment.NewLine, ex));
         }
 
     }
 }

The method below is used to instantiate the deployment service client with default credentials:

 private DeploymentServiceClient InstantiateDeploymentService(DeploymentServiceClient service, string FQDN, int port = 80, int TimeOut = 600)
 {
     try
     {
         service = ProxyClientHelper.CreateClient(new Uri(string.Format(Constants.DeploymentURL, FQDN, port)));
   
         service.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, TimeOut);
   
         return service;
     }
     catch (Exception ex)
     {
         MessageBox.Show(string.Format("Unable to instatiate Deployment Service. Exception {0}", ex));
   
         return null;
     }
 }

Note that you can use the PowerShell cmdlet Get-CrmOperationStatus to check the status of the import if you are really impatient (Remember to load the CRM Snap-in first: Add-PSSnapin Microsoft.Crm.PowerShell).

No comments:

Post a Comment