如何使用 web api 获取资源的可用性?

how to get resource's availability using web api?

CRM 2016 公开了 odata/web api,开箱即用 functions and actions

有了组织服务,我们可以发出请求like this:

// Create the van required resource object.
RequiredResource vanReq = new RequiredResource
{
    ResourceId = _vanId,
    ResourceSpecId = _specId
};

// Create the appointment request.
AppointmentRequest appointmentReq = new AppointmentRequest
{
    RequiredResources = new RequiredResource[] { vanReq },
    Direction = SearchDirection.Backward,
    Duration = 60,
    NumberOfResults = 10,
    ServiceId = _plumberServiceId,
    // The search window describes the time when the resouce can be scheduled.
    // It must be set.
    SearchWindowStart = DateTime.Now.ToUniversalTime(),
    SearchWindowEnd = DateTime.Now.AddDays(7).ToUniversalTime(),
    UserTimeZoneCode = 1
};

// Verify whether there are openings available to schedule the appointment using this resource              
SearchRequest search = new SearchRequest
{
    AppointmentRequest = appointmentReq
};
SearchResponse searched = (SearchResponse)_serviceProxy.Execute(search);

if (searched.SearchResults.Proposals.Length > 0)
{
    Console.WriteLine("Openings are available to schedule the resource.");
}

是否可以使用 functions/action 或任何其他 odata 功能来模拟此功能?

我认为请求应该是这样的:

crmOrg/api/v8.1/Search(AppointmentRequest=@request)?@request=

但是,我不确定如何对请求的其余部分进行编码。

参数如下:

http://yourcrm.org/org/api/data/v8.1/Search(AppointmentRequest=@ar)/?@ar={SearchWindowStart:%272017-01-01%27,Duration:60,NumberOfResults:10}

它是 url 编码 json 序列化 AppointmentRequest class。

{
  SearchWindowStart:'2017-01-01',
  Duration: 60,
  NumberOfResults:10,
  etc...
}

更多信息在这里:https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.appointmentrequest.aspx

ODATA 参考:http://odata.github.io/WebApi/04-06-function-parameter-support/