Dynamics 365搜索资源可用性

问题描述 投票:1回答:2

我正在尝试实现此处所述的搜索资源功能:https://cloudblogs.microsoft.com/dynamics365/it/2019/05/21/retrieve-resource-availability-with-universal-resource-scheduling-api/

[这里有一个示例,介绍了如何通过JavaScript来实现它(尽管JavaScript库可能已被弃用或不受支持),我在这里已经引用了它:https://cloudblogs.microsoft.com/dynamics365/it/2019/07/15/how-to-use-resource-schedulings-search-resource-availability-api/

我已经编写了一个.NET Core类库,该库使用Dynamics 365 OData服务将POST张贴到msdyn_SearchResourceAvailability操作。

我在互联网上看到了一些示例,但是它们都使用Dynamics 365 SDK,而不是Dynamics 365 Web API。

我遇到错误,因此提取了要发布的JSON,并在Postman中尝试了相同的调用,在此我遇到了相同的错误:

{
"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, ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.ReadResource(ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)\r\n   at Microsoft.Crm.Extensibility.ODataV4.CrmODataActionPayloadDeserializer.ReadEntry(ODataDeserializerContext readContext, ODataParameterReader reader, IEdmOperationParameter parameter)\r\n   at Microsoft.Crm.Extensibility.ODataV4.CrmODataActionPayloadDeserializer.Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)",
    "innererror": {
        "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, ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.ReadResource(ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)\r\n   at Microsoft.Crm.Extensibility.ODataV4.CrmODataActionPayloadDeserializer.ReadEntry(ODataDeserializerContext readContext, ODataParameterReader reader, IEdmOperationParameter parameter)\r\n   at Microsoft.Crm.Extensibility.ODataV4.CrmODataActionPayloadDeserializer.Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)",
        "type": "Microsoft.Crm.CrmHttpException",
        "stacktrace": "   at Microsoft.Crm.Extensibility.OData.CrmODataUtilities.ValidateInputParameters(ModelStateDictionary controllerModelState)\r\n   at Microsoft.Crm.Extensibility.OData.ActionController.<>c__DisplayClass9_0.<PostUnboundAction>b__0()\r\n   at Microsoft.PowerApps.CoreFramework.ActivityLoggerExtensions.Execute[TResult](ILogger logger, EventId eventId, ActivityType activityType, Func`1 func, IEnumerable`1 additionalCustomProperties)\r\n   at Microsoft.Xrm.Telemetry.XrmTelemetryExtensions.Execute[TResult](ILogger logger, XrmTelemetryActivityType activityType, Func`1 func)\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
    }
}
}

正在发布的JSON如下:

{
"Version": "1",
"Requirement": {
    "msdyn_duration": 180,
    "msdyn_effort": 1,
    "msdyn_fromdate": "2020-03-10T00:00:00+00:00",
    "msdyn_latitude": 55.784129,
    "msdyn_longitude": -3.982742,
    "msdyn_name": "Super Heroes Resource Requirement",
    "msdyn_remainingduration": 180,
    "msdyn_todate": "2020-03-12T00:00:00+00:00",
    "msdyn_worklocation": 690970002
},
"Settings": {
    "ConsiderSlotsWithLessThanRequiredCapacity": false,
    "ConsiderSlotsWithLessThanRequiredDuration": false,
    "ConsiderTravelTime": false,
    "ConsiderSlotsWithOverlappingBooking": false,
    "ConsiderSlotsWithProposedBookings": false,
    "MovePastStartDateToCurrentDate": false,
    "UseRealTimeResourceLocation": false,
    "MaxResourceTravelRadius": {
        "Value": 10,
        "Unit": 192350000
    },
    "SortOrder": {
        "value": [
            {
                "Name": "bookableresource",
                "SortOrder": 0
            }
        ]
    }
},
"ResourceSpecification": {
    "ResourceTypes": {
        "value": [
            2,
            3,
            5
        ]
    },
    "PreferredResources": {
        "value": [
            {
                "bookableresourceid": "d7315245-b162-ea11-a811-000d3a0bad7c"
            },
            {
                "bookableresourceid": "b54bc744-b162-ea11-a811-000d3a0ba110"
            }
        ]
    },
    "RestrictedResources": {
        "value": [
            {
                "bookableresourceid": "ba6d4a4b-b162-ea11-a811-000d3a0bad7c"
            },
            {
                "bookableresourceid": "ca6d4a4b-b162-ea11-a811-000d3a0bad7c"
            }
        ]
    },
    "Constraints": {
        "Characteristics": {
            "value": [
                {
                    "characteristicid": "a02db73e-b162-ea11-a811-000d3a0ba110"
                }
            ]
        },
        "Roles": {
            "value": [
                {
                    "bookableresourcecategoryid": "d56d4a4b-b162-ea11-a811-000d3a0bad7c"
                }
            ]
        },
        "Territories": {
            "value": [
                {
                    "territoryid": "7c2db73e-b162-ea11-a811-000d3a0ba110"
                }
            ]
        },
        "UnspecifiedTerritory": false,
        "OrganizationalUnits": {
            "value": [
                {
                    "msdyn_organizationalunitid": "822db73e-b162-ea11-a811-000d3a0ba110"
                }
            ]
        },
        "BusinessUnits": {
            "value": [
                {
                    "businessunitid": "fba6cf4b-f24a-ea11-a813-00224801cd21"
                }
            ]
        }
    }
}
}

