/// <summary> /// Data is assumed to be there, so use TryDelete /// </summary> /// <param name="service"></param> protected virtual void CleanupDataPostInitialization(IOrganizationService service) { var totalWatch = new Stopwatch(); totalWatch.Start(); var requests = new OrganizationRequestCollection(); foreach (var id in EntityIds.Where(e => e.Key != "businessunit").SelectMany(entityType => entityType.Value)) { requests.Add(new DeleteRequest { Target = Entities[id] }); } if (requests.Any()) { var response = (ExecuteMultipleResponse)service.Execute( new ExecuteMultipleRequest() { Settings = new ExecuteMultipleSettings() { ContinueOnError = true, ReturnResponses = false }, Requests = requests, }); ThrowExceptionForFaults(response, requests); totalWatch.Stop(); Debug.WriteLine("Total Time to delete {0} entities of types {1} (ms): {2}", requests.Count, String.Join(", ", requests.Select(s => ((DeleteRequest)s).Target.LogicalName).Distinct()), totalWatch.ElapsedMilliseconds); } List <Guid> businessIds; if (!EntityIds.TryGetValue("businessunit", out businessIds)) { return; } foreach (var id in businessIds) { service.DeleteBusinessUnit(id); } }
/// <summary> /// Data is assumed to be there, so use TryDelete /// </summary> /// <param name="service"></param> protected virtual void CleanupDataPostInitialization(IOrganizationService service) { var totalWatch = new Stopwatch(); totalWatch.Start(); var requests = new OrganizationRequestCollection(); foreach (var id in EntityIds.Where(e => e.Key != "businessunit").SelectMany(entityType => entityType.Value)) { requests.Add(new DeleteRequest { Target = Entities[id] }); } if (requests.Any()) { var response = (ExecuteMultipleResponse)service.Execute( new ExecuteMultipleRequest() { Settings = new ExecuteMultipleSettings() { ContinueOnError = true, ReturnResponses = false }, Requests = requests, }); ThrowExceptionForFaults(response, requests); totalWatch.Stop(); Debug.WriteLine("Total Time to delete {0} entities of types {1} (ms): {2}", requests.Count, String.Join(", ", requests.Select(s => ((DeleteRequest)s).Target.LogicalName).Distinct()), totalWatch.ElapsedMilliseconds); } List<Guid> businessIds; if (!EntityIds.TryGetValue("businessunit", out businessIds)) { return; } foreach (var id in businessIds) { service.DeleteBusinessUnit(id); } }
private void CopyData(IOrganizationService service, AttributeMetadata from, AttributeMetadata to, Action actions) { if (!MigrateData) { return; } var total = GetRecordCount(service, from); var count = 0; Trace("Copying data from {0} to {1}", from.LogicalName, to.LogicalName); var requests = new OrganizationRequestCollection(); // Grab from and to, and only update if not equal. This is to speed things up if it has failed part way through foreach (var entity in service.GetAllEntities<Entity>(new QueryExpression(from.EntityLogicalName) { ColumnSet = new ColumnSet(from.LogicalName, to.LogicalName) })) { if (count++ % 100 == 0 || count == total) { if (requests.Any()) { PerformUpdates(service, requests); } Trace("Copying {0} / {1}", count, total); requests.Clear(); } var value = entity.GetAttributeValue<Object>(from.LogicalName); if (actions.HasFlag(Action.ChangeType) && from.GetType() != to.GetType()) { value = CopyValue(from, to, value); } var toValue = entity.GetAttributeValue<Object>(to.LogicalName); if (value != null) { if (value.Equals(toValue)) continue; entity.Attributes[to.LogicalName] = value; requests.Add(new UpdateRequest { Target = entity }); } else if (toValue != null) { entity.Attributes[to.LogicalName] = null; requests.Add(new UpdateRequest { Target = entity }); } } if (requests.Any()) { PerformUpdates(service, requests); } Trace("Data Migration Complete", count, total); }
private void CopyData(IOrganizationService service, AttributeMetadata from, AttributeMetadata to, Action actions) { if (!MigrateData) { return; } var total = GetRecordCount(service, from); var count = 0; Trace("Copying data from {0} to {1}", from.LogicalName, to.LogicalName); var requests = new OrganizationRequestCollection(); // Grab from and to, and only update if not equal. This is to speed things up if it has failed part way through foreach (var entity in service.GetAllEntities <Entity>(new QueryExpression(from.EntityLogicalName) { ColumnSet = new ColumnSet(from.LogicalName, to.LogicalName) })) { if (count++ % 100 == 0 || count == total) { if (requests.Any()) { PerformUpdates(service, requests); } Trace("Copying {0} / {1}", count, total); requests.Clear(); } var value = entity.GetAttributeValue <Object>(from.LogicalName); if (actions.HasFlag(Action.ChangeType) && from.GetType() != to.GetType()) { value = CopyValue(from, to, value); } var toValue = entity.GetAttributeValue <Object>(to.LogicalName); if (value != null) { if (value.Equals(toValue)) { continue; } entity.Attributes[to.LogicalName] = value; requests.Add(new UpdateRequest { Target = entity }); } else if (toValue != null) { entity.Attributes[to.LogicalName] = null; requests.Add(new UpdateRequest { Target = entity }); } } if (requests.Any()) { PerformUpdates(service, requests); } Trace("Data Migration Complete", count, total); }