/// <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."); } }
/// <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 })); }