/// <summary>
        /// Checks is the requested organization exists in the target CRM deployment.
        /// </summary>
        /// <param name="client">The <see cref="DeploymentServiceClient"/> that we are using to call CRM.</param>
        /// <param name="organization">The name of the Organization we want to check for.</param>
        /// <returns>True if the organization exists, false otherwise.</returns>
        public static async Task <bool> OrganizationExists(this DeploymentServiceClient client, string organization)
        {
            EntityInstanceId org = null;
            await Task.Factory.StartNew(() => org = client.RetrieveAll(DeploymentEntityType.Organization)
                                        .FirstOrDefault(item => item.Name == organization));

            return(org != null);
        }
Beispiel #2
0
        /// <summary>
        /// Disables an Organization in CRM.
        /// </summary>
        /// <param name="client">The <see cref="DeploymentServiceClient"/> that we are using to call CRM.</param>
        /// <param name="organization">The name of the Organization we want to disable.</param>
        /// <param name="eventStream">The Rx event stream used to push build events onto.</param>
        public static async Task DisableOrganizationAsync(this DeploymentServiceClient client, string organization, IObserver <BuildEvent> eventStream)
        {
            EntityInstanceId org = null;
            await Task.Factory.StartNew(() => org = client.RetrieveAll(DeploymentEntityType.Organization)
                                        .FirstOrDefault(item => item.Name == organization));

            if (org == null)
            {
                throw new ArgumentException($"{organization} Organisation not found", nameof(organization));
            }

            var orgToRemove = (Organization)client.Retrieve(DeploymentEntityType.Organization, new EntityInstanceId {
                Id = org.Id
            });

            orgToRemove.State = OrganizationState.Disabled;
            await Task.Factory.StartNew(() => client.Update(orgToRemove));
        }
Beispiel #3
0
        /// <summary>
        /// Deletes any entity records that were created for this sample.
        /// <param name="prompt">Indicates whether to prompt the user
        /// to delete the records created in this sample.</param>
        /// </summary>
        public void DeleteRequiredRecords(bool prompt)
        {
            bool toBeDeleted = true;

            if (prompt)
            {
                // Ask the user if the created entities should be deleted.
                Console.Write("\nDo you want these entity records deleted? (y/n) [y]: ");
                String answer = Console.ReadLine();
                if (answer.StartsWith("y") ||
                    answer.StartsWith("Y") ||
                    answer == String.Empty)
                {
                    toBeDeleted = true;
                }
                else
                {
                    toBeDeleted = false;
                }
            }

            if (toBeDeleted)
            {
                // First disable the org
                EntityInstanceId organizationCreated = new EntityInstanceId();
                organizationCreated.Id = _organizationID;
                Microsoft.Xrm.Sdk.Deployment.Organization organization =
                    (Microsoft.Xrm.Sdk.Deployment.Organization)client.Retrieve(
                        DeploymentEntityType.Organization, organizationCreated);

                // Update status to disabled
                organization.State = OrganizationState.Disabled;

                client.Update(organization);
                Console.WriteLine("Organization has been disabled.");

                // Second delete it
                client.Delete(DeploymentEntityType.Organization, organizationCreated);
                Console.WriteLine("Organization has been deleted.");
            }
        }
