IOrganisationService.Retrieve记录不存在
IOrganisationService.Retrieve Record does not exist
我编写了代码来检查 CRM 中的记录是否存在。问题是 IOrganisationService.Retrieve returns 一个错误,而不是一个 null 当没有找到记录时。我希望找不到多个记录,我不想使用 try catch 然后使用 catch 中的错误。
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null))
{
IOrganizationService service = (IOrganizationService)serviceProxy;
//Get record
var record = service.Retrieve(entryId, guid, new ColumnSet(true)); //switch to var if no work
//Check if record is not null/empty
recordExists = !String.IsNullOrWhiteSpace(record.Id.ToString()); //<- does the record exist
}
建议?
使用 RetrieveMultiple
检索具有您感兴趣的 ID 的用户:
// Create and cache the plugin context
this.context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
this.service = serviceFactory.CreateOrganizationService(this.context.UserId);
// Retrieve users with certain ID
Guid userId = Guid.NewGuid();
var query = new QueryExpression("systemuser");
query.Criteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, userId));
EntityCollection users;
try
{
users = this.service.RetrieveMultiple(query);
}
catch (FaultException<OrganizationServiceFault> faultException)
{
throw new InvalidPluginExecutionException($"Failed to retrieve system users that have an ID of '{userId}'", faultException);
}
if (users.Entities.Length == 0) // (or !users.Entities.Any() using Linq)
{
// Do something
}
方法Retrieve
假定具有给定ID的记录实际存在于系统中。一般来说,它只会在两种情况下失败:
- 记录已被其他用户或进程删除。
- 用户没有足够的读取权限。
当您需要查询可能不存在的数据时,或者当一个查询可以产生零条、一条或多条记录时,请使用QueryExpression
查询;您可以使用 RetrieveMultiple
方法发出这些查询。如果愿意,您还可以使用 LINQ for CRM,它基本上包装了 QueryExpression
功能。
我编写了代码来检查 CRM 中的记录是否存在。问题是 IOrganisationService.Retrieve returns 一个错误,而不是一个 null 当没有找到记录时。我希望找不到多个记录,我不想使用 try catch 然后使用 catch 中的错误。
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null))
{
IOrganizationService service = (IOrganizationService)serviceProxy;
//Get record
var record = service.Retrieve(entryId, guid, new ColumnSet(true)); //switch to var if no work
//Check if record is not null/empty
recordExists = !String.IsNullOrWhiteSpace(record.Id.ToString()); //<- does the record exist
}
建议?
使用 RetrieveMultiple
检索具有您感兴趣的 ID 的用户:
// Create and cache the plugin context
this.context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
this.service = serviceFactory.CreateOrganizationService(this.context.UserId);
// Retrieve users with certain ID
Guid userId = Guid.NewGuid();
var query = new QueryExpression("systemuser");
query.Criteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, userId));
EntityCollection users;
try
{
users = this.service.RetrieveMultiple(query);
}
catch (FaultException<OrganizationServiceFault> faultException)
{
throw new InvalidPluginExecutionException($"Failed to retrieve system users that have an ID of '{userId}'", faultException);
}
if (users.Entities.Length == 0) // (or !users.Entities.Any() using Linq)
{
// Do something
}
方法Retrieve
假定具有给定ID的记录实际存在于系统中。一般来说,它只会在两种情况下失败:
- 记录已被其他用户或进程删除。
- 用户没有足够的读取权限。
当您需要查询可能不存在的数据时,或者当一个查询可以产生零条、一条或多条记录时,请使用QueryExpression
查询;您可以使用 RetrieveMultiple
方法发出这些查询。如果愿意,您还可以使用 LINQ for CRM,它基本上包装了 QueryExpression
功能。