I want to create an console application which can retrieve all duplicate records of any specific entity and wants to dump the records in the file.
I have been trying with this example. But facing issue in retrieving the entity. As I said it can be any entity provided by the customer I need to validate that entity name as well.
I have also tried below code, but it will try to get duplicate records for specific account id:
private void RetrieveDuplicates()
{
string entityLogicalName = "account";
var entity = _serviceProxy.Retrieve(entityLogicalName, Guid.NewGuid(), new ColumnSet("name"));
// PagingInfo is Required.
var request = new RetrieveDuplicatesRequest
{
BusinessEntity = entity,
MatchingEntityName = entityLogicalName,
PagingInfo = new PagingInfo() { PageNumber = 100, Count = 250 }
};
Console.WriteLine("Retrieving duplicates");
var response = (RetrieveDuplicatesResponse)_serviceProxy.Execute(request);
}
Well, RetrieveMultiple is the answer here.
What I have done now is created a new methods as below to retrieve all the data:
Main method
public void RetrieveDuplicates(IOrganizationService _crmService)
{
Console.Write("Please enter entity's logical name: ");
string entityLogicalName = Console.ReadLine();
//entityLogicalName = "account";
Console.Write("Please enter duplicated column: ");
string duplicatedColumn = Console.ReadLine();
QueryExpression query = new QueryExpression(entityLogicalName)
{
ColumnSet = new ColumnSet(duplicatedColumn)
};
query.AddOrder(duplicatedColumn, 0);
var results = _crmService.RetrieveAll(query)
.GroupBy(e => e.GetAttributeValue<string>(duplicatedColumn), e => e);
foreach (var group in results)
{
var count2 = group.Count();
if (count2 > 1)
{
using (StreamWriter writer = new StreamWriter("important" + DateTime.Now.ToString("ddMMMyyyyhhmm") + ".txt"))
{
foreach (var entity in group)
{
foreach (var attribute in entity.Attributes)
{
writer.WriteLine("{0} => {1} || ", attribute.Key, attribute.Value);
}
}
}
}
}
}
To execute all method
public IEnumerable<Entity> RetrieveAll(this IOrganizationService crmService, QueryExpression query)
{
if (crmService == null)
{
throw new ArgumentNullException(nameof(crmService));
}
if (query == null)
{
throw new ArgumentNullException(nameof(query));
}
if (query.TopCount.HasValue)
{
// You cannot use paging info with a top count
var results = crmService.RetrieveMultiple(query);
foreach (var entity in results.Entities) yield return entity;
}
else
{
EntityCollection results = null;
query.PageInfo = new PagingInfo
{
PageNumber = 1
};
do
{
results = crmService.RetrieveMultiple(query);
query.PageInfo.PageNumber++;
query.PageInfo.PagingCookie = results.PagingCookie;
foreach (var entity in results.Entities) yield return entity;
} while (results?.MoreRecords != false);
}
}