Beispiel #4
0
        /// <summary>
        /// Deletes an Organization in CRM.
        /// </summary>
        /// <param name="client">The <see cref="DeploymentServiceClient"/> that we are using to call CRM.</param>
        /// <param name="organization">The name of the Organization we want to delete.</param>
        /// <param name="eventStream">The Rx event stream used to push build events onto.</param>
        public static async Task DeleteOrganizationAsync(this DeploymentServiceClient client, string organization, IObserver <BuildEvent> eventStream)
        {
            EntityInstanceId org = null;
            await Task.Factory.StartNew(() => org = client.RetrieveAll(DeploymentEntityType.Organization)
                                        .FirstOrDefault(item => item.Name == organization));

            if (org == null)
            {
                throw new ArgumentException($"{organization} Organisation not found", nameof(organization));
            }

            var orgToDisable = (Organization)client.Retrieve(DeploymentEntityType.Organization, new EntityInstanceId {
                Id = org.Id
            });

            if (orgToDisable.State != OrganizationState.Disabled)
            {
                throw new InvalidOperationException($"The Organization needs to be in the Disabled state to be deleted and currently is in the {orgToDisable.State} state");
            }

            await Task.Factory.StartNew(() => client.Delete(DeploymentEntityType.Organization, new EntityInstanceId {
                Id = orgToDisable.Id
            }));
        }
        /// <summary>
        /// Deletes any entity records that were created for this sample.
        /// <param name="prompt">Indicates whether to prompt the user 
        /// to delete the records created in this sample.</param>
        /// </summary>
        public void DeleteRequiredRecords(bool prompt)
        {
            bool toBeDeleted = true;

            if (prompt)
            {
                // Ask the user if the created entities should be deleted.
                Console.Write("\nDo you want these entity records deleted? (y/n) [y]: ");
                String answer = Console.ReadLine();
                if (answer.StartsWith("y") ||
                    answer.StartsWith("Y") ||
                    answer == String.Empty)
                {
                    toBeDeleted = true;
                }
                else
                {
                    toBeDeleted = false;
                }
            }

            if (toBeDeleted)
            {
                // First disable the org
                EntityInstanceId organizationCreated = new EntityInstanceId();
                organizationCreated.Id = _organizationID;
                Microsoft.Xrm.Sdk.Deployment.Organization organization = 
                    (Microsoft.Xrm.Sdk.Deployment.Organization)client.Retrieve(
                    DeploymentEntityType.Organization, organizationCreated);
                
                // Update status to disabled
                organization.State = OrganizationState.Disabled;

                client.Update(organization);
                Console.WriteLine("Organization has been disabled.");
                
                // Second delete it
                client.Delete(DeploymentEntityType.Organization, organizationCreated);
                Console.WriteLine("Organization has been deleted.");
            }
        }
        private void CreateOrganization()
        {
            if (string.IsNullOrWhiteSpace(this.DeploymentUrl) || !Uri.IsWellFormedUriString(this.DeploymentUrl, UriKind.Absolute))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The Deployment service URL is not valid. {0}", this.DeploymentUrl));
                return;
            }

            if (string.IsNullOrWhiteSpace(this.Name))
            {
                Log.LogError("Missing required parameter: Name");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.DisplayName))
            {
                Log.LogError("Missing required parameter: Display Name");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.SqlServerInstance))
            {
                Log.LogError("Missing required parameter: Sql Server Instance");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.SsrsUrl) || !Uri.IsWellFormedUriString(this.SsrsUrl, UriKind.Absolute))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The Sql Server Reporting Service URL is not valid. {0}", this.SsrsUrl));
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to the deployment service {0}.", this.DeploymentUrl));
            using (var service = ProxyClientHelper.CreateClient(new Uri(this.DeploymentUrl)))
            {
                var newOrganization = new Microsoft.Xrm.Sdk.Deployment.Organization
                {
                    FriendlyName = this.DisplayName,
                    UniqueName = this.Name,
                    SqlServerName = this.SqlServerInstance,
                    SrsUrl = this.SsrsUrl
                };

                try
                {
                    var request = new BeginCreateOrganizationRequest
                    {
                        Organization = newOrganization
                    };

                    var response = service.Execute(request) as BeginCreateOrganizationResponse;
                    if (response == null)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentCulture, "No response was received while creating Organization {0}", this.Name));
                        return;
                    }

                    var operationId = new EntityInstanceId
                    {
                        Id = response.OperationId
                    };

                    int waitCount = 0;
                    var organizationCreationStatus = service.Retrieve(DeploymentEntityType.DeferredOperationStatus, operationId) as DeferredOperationStatus;

                    // Wait for the organization to be created. Checking the stauts repeatedly
                    while (organizationCreationStatus != null && 
                        (organizationCreationStatus.State == DeferredOperationState.Processing || organizationCreationStatus.State == DeferredOperationState.Queued))
                    {
                        Thread.Sleep(WaitIntervalInMilliseconds);
                        Log.LogMessage(MessageImportance.High, "Processing...");
                        organizationCreationStatus = service.Retrieve(DeploymentEntityType.DeferredOperationStatus, operationId) as DeferredOperationStatus;
                        if (++waitCount > this.Timeout)
                        {
                            break;
                        }
                    }

                    if (waitCount >= this.Timeout)
                    {
                        Log.LogMessage(MessageImportance.High, string.Format(CultureInfo.CurrentCulture, "Your request for creation of Organization {0} is still being processed but the task has exceeded its timeout value of {1} minutes.", this.Name, this.Timeout));
                    }
                    else
                    {
                        Log.LogMessage(MessageImportance.High, string.Format(CultureInfo.CurrentCulture, "The Organization {0} was created successfully.", this.Name));
                    }
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(CultureInfo.CurrentCulture, "An error occurred while creating Organization  {0}. [{1}]", this.Name, exception.Message));
                }
            }
        }
 public void Delete(DeploymentEntityType entityType, EntityInstanceId id)
 {
     _service.UsingService(s => s.Delete(entityType, id));
 }
 public DeploymentObject Retrieve(DeploymentEntityType entityType, EntityInstanceId id)
 {
     return(_service.UsingService(s => s.Retrieve(entityType, id)));
 }
        private void CreateOrganization()
        {
            if (string.IsNullOrWhiteSpace(this.DeploymentUrl) || !Uri.IsWellFormedUriString(this.DeploymentUrl, UriKind.Absolute))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The Deployment service URL is not valid. {0}", this.DeploymentUrl));
                return;
            }

            if (string.IsNullOrWhiteSpace(this.Name))
            {
                Log.LogError("Missing required parameter: Name");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.DisplayName))
            {
                Log.LogError("Missing required parameter: Display Name");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.SqlServerInstance))
            {
                Log.LogError("Missing required parameter: Sql Server Instance");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.SsrsUrl) || !Uri.IsWellFormedUriString(this.SsrsUrl, UriKind.Absolute))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The Sql Server Reporting Service URL is not valid. {0}", this.SsrsUrl));
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to the deployment service {0}.", this.DeploymentUrl));
            using (var service = ProxyClientHelper.CreateClient(new Uri(this.DeploymentUrl)))
            {
                var newOrganization = new Microsoft.Xrm.Sdk.Deployment.Organization
                {
                    FriendlyName  = this.DisplayName,
                    UniqueName    = this.Name,
                    SqlServerName = this.SqlServerInstance,
                    SrsUrl        = this.SsrsUrl
                };

                try
                {
                    var request = new BeginCreateOrganizationRequest
                    {
                        Organization = newOrganization
                    };

                    var response = service.Execute(request) as BeginCreateOrganizationResponse;
                    if (response == null)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentCulture, "No response was received while creating Organization {0}", this.Name));
                        return;
                    }

                    var operationId = new EntityInstanceId
                    {
                        Id = response.OperationId
                    };

                    int waitCount = 0;
                    var organizationCreationStatus = service.Retrieve(DeploymentEntityType.DeferredOperationStatus, operationId) as DeferredOperationStatus;

                    // Wait for the organization to be created. Checking the stauts repeatedly
                    while (organizationCreationStatus != null &&
                           (organizationCreationStatus.State == DeferredOperationState.Processing || organizationCreationStatus.State == DeferredOperationState.Queued))
                    {
                        Thread.Sleep(WaitIntervalInMilliseconds);
                        Log.LogMessage(MessageImportance.High, "Processing...");
                        organizationCreationStatus = service.Retrieve(DeploymentEntityType.DeferredOperationStatus, operationId) as DeferredOperationStatus;
                        if (++waitCount > this.Timeout)
                        {
                            break;
                        }
                    }

                    if (waitCount >= this.Timeout)
                    {
                        Log.LogMessage(MessageImportance.High, string.Format(CultureInfo.CurrentCulture, "Your request for creation of Organization {0} is still being processed but the task has exceeded its timeout value of {1} minutes.", this.Name, this.Timeout));
                    }
                    else
                    {
                        Log.LogMessage(MessageImportance.High, string.Format(CultureInfo.CurrentCulture, "The Organization {0} was created successfully.", this.Name));
                    }
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(CultureInfo.CurrentCulture, "An error occurred while creating Organization  {0}. [{1}]", this.Name, exception.Message));
                }
            }
        }