我想创建一个控制台应用程序,该应用程序可以检索任何特定实体的所有重复记录,并希望将记录转储到文件中。
我一直在尝试使用this示例。但是在检索实体时面临问题。正如我所说的,它可以是客户提供的任何实体,我也需要验证该实体名称。
我也尝试了以下代码,但是它将尝试获取特定帐户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);
}
答案 0 :(得分:0)
好吧, RetrieveMultiple 是这里的答案。
我现在所做的是创建一个如下所示的新方法来检索所有数据:
主要方法
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);
}
}
}
}
}
}
执行所有方法
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);
}
}