public void Test_DatabaseExists() { const string databaseName = "master"; using (var db = new CrmDatabase("serveraddress", "integration", "pwd")) { var exists = db.DatabaseExists(databaseName).Result; Assert.IsTrue(exists); } }
/// <summary> /// Reprovisions the Xrm Organisation by dropping the database and re-creating the organisation /// </summary> /// <param name="eventStream">The Build Event stream</param> /// <param name="crmDb">An object representing the CRM Database</param> /// <param name="credential">The Network Credential to use on the CRM deployment client.</param> /// <returns>The async <see cref="Task"/> unit of work.</returns> private async Task Reprovision(IObserver <BuildEvent> eventStream, CrmDatabase crmDb, NetworkCredential credential) { using (var client = ProxyClientHelper.CreateClient(new Uri(DeploymentServiceUrl))) { if (client.ClientCredentials != null) { client.ClientCredentials.Windows.ClientCredential = credential; } var crmDatabaseName = OrganizationUniqueName + "_MSCRM"; try { if (await client.OrganizationExists(OrganizationUniqueName)) { await client.DisableOrganizationAsync(OrganizationUniqueName, eventStream); await client.DeleteOrganizationAsync(OrganizationUniqueName, eventStream); } else { eventStream.OnNext(new BuildEvent(BuildEventType.Warning, BuildEventImportance.Medium, $"The Organization {OrganizationUniqueName} doesn't exist in the CRM server. Is this intentional?")); } if (await crmDb.DatabaseExists(crmDatabaseName)) { await crmDb.DropDatabaseAsync(crmDatabaseName, eventStream); } else { eventStream.OnNext(new BuildEvent(BuildEventType.Warning, BuildEventImportance.Medium, $"The Organization Database {crmDatabaseName} doesn't exist in the SQL Server. Is this intentional?")); } await client.CreateOrganizationAsync( new Organization { BaseCurrencyCode = OrganizationBaseCurrencyCode, BaseCurrencyName = OrganizationBaseCurrencyName, BaseCurrencyPrecision = OrganizationBaseCurrencyPrecision, BaseCurrencySymbol = OrganizationBaseCurrencySymbol, BaseLanguageCode = OrganizationBaseLanguageCode, UniqueName = OrganizationUniqueName, FriendlyName = OrganizationFriendlyName, SqlCollation = SqlCollation, SqlServerName = SqlServerName.Replace(".cloudapp.net", ""), SrsUrl = SsrsUrl, SqmIsEnabled = SqmIsEnabled }, eventStream); } catch (AggregateException ex) { var fault = ex.InnerExceptions.First() as FaultException <DeploymentServiceFault>; if (fault == null) { throw ex.InnerExceptions.First(); } dynamic clunkyCast = fault.Detail.ErrorDetails; foreach (KeyValuePair <string, object> p in clunkyCast) { eventStream.OnNext(new BuildEvent(BuildEventType.Error, BuildEventImportance.High, $"{p.Key} : {p.Value}")); } } } }