Last active
December 10, 2018 19:27
-
-
Save mdearing06/e7d3058fd14b678c5549135bde5d384b to your computer and use it in GitHub Desktop.
Compare Entities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
string prefix = "new"; | |
var sourceConnectionString = $@"Url=https://SOURCE.crm.dynamics.com;AuthType=Office365;UserName=USERNAME;Password=PASSWORD;RequireNewInstance=true;"; | |
var targetConnectionString = $@"Url=https://TARGET.crm.dynamics.com;AuthType=Office365;UserName=USERNAME;Password=PASSWORD;RequireNewInstance=true;"; | |
var sourceCrmSvcClient = new CrmServiceClient(sourceConnectionString); | |
var targetCrmSvcClient = new CrmServiceClient(targetConnectionString); | |
var targetEntities = GetEntities(prefix, targetCrmSvcClient); | |
foreach (var targetEntity in targetEntities) | |
{ | |
var targetAttributes = GetAttributes(targetCrmSvcClient, targetEntity); | |
IEnumerable<dynamic> sourceAttributes = null; | |
try | |
{ | |
sourceAttributes = GetAttributes(sourceCrmSvcClient, targetEntity); | |
} | |
catch { $"Entity {targetEntity} does not exist in source".Dump(); continue;} | |
foreach (var targetAttribute in targetAttributes) | |
{ | |
var sourceAttribute = sourceAttributes.Where(x => x.Name == targetAttribute.Name).FirstOrDefault(); | |
if (sourceAttribute == null) | |
{ | |
continue; | |
} | |
if (!String.Equals(targetAttribute.SchemaName, sourceAttribute.SchemaName, StringComparison.Ordinal)) | |
{ | |
$"{targetEntity}.{targetAttribute.Name} has different casing {targetAttribute.SchemaName} & {sourceAttribute.SchemaName}".Dump(); | |
continue; | |
} | |
if (!String.Equals(targetAttribute.Type, sourceAttribute.Type, StringComparison.OrdinalIgnoreCase)) | |
{ | |
$"{targetEntity}.{targetAttribute.Name} has different type: {targetAttribute.Type} & {sourceAttribute.Type}".Dump(); | |
continue; | |
} | |
} | |
} | |
} | |
private IList<String> GetEntities(string prefix, IOrganizationService orgService) | |
{ | |
var r = (RetrieveAllEntitiesResponse)orgService.Execute(new RetrieveAllEntitiesRequest() | |
{ | |
EntityFilters = EntityFilters.Entity, | |
RetrieveAsIfPublished = true | |
}); | |
return r.EntityMetadata.Where(x=> x.LogicalName.StartsWith(prefix)).Select(x => x.LogicalName).ToList(); | |
} | |
private IEnumerable<dynamic> GetAttributes(IOrganizationService orgService, string entity) | |
{ | |
var entityMetadata = ((RetrieveEntityResponse)orgService.Execute(new RetrieveEntityRequest() | |
{ | |
LogicalName = entity, | |
EntityFilters = EntityFilters.Attributes, | |
RetrieveAsIfPublished = true | |
})).EntityMetadata; | |
return | |
entityMetadata.Attributes.Select(x => new | |
{ | |
Name = x.LogicalName, | |
SchemaName = x.SchemaName, | |
Type = x.AttributeType.Value.ToString(), | |
}) | |
.OrderBy(x => x.Name); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach (var targetAttribute in targetAttributes) | |
{ | |
var sourceAttribute = sourceAttributes.Where(x => x.Name == targetAttribute.Name).FirstOrDefault(); | |
if (sourceAttribute == null) | |
{ | |
continue; | |
} | |
if (!String.Equals(targetAttribute.SchemaName, sourceAttribute.SchemaName, StringComparison.Ordinal)) | |
{ | |
$"{targetEntity}.{targetAttribute.Name} has different casing {targetAttribute.SchemaName} & {sourceAttribute.SchemaName}".Dump(); | |
continue; | |
} | |
if (!String.Equals(targetAttribute.Type, sourceAttribute.Type, StringComparison.OrdinalIgnoreCase)) | |
{ | |
$"{targetEntity}.{targetAttribute.Name} has different type: {targetAttribute.Type} & {sourceAttribute.Type}".Dump(); | |
continue; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string prefix = "new"; | |
var sourceConnectionString = $@"Url=https://SOURCE.crm.dynamics.com;AuthType=Office365;UserName=USERNAME;Password=PASSWORD;RequireNewInstance=true;"; | |
var targetConnectionString = $@"Url=https://TARGET.crm.dynamics.com;AuthType=Office365;UserName=USERNAME;Password=PASSWORD;RequireNewInstance=true;"; | |
var sourceCrmSvcClient = new CrmServiceClient(sourceConnectionString); | |
var targetCrmSvcClient = new CrmServiceClient(targetConnectionString); | |
var targetEntities = GetEntities(prefix, targetCrmSvcClient); | |
//<removed for brevity> | |
private IList<String> GetEntities(string prefix, IOrganizationService orgService) | |
{ | |
var r = (RetrieveAllEntitiesResponse)orgService.Execute(new RetrieveAllEntitiesRequest() | |
{ | |
EntityFilters = EntityFilters.Entity, | |
RetrieveAsIfPublished = true | |
}); | |
return r.EntityMetadata.Where(x=> x.LogicalName.StartsWith(prefix)).Select(x => x.LogicalName).ToList(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
sourceAttributes = GetAttributes(sourceCrmSvcClient, targetEntity); | |
} | |
catch { $"Entity {targetEntity} does not exist in source".Dump(); continue;} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach (var targetEntity in targetEntities) | |
{ | |
var targetAttributes = GetAttributes(targetCrmSvcClient, targetEntity); | |
//<removed for brevity> | |
} | |
private IEnumerable<dynamic> GetAttributes(IOrganizationService orgService, string entity) | |
{ | |
var entityMetadata = ((RetrieveEntityResponse)orgService.Execute(new RetrieveEntityRequest() | |
{ | |
LogicalName = entity, | |
EntityFilters = EntityFilters.Attributes, | |
RetrieveAsIfPublished = true | |
})).EntityMetadata; | |
return | |
entityMetadata.Attributes.Select(x => new | |
{ | |
Name = x.LogicalName, | |
SchemaName = x.SchemaName, | |
Type = x.AttributeType.Value.ToString(), | |
}) | |
.OrderBy(x => x.Name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment