如果需要通过SOAP端点获取实体的所有元数据,可以使用RetrieveEntityRequest:
var request = new RetrieveEntityRequest
{
EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.All,
LogicalName = "account"
}
var response = (RetrieveEntityResponse)organizationService.Execute(request)
EntityFilters 是一个枚举,它允许您指定要获取的元数据内容:
[Flags]
public enum EntityFilters
{
Entity = 1,
Default = 1,
Attributes = 2,
Privileges = 4,
Relationships = 8,
All = 15
}
这是一个标志枚举,您可以像这样使用它:
var request = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.Privileges | EntityFilters.Entity,
LogicalName = "account"
}
或者简单地使用
All
值来获取所有必要的元数据。在您的尝试中,您未能检索到元数据,因为您仅请求实体元数据,而您对属性元数据感兴趣。
因此,以您的代码片段为基础,我会按照以下方式使用它:
[Route("Account", Name = "CreateAccount", Order = 1)]
[HttpPost]
public Account CreateAccount([FromBody] Account account)
{
VerifyRequiredFields(account);
var newAccount = _accountService.CreateAccountEntity(account);
return newAccount;
}
private void VerifyRequiredFields(Account account)
{
var response = GetEntityMetadata(account);
var requiredAttributes = response.EntityMetadata.Attributes.Where(a => a.RequiredLevel?.Value == AttributeRequiredLevel.SystemRequired);
foreach(var requiredAttribute in requiredAttributes)
{
if(CheckIfValueIsProvided(requiredAttribute.LogicalName, account))
{
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, $"You are missing required value {requiredAttribute.LogicalName}"));
}
}
}
方法
GetEntityMetadata
只是在执行前一个示例中的操作,因此调用
RetrieveEntityRequest
并返回
RetrieveEntityResponse
。
当然,方法
CheckIfValueIsProvided
的实现取决于您的帐户模型类如何定义,但您可能需要在您的模型和CRM实体模型之间建立某种映射(以知道如何将例如字段“accountnumber”映射到您的模型中的某个字段)。这远远超出了本问题的范围,但我相信您已经知道足够启动。
请记住,这只是一个示例。您不应该将此逻辑保留在控制器类中,而应将其移至某个实用程序类中,以便在不同的控制器中重复使用。元数据很少更改(您可能对此更改有控制),因此您也可能希望在Web应用程序中缓存元数据等。我希望您已经有了一个想法,但整个逻辑设计另外一回事。
如果您想从JavaScript中执行此操作,您可能应该坚持使用webAPI:
http://CRMADDRESS/api/data/v8.2/EntityDefinitions(LogicalName='account')/Attributes?$select=LogicalName,RequiredLevel
这将为您获取所需的内容(属性名称及其所需级别)。它看起来会像这样:
{
"LogicalName":"preferredcontactmethodcodename","RequiredLevel":{
"Value":"None","CanBeChanged":false,"ManagedPropertyLogicalName":"canmodifyrequirementlevelsettings"
},"MetadataId":"8663b910-af86-4dea-826e-8222706372f4"
},{
"@odata.type":"#Microsoft.Dynamics.CRM.StringAttributeMetadata","LogicalName":"emailaddress3","RequiredLevel":{
"Value":"None","CanBeChanged":true,"ManagedPropertyLogicalName":"canmodifyrequirementlevelsettings"
},"MetadataId":"97fb4aae-ea5d-427f-9b2b-9a6b9754286e"
},{
"@odata.type":"#Microsoft.Dynamics.CRM.StringAttributeMetadata","LogicalName":"emailaddress2","RequiredLevel":{
"Value":"None","CanBeChanged":true,"ManagedPropertyLogicalName":"canmodifyrequirementlevelsettings"
},"MetadataId":"98b09426-95ab-4f21-87a0-f6775f2b4210"
},{
"@odata.type":"#Microsoft.Dynamics.CRM.StringAttributeMetadata","LogicalName":"emailaddress1","RequiredLevel":{
"Value":"None","CanBeChanged":true,"ManagedPropertyLogicalName":"canmodifyrequirementlevelsettings"
},"MetadataId":"b254ab69-de5a-4edb-8059-bdeb6863c544"
},{
"@odata.type":"#Microsoft.Dynamics.CRM.StringAttributeMetadata","LogicalName":"masteraccountidyominame","RequiredLevel":{
"Value":"None","CanBeChanged":false,"ManagedPropertyLogicalName":"canmodifyrequirementlevelsettings"
},"MetadataId":"a15dedfc-9382-43ac-8d10-7773aa3eefeb"
},{
"@odata.type":"#Microsoft.Dynamics.CRM.StringAttributeMetadata","LogicalName":"address1_city","RequiredLevel":{
"Value":"None","CanBeChanged":true,"ManagedPropertyLogicalName":"canmodifyrequirementlevelsettings"
},"MetadataId":"ca8d0a94-8569-4154-b511-718e11635449"
},{
"@odata.type":"#Microsoft.Dynamics.CRM.LookupAttributeMetadata","LogicalName":"slaid","RequiredLevel":{
"Value":"None","CanBeChanged":true,"ManagedPropertyLogicalName":"canmodifyrequirementlevelsettings"
},"MetadataId":"6bdcd7f1-5865-4fef-91b0-676824b18641"
}
你可以使用这个来在客户端验证请求,给用户一个提示,在他向服务器发送请求之前,提醒他缺少重要的数据。