Dynamics 365 Web API - 如何使用 Web API 为 Webhook 注册步骤?

问题描述

我正在探索 Dynamics CRM Web API 并尝试注册一个将在更新/创建实体(如联系人/帐户)时触发的 Webhook。我可以使用 PluginRegistration 工具创建此设置,但我想从我的工作流程中排除该工具。如果可能,我想注册 webhook 和通过 web API 的步骤。我能够成功注册一个 webhook - 使用对以下端点的 POST 请求:

/api/data/v9.0/serviceendpoints

具有以下正文:

{
    "name":"Test Webhook","url":"http://somewebhookendpoint.somedomain.com","contract":8,"authtype":4,"authvalue":"args"
}

但是,当尝试创建 sdkmessageprocessingstep(我假设这是我尝试在此 webhook 下创建的步骤)时,出现 400 Bad Request 错误。这是我传递给 sdkmessageprocessingstep 创建端点 (api/data/v9.0/sdkmessageprocessingsteps)

的请求正文
{
        "name":"Test for Step Creation","stage":40,"rank":1,"eventhandlerid":"04e08d50-e63b-eb11-a813-000d3a0a7552","sdkmessageid":"9ebdbb1b-ea3e-db11-86a7-000a3a5473e8","sdkmessagefilterid": "c2c5bb1b-ea3e-db11-86a7-000a3a5473e8","supporteddeployment":0,"description":"TEST FOR STEP CREATION #1"
}

但在传递此请求后,我收到以下错误(这似乎表明我没有以正确的格式发送 sdkmessageid)。

{
    "error": {
        "code": "0x0","message": "An error occurred while validating input parameters: Microsoft.OData.ODataException: A 'PrimitiveValue' node with non-null value was found when trying to read the value of the property 'sdkmessageid'; however,a 'StartArray' node,a 'StartObject' node,or a 'PrimitiveValue' node with null value was expected.\r\n   at Microsoft.OData.JsonLight.ODataJsonLightPropertyAndValueDeserializer.ValidateExpandednestedResourceInfoPropertyValue(IJsonReader jsonReader,Nullable`1 isCollection,String propertyName)\r\n   at Microsoft.OData.JsonLight.ODataJsonLightResourceDeserializer.ReadPropertyWithValue(IODataJsonLightReaderResourceState resourceState,String propertyName,Boolean isDeltaResourceSet)\r\n   at Microsoft.OData.JsonLight.ODataJsonLightResourceDeserializer.<>c__displayClass9_0.<ReadResourceContent>b__0(PropertyParsingResult propertyParsingResult,String propertyName)\r\n   at Microsoft.OData.JsonLight.ODataJsonLightDeserializer.Processproperty(PropertyAndAnnotationCollector propertyAndAnnotationCollector,Func`2 readPropertyAnnotationValue,Action`2 handleProperty)\r\n   at Microsoft.OData.JsonLight.ODataJsonLightResourceDeserializer.ReadResourceContent(IODataJsonLightReaderResourceState resourceState)\r\n   at Microsoft.OData.JsonLight.ODataJsonLightReader.StartReadingResource()\r\n   at Microsoft.OData.JsonLight.ODataJsonLightReader.ReadResourceSetItemStart(PropertyAndAnnotationCollector propertyAndAnnotationCollector,SelectedPropertiesNode selectedProperties)\r\n   at Microsoft.OData.JsonLight.ODataJsonLightReader.ReadAtStartImplementationSynchronously(PropertyAndAnnotationCollector propertyAndAnnotationCollector)\r\n   at Microsoft.OData.ODataReaderCore.ReadImplementation()\r\n   at Microsoft.OData.ODataReaderCore.InterceptException[T](Func`1 action)\r\n   at System.Web.OData.Formatter.Deserialization.ODataReaderExtensions.ReadResourceOrResourceSet(ODataReader reader)\r\n   at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.Read(ODataMessageReader messageReader,Type type,ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.ReadFromStream(Type type,Stream readStream,HttpContent content,IFormatterLogger formatterLogger)"
    }
}

我不确定应该如何传递 eventhandlerid,sdkmessageid & sdkmessagefilterid。我尝试将它作为对象传递 - 就像这样:

{
        "name":"test through json","EventHandler":{"@id":"04e08d50-e63b-eb11-a813-000d3a0a7552"},"SdkMessage":{"@id":"9ebdbb1b-ea3e-db11-86a7-000a3a5473e8"},"SdkMessageFilter": {"@id":"c2c5bb1b-ea3e-db11-86a7-000a3a5473e8"},"description":"TEST through JSON #1"
}

但无济于事(删除@ 也不起作用)。响应似乎表明我传递了一些不正确的参数:

{
    "error": {
        "code": "0x0","message": "An error occurred while validating input parameters: Microsoft.OData.ODataException: Does not support untyped value in non-open type.\r\n   at System.Web.OData.Formatter.Deserialization.DeserializationHelpers.ApplyProperty(ODataProperty property,IEdmStructuredTypeReference resourceType,Object resource,ODataDeserializerProvider deserializerProvider,ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.ApplyStructuralProperties(Object resource,ODataResourceWrapper resourceWrapper,IEdmStructuredTypeReference structuredType,ODataDeserializerContext readContext)\r\n   at Microsoft.Crm.Extensibility.CrmODataEntityDeserializer.ApplyStructuralProperties(Object resource,ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.ReadResource(ODataResourceWrapper resourceWrapper,IFormatterLogger formatterLogger)"
    }
}

有人可以指定我如何使用 Dynamics Web API 在 sdkmessageprocessingstep注册 serviceendpoint 吗?

解决方法

可能是单值导航属性(查找)分配问题,但错误消息有点不同。

我还没有测试过这个payload,所以请测试一下。顺便说一句,CRM REST 构建器在这些场景中有助于构建 Web api 片段。

{
        "name":"Test for Step Creation","stage":40,"rank":1,"eventhandler_serviceendpoint@odata.bind":"/serviceendpoints(04e08d50-e63b-eb11-a813-000d3a0a7552)","sdkmessageid@odata.bind":"/sdkmessages(9ebdbb1b-ea3e-db11-86a7-000a3a5473e8)","sdkmessagefilterid@odata.bind": "/sdkmessagefilters(c2c5bb1b-ea3e-db11-86a7-000a3a5473e8)","supporteddeployment":0,"description":"TEST FOR STEP CREATION #1"
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...