谁能告诉我我要去哪里错了吗?

dynamics-crm dynamics-365 dynamics-crm-webapi
2个回答
0
投票

我能够从v8.7.x升级到现场服务v8.8.x,并使用具有以下负载的Web api测试CRM操作msdyn_SearchResourceAvailability。我似乎没有所有的配置和数据设置,但Web API产生了良好的响应(不同于400 =错误的请求..大声笑)

var parameters = {};
parameters.Version = "2";
var requirement = {};
requirement.msdyn_resourcerequirementid = "B9E6F413-0063-EA11-A811-000D3A5A1CAC"; //Delete if creating new record 
requirement["@odata.type"] = "Microsoft.Dynamics.CRM.msdyn_resourcerequirement";
parameters.Requirement = requirement;
var settings = {};
settings.systemuserid = "26ADDD07-D9F4-E711-8138-E0071B715B11"; //Delete if creating new record 
settings["@odata.type"] = "Microsoft.Dynamics.CRM.systemuser";
parameters.Settings = settings;

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/msdyn_SearchResourceAvailability", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            alert(this.response)
        } else {
            alert(this.status);
        }
    }
};
req.send(JSON.stringify(parameters));

响应:

{
  "@odata.context": "https://crmdev.crm.dynamics.com/api/data/v9.1/$metadata#Microsoft.Dynamics.CRM.msdyn_SearchResourceAvailabilityResponse",
  "TimeSlots": [
    {
      "@odata.type": "#Microsoft.Dynamics.CRM.organization"
    },
    {
      "@odata.type": "#Microsoft.Dynamics.CRM.organization"
    },
    {
      "@odata.type": "#Microsoft.Dynamics.CRM.organization"
    },
    {
      "@odata.type": "#Microsoft.Dynamics.CRM.organization"
    },
    {
      "@odata.type": "#Microsoft.Dynamics.CRM.organization"
    }
  ],
  "Resources": [
    {
      "@odata.type": "#Microsoft.Dynamics.CRM.organization"
    }
  ],
  "Related": {
    "@odata.type": "#Microsoft.Dynamics.CRM.organization"
  },
  "Exceptions": {
    "@odata.type": "#Microsoft.Dynamics.CRM.organization"
  }
}

更新:

  1. 此调用仅需要版本,要求和设置,因此从最少的代码输入开始并对其进行增强

  2. 对错误消息Microsoft.OData.ODataException: Does not support untyped value in non-open type进行故障排除时,请按此方向进行操作-typo in some schema name could be the reason

  3. 不知道此msdyn_SearchResourceAvailability操作消息在Web api中是否仍然可用,但仅尝试了OrganizationRequest SDK [Reference


这是示例请求以及所需的有效载荷:

var parameters = {};
var workorder = {};
workorder.msdyn_workorderid = "ADE6F413-0063-EA11-A811-000D3A5A1CAC"; //Delete if creating new record 
workorder["@odata.type"] = "Microsoft.Dynamics.CRM.msdyn_workorder";
parameters.WorkOrder = workorder;
parameters.RealTimeMode = true;
parameters.Duration = 30;
parameters.IgnoreDuration = true;
parameters.IgnoreTravelTime = true;
parameters.AllowOverlapping = true;
parameters.Radius = 0;
parameters.StartTime = new Date("3/10/2020").toISOString();
parameters.EndTime = new Date("3/10/2020").toISOString();
var resources1 = {};
resources1.systemuserid = "3BD2ADED-20B2-E911-A98E-000D3A374B53"; //Delete if creating new record 
resources1["@odata.type"] = "Microsoft.Dynamics.CRM.systemuser";
parameters.Resources = [resources1];

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/msdyn_RetrieveResourceAvailability", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            alert("response: "+this.response)
        } else {
            alert(this.status);
        }
    }
};
req.send(JSON.stringify(parameters));

我刚刚在沙盒中生成了此数据,但没有任何数据,但是您可以尝试使用CRM REST构建器来建立请求。


0
投票

部分问题是,C#使用该SDK进行采样,而该SDK又使用了已弃用的2011 WCF服务,而不是当前的OData服务。 OData服务似乎在您指定的@ odata.type方面更为严格,因为它看起来是要检查所提供的属性是该实体的真实属性/字段。 SettingsResourceSpecification的问题在于,在[[Action中,未指定实体的类型。

© www.soinside.com 2019 - 2024. All rights reserved.