Web API 查询数据示例

 

发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

这组示例演示如何使用 Microsoft Dynamics 365 Web API 查询数据。 此示例作为一个单独的项目为以下语言实施:

本主题介绍了本组中每个示例实施的一组普通操作: 本主题介绍本组中各示例将在无语言特定的详细信息的情况下执行的 HTTP 请求和响应以及文本输出。 有关如何执行此操作的详细信息,请参阅语言特定的说明和单独的示例。

演示

此示例分为以下主要部分,其中包含关联的概念主题中更详细地讨论的 Web API 查询数据操作。

主题部分

关联的主题

选择特定属性

检索特定属性

包含格式化值

使用查询函数

筛选结果

标准查询函数

使用函数组成查询

Web API Query Function Reference

使用运算符

标准筛选器运算符

设置优先级

标准筛选器运算符

排序结果

排序结果

筛选结果

参数别名

将参数别名与系统查询选项一起使用。

限制结果

限制结果

对返回实体数目的限制

扩展结果

通过扩展导航属性检索相关实体

FetchXML 查询

FetchXML schema

使用 FetchXML 对大型结果集进行分页

使用自定义 FetchXML

预定义的查询

检索和执行预定义的查询

userquery EntityType

savedquery EntityType

以下章节包含执行的 Dynamics 365 Web API 操作的简短讨论,以及相应的 HTTP 消息和关联的控制台输出。

示例数据

为了确保此示例中的查询正常工作,此示例创建了一组标准的示例记录。 除非用户选择不删除这些示例记录,否则将删除它们。 这是示例将查询的数据。 结果结果可能有所不同,具体取决于环境中的任何现有数据。

数据使用深度插入添加到一个 POST 请求中,并与以下结构匹配:

    {
           "name": "Contoso, Ltd. (sample)",
           "primarycontactid": {
               "firstname": "Yvonne", "lastname": "McKay (sample)", "jobtitle": "Coffee Master",
               "annualincome": 45000, "Contact_Tasks": [
               { "subject": "Task 1", "description": "Task 1 description" },
               { "subject": "Task 2", "description": "Task 2 description" },
               { "subject": "Task 3", "description": "Task 3 description" }
               ]
           }, 
                            "Account_Tasks": [
           { "subject": "Task 1", "description": "Task 1 description" },
           { "subject": "Task 2", "description": "Task 2 description" },
           { "subject": "Task 3", "description": "Task 3 description" }
           ],
           "contact_customer_accounts": [
               {
                   "firstname": "Susanna", "lastname": "Stubberod (sample)", "jobtitle": "Senior Purchaser",
                   "annualincome": 52000, "Contact_Tasks": [
               { "subject": "Task 1", "description": "Task 1 description" },
               { "subject": "Task 2", "description": "Task 2 description" },
               { "subject": "Task 3", "description": "Task 3 description" }
                   ]
               },
               {
                   "firstname": "Nancy", "lastname": "Anderson (sample)", "jobtitle": "Activities Manager",
                   "annualincome": 55500, "Contact_Tasks": [
                   { "subject": "Task 1", "description": "Task 1 description" },
                   { "subject": "Task 2", "description": "Task 2 description" },
                   { "subject": "Task 3", "description": "Task 3 description" }
                   ]
               },
               {
                   "firstname": "Maria", "lastname": "Cambell (sample)", "jobtitle": "Accounts Manager",
                   "annualincome": 31000, "Contact_Tasks": [
                   { "subject": "Task 1", "description": "Task 1 description" },
                   { "subject": "Task 2", "description": "Task 2 description" },
                   { "subject": "Task 3", "description": "Task 3 description" }
                   ]
               },
               {
                   "firstname": "Nancy", "lastname": "Anderson (sample)", "jobtitle": "Logistics Specialist",
                   "annualincome": 63500, "Contact_Tasks": [
                   { "subject": "Task 1", "description": "Task 1 description" },
                   { "subject": "Task 2", "description": "Task 2 description" },
                   { "subject": "Task 3", "description": "Task 3 description" }
                   ]
               },
               {
                   "firstname": "Scott", "lastname": "Konersmann (sample)", "jobtitle": "Accounts Manager",
                   "annualincome": 38000, "Contact_Tasks": [
                   { "subject": "Task 1", "description": "Task 1 description" },
                   { "subject": "Task 2", "description": "Task 2 description" },
                   { "subject": "Task 3", "description": "Task 3 description" }
                   ]
               },
               {
                   "firstname": "Robert", "lastname": "Lyon (sample)", "jobtitle": "Senior Technician",
                   "annualincome": 78000, "Contact_Tasks": [
                   { "subject": "Task 1", "description": "Task 1 description" },
                   { "subject": "Task 2", "description": "Task 2 description" },
                   { "subject": "Task 3", "description": "Task 3 description" }
                   ]
               },
               {
                   "firstname": "Paul", "lastname": "Cannon (sample)", "jobtitle": "Ski Instructor",
                   "annualincome": 68500, "Contact_Tasks": [
                   { "subject": "Task 1", "description": "Task 1 description" },
                   { "subject": "Task 2", "description": "Task 2 description" },
                   { "subject": "Task 3", "description": "Task 3 description" }
                   ]
               },
               {
                   "firstname": "Rene", "lastname": "Valdes (sample)", "jobtitle": "Data Analyst III",
                   "annualincome": 86000, "Contact_Tasks": [
                   { "subject": "Task 1", "description": "Task 1 description" },
                   { "subject": "Task 2", "description": "Task 2 description" },
                   { "subject": "Task 3", "description": "Task 3 description" }
                   ]
               },
               {
                   "firstname": "Jim", "lastname": "Glynn (sample)", "jobtitle": "Senior International Sales Manager",
                   "annualincome": 81400, "Contact_Tasks": [
                   { "subject": "Task 1", "description": "Task 1 description" },
                   { "subject": "Task 2", "description": "Task 2 description" },
                   { "subject": "Task 3", "description": "Task 3 description" }
                   ]
               }
           ]
       }

选择特定属性

请始终使用 $select 查询选项构造查询,否则服务器将返回每个实体的所有属性,这样会降低性能。 此示例演示如何通过选择 contact EntityType 的三个属性构造基本查询。 这些属性是 fullnamejobtitleannualincome。 本节还说明联系人的 annualincome 属性的结果中看到的已格式化值和未格式化值之间的差异。详细信息:请求特定属性包含格式化值

在此示例中,我们请求一个特定联系人。 在本案例中,这是客户的主要联系人 Yvonne McKay (sample)。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts(b848fdee-c143-e611-80d5-00155da84802)?$select=fullname,jobtitle,annualincome HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 517

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)/$entity",
   "@odata.etag":"W/\"619718\"",
   "fullname":"Yvonne McKay (sample)",
   "jobtitle":"Coffee Master",
   "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
   "annualincome":45000.0000,
   "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
   "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
   "contactid":"15c364b2-bf43-e611-80d5-00155da84802"
}

控制台输出

Contact basic info:
    Fullname: 'Yvonne McKay (sample)'
    Jobtitle: 'Coffee Master'
    Annualincome: '45000' (unformatted)
    Annualincome: $45,000.00 (formatted)

使用查询函数

可使用筛选器选项设置所需结果的条件。 可以使用查询函数、比较运算符和逻辑运算符生成简单到复杂的筛选器。详细信息:筛选结果

查询函数是可用作查询中的筛选器条件的函数。 存在标准查询函数和 Dynamics 365 特定的查询函数。 这些函数接受参数并返回 Boolean 值。 此示例演示如何为每种类型创建一个查询。

标准查询函数

Dynamics 365 支持一小组 OData 内置查询函数,特别是:containsendswithstartswith。 例如,contains 标准查询函数可用于筛选匹配某个字符串的属性。 在此操作中,我们查询 fullname 包含字符串 (sample) 的所有联系人。详细信息:标准查询函数

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(fullname,'(sample)') HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 4284

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "value":[
      {
         "@odata.etag":"W/\"619718\"",
         "fullname":"Yvonne McKay (sample)",
         "jobtitle":"Coffee Master",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
         "annualincome":45000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"15c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619839\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"1cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619841\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"20c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619843\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"24c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619845\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"28c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619847\"",
         "fullname":"Scott Konersmann (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$38,000.00",
         "annualincome":38000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"2cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619849\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"30c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619851\"",
         "fullname":"Paul Cannon (sample)",
         "jobtitle":"Ski Instructor",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$68,500.00",
         "annualincome":68500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"34c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619853\"",
         "fullname":"Rene Valdes (sample)",
         "jobtitle":"Data Analyst III",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$86,000.00",
         "annualincome":86000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"38c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619855\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"3cc364b2-bf43-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contacts filtered by fullname containing '(sample)':
    1) Yvonne McKay (sample), Coffee Master, $45,000.00
    2) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    3) Nancy Anderson (sample), Activities Manager, $55,500.00
    4) Maria Cambell (sample), Accounts Manager, $31,000.00
    5) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    6) Scott Konersmann (sample), Accounts Manager, $38,000.00
    7) Robert Lyon (sample), Senior Technician, $78,000.00
    8) Paul Cannon (sample), Ski Instructor, $68,500.00
    9) Rene Valdes (sample), Data Analyst III, $86,000.00
    10) Jim Glynn (sample), Senior International Sales Manager, $81,400.00

Dynamics 365 查询函数

Dynamics 365 查询函数提供大量选项,用于生成 Dynamics 365 的相关查询。 有关这些函数的完整列表,请参阅 Web API Query Function Reference。详细信息:使用函数组成查询

这些查询函数的使用方式类似标准查询函数的使用方式。 主要差别是,使用 Dynamics 365 查询函数时,您必须提供函数的全名,包括参数名。 例如,若要获取上一小时创建的联系人列表,可以使用 LastXHours Function 构造查询。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=Microsoft.Dynamics.CRM.LastXHours(PropertyName='createdon',PropertyValue='1') HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 4284

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "value":[
      {
         "@odata.etag":"W/\"619718\"",
         "fullname":"Yvonne McKay (sample)",
         "jobtitle":"Coffee Master",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
         "annualincome":45000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"15c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619839\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"1cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619841\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"20c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619843\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"24c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619845\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"28c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619847\"",
         "fullname":"Scott Konersmann (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$38,000.00",
         "annualincome":38000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"2cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619849\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"30c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619851\"",
         "fullname":"Paul Cannon (sample)",
         "jobtitle":"Ski Instructor",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$68,500.00",
         "annualincome":68500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"34c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619853\"",
         "fullname":"Rene Valdes (sample)",
         "jobtitle":"Data Analyst III",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$86,000.00",
         "annualincome":86000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"38c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619855\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"3cc364b2-bf43-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contacts that were created within the last 1hr:
    1) Yvonne McKay (sample), Coffee Master, $45,000.00
    2) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    3) Nancy Anderson (sample), Activities Manager, $55,500.00
    4) Maria Cambell (sample), Accounts Manager, $31,000.00
    5) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    6) Scott Konersmann (sample), Accounts Manager, $38,000.00
    7) Robert Lyon (sample), Senior Technician, $78,000.00
    8) Paul Cannon (sample), Ski Instructor, $68,500.00
    9) Rene Valdes (sample), Data Analyst III, $86,000.00
    10) Jim Glynn (sample), Senior International Sales Manager, $81,400.00

使用运算符

使用标准筛选器运算符eqnegtgeltleandornot)进一步优化结果。 在此示例中,将查询 fullname 包含 (sample) 且年收入超过 55000 的所有联系人的列表。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(fullname,'(sample)')%20and%20annualincome%20gt%2055000 HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 2629

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "value":[
      {
         "@odata.etag":"W/\"619841\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"20c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619845\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"28c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619849\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"30c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619851\"",
         "fullname":"Paul Cannon (sample)",
         "jobtitle":"Ski Instructor",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$68,500.00",
         "annualincome":68500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"34c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619853\"",
         "fullname":"Rene Valdes (sample)",
         "jobtitle":"Data Analyst III",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$86,000.00",
         "annualincome":86000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"38c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619855\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"3cc364b2-bf43-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contacts filtered by fullname and annualincome (<$55,000):
    1) Nancy Anderson (sample), Activities Manager, $55,500.00
    2) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    3) Robert Lyon (sample), Senior Technician, $78,000.00
    4) Paul Cannon (sample), Ski Instructor, $68,500.00
    5) Rene Valdes (sample), Data Analyst III, $86,000.00
    6) Jim Glynn (sample), Senior International Sales Manager, $81,400.00

设置优先级

您将使用括号指定条件的评估顺序。

在此示例中,将查询 fullname 包含 (sample),jobtitle 包含 senior 或 specialist 且 annualincome 超过 55000 的所有联系人的列表。 为了获得所需结果,使用了括号来将 jobtitle 筛选器组合在一起。 由于所有运算符都有相同的优先级,所以忽略括号将为 or 运算符指定与 and 相同的优先级。 筛选器从左到右应用。 这些语句在筛选器中的显示顺序可能影响结果。 本示例中的查询如下所示:$filter=contains(fullname,'(sample)') and (contains(jobtitle,'senior') or contains(jobtitle,'specialist')) and annualincome gt 55000。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(fullname,'(sample)')%20and%20(contains(jobtitle,'senior')%20or%20contains(jobtitle,'specialist'))%20and%20annualincome%20gt%2055000 HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 1393

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "value":[
      {
         "@odata.etag":"W/\"619845\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"28c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619849\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"30c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619855\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"3cc364b2-bf43-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contacts filtered by fullname, annualincome and jobtitle (Senior or Specialist):
    1) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    2) Robert Lyon (sample), Senior Technician, $78,000.00
    3) Jim Glynn (sample), Senior International Sales Manager, $81,400.00

排序结果

可以使用 $orderby 筛选器选项指定结果按升序还是降序排序。 在此示例中,我们将查询 fullname 包含 (sample) 的所有联系人,并使用以下语法请求数据基于 jobtitle 属性值按照升序排序,然后根据 annualincome 属性值按照降序排序:$orderby=jobtitle asc, annualincome desc。详细信息:排序结果

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(fullname,'(sample)')%20&$orderby=jobtitle%20asc,%20annualincome%20desc HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 4284

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "value":[
      {
         "@odata.etag":"W/\"619847\"",
         "fullname":"Scott Konersmann (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$38,000.00",
         "annualincome":38000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"2cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619843\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"24c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619841\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"20c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619718\"",
         "fullname":"Yvonne McKay (sample)",
         "jobtitle":"Coffee Master",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
         "annualincome":45000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"15c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619853\"",
         "fullname":"Rene Valdes (sample)",
         "jobtitle":"Data Analyst III",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$86,000.00",
         "annualincome":86000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"38c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619845\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"28c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619855\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"3cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619839\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"1cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619849\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"30c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619851\"",
         "fullname":"Paul Cannon (sample)",
         "jobtitle":"Ski Instructor",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$68,500.00",
         "annualincome":68500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"34c364b2-bf43-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contacts ordered by jobtitle (ascending) and annualincome (descending):
    1) Scott Konersmann (sample), Accounts Manager, $38,000.00
    2) Maria Cambell (sample), Accounts Manager, $31,000.00
    3) Nancy Anderson (sample), Activities Manager, $55,500.00
    4) Yvonne McKay (sample), Coffee Master, $45,000.00
    5) Rene Valdes (sample), Data Analyst III, $86,000.00
    6) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    7) Jim Glynn (sample), Senior International Sales Manager, $81,400.00
    8) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    9) Robert Lyon (sample), Senior Technician, $78,000.00
    10) Paul Cannon (sample), Ski Instructor, $68,500.00

参数别名

可使用参数别名更轻松地在筛选器中重复利用参数。 可在 $filter$orderby 选项中使用参数化的别名。 如果未分派别名,值会假定为空。 您还可以在调用函数时使用参数别名。详细信息:使用 Web API 功能将参数别名与系统查询选项一起使用。。 以排序结果操作为例,可以使用参数再次编写该查询,并获得相同的输出结果。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(@p1,'(sample)')%20&$orderby=@p2%20asc,%20@p3%20desc&@p1=fullname&@p2=jobtitle&@p3=annualincome HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 4284

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "value":[
      {
         "@odata.etag":"W/\"619847\"",
         "fullname":"Scott Konersmann (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$38,000.00",
         "annualincome":38000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"2cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619843\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"24c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619841\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"20c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619718\"",
         "fullname":"Yvonne McKay (sample)",
         "jobtitle":"Coffee Master",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
         "annualincome":45000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"15c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619853\"",
         "fullname":"Rene Valdes (sample)",
         "jobtitle":"Data Analyst III",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$86,000.00",
         "annualincome":86000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"38c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619845\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"28c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619855\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"3cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619839\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"1cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619849\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"30c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619851\"",
         "fullname":"Paul Cannon (sample)",
         "jobtitle":"Ski Instructor",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$68,500.00",
         "annualincome":68500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"34c364b2-bf43-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contacts list using parameterized aliases:
    1) Scott Konersmann (sample), Accounts Manager, $38,000.00
    2) Maria Cambell (sample), Accounts Manager, $31,000.00
    3) Nancy Anderson (sample), Activities Manager, $55,500.00
    4) Yvonne McKay (sample), Coffee Master, $45,000.00
    5) Rene Valdes (sample), Data Analyst III, $86,000.00
    6) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    7) Jim Glynn (sample), Senior International Sales Manager, $81,400.00
    8) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    9) Robert Lyon (sample), Senior Technician, $78,000.00
    10) Paul Cannon (sample), Ski Instructor, $68,500.00

限制结果

返回的数据超过需要对性能来说不是好事。 服务器将为每个请求返回最多 5000 个实体。 可以使用 $top 查询选项或通过在请求头中添加 odata.maxpagesize 来限制返回的结果数量。$top 查询选项仅返回结果集中排在前面的实体,并忽略其他的实体。odata.maxpagesize 请求头使用 @odata.nextLink 属性指定每页返回的实体数以获取下一页的结果。 有关 odata.maxpagesize 的详细信息,请参阅有关 分页 的章节,另请参阅 对返回实体数目的限制

最靠前的结果

我们可以应用 $top 查询选项以将基本查询操作限制为 fullname 包含 (sample) 的前两个联系人。 在这种情况下,请求实际生成至少 10 个结果,但是响应中仅返回前 5 个条目。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(fullname,'(sample)')&$top=5 HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Content-Length: 2209

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "value":[
      {
         "@odata.etag":"W/\"619718\"",
         "fullname":"Yvonne McKay (sample)",
         "jobtitle":"Coffee Master",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
         "annualincome":45000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"15c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619839\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"1cc364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619841\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"20c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619843\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"24c364b2-bf43-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"619845\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"28c364b2-bf43-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contacts top 5 results:
    1) Yvonne McKay (sample), Coffee Master, $45,000.00
    2) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    3) Nancy Anderson (sample), Activities Manager, $55,500.00
    4) Maria Cambell (sample), Accounts Manager, $31,000.00
    5) Nancy Anderson (sample), Logistics Specialist, $63,500.00

结果计数

您可以仅获取集合值属性指定的记录计数或筛选器中匹配的实体计数。 获取计数将告知我们结果中可能的实体数。 但是,Dynamics 365 服务器将返回 5000 作为最大计数,即使结果可能更多也不例外。 在此示例中,我们构造 jobtitle 包含 Senior 或 Manager 的筛选器,并且还请求了 $count 个结果。 响应中包含 @odata.count 属性中的计数,以及查询的结果。详细信息:检索实体计数

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(jobtitle,'senior')%20or%20contains(jobtitle,%20'manager')&$count=true HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 2654

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "@odata.count":6,
   "value":[
      {
         "@odata.etag":"W/\"620258\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"bf48fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620260\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"c348fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620262\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"c748fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620266\"",
         "fullname":"Scott Konersmann (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$38,000.00",
         "annualincome":38000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"cf48fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620268\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"d348fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620274\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"df48fdee-c143-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

6 contacts have either 'Manager' or 'Senior' designation in their jobtitle.
Manager or Senior:
    1) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    2) Nancy Anderson (sample), Activities Manager, $55,500.00
    3) Maria Cambell (sample), Accounts Manager, $31,000.00
    4) Scott Konersmann (sample), Accounts Manager, $38,000.00
    5) Robert Lyon (sample), Senior Technician, $78,000.00
    6) Jim Glynn (sample), Senior International Sales Manager, $81,400.00

分页

若要为返回大量实体的查询检索按顺序排列的一小组结果,请使用 odata.maxpagesize,而不是使用 $top。详细信息:指定在一个页面中返回的实体数

在此示例中,我们请求 $count,并将 odata.maxpagesize 设置为 4。 此筛选器匹配 10 个联系人,但是我们一次仅检索 4 个。 我们还使用此计数和最大页大小算出有多少总页数。 此请求返回第一页的结果。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(fullname,'(sample)')&$count=true HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=4, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=4
Content-Length: 2294

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "@odata.count":10,
   "value":[
      {
         "@odata.etag":"W/\"620138\"",
         "fullname":"Yvonne McKay (sample)",
         "jobtitle":"Coffee Master",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
         "annualincome":45000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"b848fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620258\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"bf48fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620260\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"c348fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620262\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"c748fdee-c143-e611-80d5-00155da84802"
      }
   ],
   "@odata.nextLink":"http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(fullname,'(sample)')&$count=true&$skiptoken=%3Ccookie%20pagenumber=%222%22%20pagingcookie=%22%253ccookie%2520page%253d%25221%2522%253e%253ccontactid%2520last%253d%2522%257bC748FDEE-C143-E611-80D5-00155DA84802%257d%2522%2520first%253d%2522%257bB848FDEE-C143-E611-80D5-00155DA84802%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20istracking=%22False%22%20/%3E"
}

控制台输出

Contacts total: 10    Contacts per page: 4.
Page 1 of 3:
    1) Yvonne McKay (sample), Coffee Master, $45,000.00
    2) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    3) Nancy Anderson (sample), Activities Manager, $55,500.00
    4) Maria Cambell (sample), Accounts Manager, $31,000.00

若要检索第 2 页,请使用 GET 请求和 @odata.nextLink 属性的值。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(fullname,'(sample)')&$count=true&$skiptoken=%3Ccookie%20pagenumber=%222%22%20pagingcookie=%22%253ccookie%2520page%253d%25221%2522%253e%253ccontactid%2520last%253d%2522%257bC748FDEE-C143-E611-80D5-00155DA84802%257d%2522%2520first%253d%2522%257bB848FDEE-C143-E611-80D5-00155DA84802%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20istracking=%22False%22%20/%3E HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=4, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=4
Content-Length: 2294

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome)",
   "@odata.count":10,
   "value":[
      {
         "@odata.etag":"W/\"620264\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"cb48fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620266\"",
         "fullname":"Scott Konersmann (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$38,000.00",
         "annualincome":38000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"cf48fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620268\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"d348fdee-c143-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620270\"",
         "fullname":"Paul Cannon (sample)",
         "jobtitle":"Ski Instructor",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$68,500.00",
         "annualincome":68500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"d748fdee-c143-e611-80d5-00155da84802"
      }
   ],
   "@odata.nextLink":"http://cc_WebAPI_ServiceURI/contacts?$select=fullname,jobtitle,annualincome&$filter=contains(fullname,'(sample)')&$count=true&$skiptoken=%3Ccookie%20pagenumber=%223%22%20pagingcookie=%22%253ccookie%2520page%253d%25222%2522%253e%253ccontactid%2520last%253d%2522%257bD748FDEE-C143-E611-80D5-00155DA84802%257d%2522%2520first%253d%2522%257bCB48FDEE-C143-E611-80D5-00155DA84802%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20istracking=%22False%22%20/%3E"
}

控制台输出

Page 2 of 3:
    1) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    2) Scott Konersmann (sample), Accounts Manager, $38,000.00
    3) Robert Lyon (sample), Senior Technician, $78,000.00
    4) Paul Cannon (sample), Ski Instructor, $68,500.00

扩展结果

若要检索有关关联的实体的信息,请在导航属性中使用 $expand 查询选项。详细信息:通过扩展导航属性检索相关实体

扩展单值导航属性

单值导航属性表示多对一关系。 在我们的示例数据中,计数通过 primarycontactid 属性与联系人建立关系。 在此关系中,客户只能有一个主要联系人。 我们可以使用 account EntityType 创建查询,以便获取有关该客户的信息,以及扩展有关其主要联系人的信息。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/accounts(b2546951-c543-e611-80d5-00155da84802)?$select=name&$expand=primarycontactid($select=fullname,jobtitle,annualincome) HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 700

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#accounts(name,primarycontactid,primarycontactid(fullname,jobtitle,annualincome))/$entity",
   "@odata.etag":"W/\"620641\"",
   "name":"Contoso, Ltd. (sample)",
   "accountid":"b2546951-c543-e611-80d5-00155da84802",
   "primarycontactid":{
      "@odata.etag":"W/\"620534\"",
      "fullname":"Yvonne McKay (sample)",
      "jobtitle":"Coffee Master",
      "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
      "annualincome":45000.0000,
      "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
      "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
      "contactid":"b3546951-c543-e611-80d5-00155da84802"
   }
}

控制台输出

Account 'Contoso, Ltd. (sample)' has the following primary contact person:
    Fullname: 'Yvonne McKay (sample)' 
    Jobtitle: 'Coffee Master' 
    Annualincome: '45000'

扩展合作伙伴属性

每个导航属性都有一个相应的“合作伙伴”属性。 一旦建立关联,我们可以通过此关联检索信息。 使用哪个属性取决于查询针对的基本实体。 例如,在上一个操作中,我们创建了一个针对 account EntityType 的查询,并且希望获取有关其主要联系人的更多信息。 方法是通过 primarycontactid 属性。 如果您查找 account EntityType,在 Single-valued navigation properties 部分下,可以看到与 primarycontactid 对应的合作伙伴属性为 contact EntityType 中找到的 account_primary_contact 集合值导航属性。

可以通过编写针对某个联系人的查询,扩展 account_primary_contact 属性以获取有关此联系人是主要联系人的客户的信息。 在示例数据中,Yvonne McKay (sample) 仅为一个客户的主要联系人。 但是,可以将其指定为其他客户的主要联系人。 由于 account_primary_contact 属性具有多对一关系,所以结果作为客户实体阵列返回。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts(b3546951-c543-e611-80d5-00155da84802)?$select=fullname,jobtitle,annualincome&$expand=account_primary_contact($select=name) HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 737

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome,account_primary_contact,account_primary_contact(name))/$entity",
   "@odata.etag":"W/\"620534\"",
   "fullname":"Yvonne McKay (sample)",
   "jobtitle":"Coffee Master",
   "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
   "annualincome":45000.0000,
   "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
   "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
   "contactid":"b3546951-c543-e611-80d5-00155da84802",
   "account_primary_contact":[
      {
         "@odata.etag":"W/\"620919\"",
         "name":"Contoso, Ltd. (sample)",
         "accountid":"b2546951-c543-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contact 'Yvonne McKay (sample)' is the primary contact for the following accounts:
    1) Contoso, Ltd. (sample)

扩展集合值导航属性

集合值导航属性支持一对多或多对多关系。 例如,在我们的示例数据中,计数通过 contact_customer_accounts 属性与多个联系人建立关系。

我们可以使用 account EntityType 创建查询,以便获取有关该客户的信息,并扩展有关其主要联系人的信息。 在这种情况下,Contoso, Ltd. (sample) 通过 contact_customer_accounts 集合值导航属性关联到另外 9 个联系人。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/accounts(86546951-c543-e611-80d5-00155da84802)?$select=name&$expand=contact_customer_accounts($select=fullname,jobtitle,annualincome) HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 4073

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#accounts(name,contact_customer_accounts,contact_customer_accounts(fullname,jobtitle,annualincome))/$entity",
   "@odata.etag":"W/\"620921\"",
   "name":"Contoso, Ltd. (sample)",
   "accountid":"86546951-c543-e611-80d5-00155da84802",
   "contact_customer_accounts":[
      {
         "@odata.etag":"W/\"620847\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"8e546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620849\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"92546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620851\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"96546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620853\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"9a546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620855\"",
         "fullname":"Scott Konersmann (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$38,000.00",
         "annualincome":38000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"9e546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620857\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"a2546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620859\"",
         "fullname":"Paul Cannon (sample)",
         "jobtitle":"Ski Instructor",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$68,500.00",
         "annualincome":68500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"a6546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620861\"",
         "fullname":"Rene Valdes (sample)",
         "jobtitle":"Data Analyst III",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$86,000.00",
         "annualincome":86000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"aa546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620863\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"ae546951-c543-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Account 'Contoso, Ltd. (sample)' has the following contact customers:
    1) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    2) Nancy Anderson (sample), Activities Manager, $55,500.00
    3) Maria Cambell (sample), Accounts Manager, $31,000.00
    4) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    5) Scott Konersmann (sample), Accounts Manager, $38,000.00
    6) Robert Lyon (sample), Senior Technician, $78,000.00
    7) Paul Cannon (sample), Ski Instructor, $68,500.00
    8) Rene Valdes (sample), Data Analyst III, $86,000.00
    9) Jim Glynn (sample), Senior International Sales Manager, $81,400.00

扩展多个导航属性

可以根据查询的需要扩展任意数量的导航属性。 但是,$expand 操作只能深入一个级别。

此示例扩展 account EntityTypeprimarycontactidcontact_customer_accountsAccount_Tasks 导航属性。 此查询返回包含计数和两个集合的信息的响应:一个联系人集合,一个任务集合。 此示例代码将单独处理这些集合。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/accounts(86546951-c543-e611-80d5-00155da84802)?$select=name&$expand=primarycontactid($select=fullname,jobtitle,annualincome),contact_customer_accounts($select=fullname,jobtitle,annualincome),Account_Tasks($select=subject,description) HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Preference-Applied: odata.maxpagesize=10
Content-Length: 5093

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#accounts(name,primarycontactid,contact_customer_accounts,Account_Tasks,primarycontactid(fullname,jobtitle,annualincome),contact_customer_accounts(fullname,jobtitle,annualincome),Account_Tasks(subject,description))/$entity",
   "@odata.etag":"W/\"620921\"",
   "name":"Contoso, Ltd. (sample)",
   "accountid":"86546951-c543-e611-80d5-00155da84802",
   "primarycontactid":{
      "@odata.etag":"W/\"620726\"",
      "fullname":"Yvonne McKay (sample)",
      "jobtitle":"Coffee Master",
      "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
      "annualincome":45000.0000,
      "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
      "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
      "contactid":"87546951-c543-e611-80d5-00155da84802"
   },
   "contact_customer_accounts":[
      {
         "@odata.etag":"W/\"620847\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"8e546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620849\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"92546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620851\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"96546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620853\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"9a546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620855\"",
         "fullname":"Scott Konersmann (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$38,000.00",
         "annualincome":38000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"9e546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620857\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"a2546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620859\"",
         "fullname":"Paul Cannon (sample)",
         "jobtitle":"Ski Instructor",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$68,500.00",
         "annualincome":68500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"a6546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620861\"",
         "fullname":"Rene Valdes (sample)",
         "jobtitle":"Data Analyst III",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$86,000.00",
         "annualincome":86000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"aa546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620863\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"ae546951-c543-e611-80d5-00155da84802"
      }
   ],
   "Account_Tasks":[
      {
         "@odata.etag":"W/\"620840\"",
         "subject":"Task 1",
         "description":"Task 1 description",
         "activityid":"8b546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620842\"",
         "subject":"Task 2",
         "description":"Task 2 description",
         "activityid":"8c546951-c543-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"620844\"",
         "subject":"Task 3",
         "description":"Task 3 description",
         "activityid":"8d546951-c543-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

-- Expanding multiple property types in one request -- 
Account 'Contoso, Ltd. (sample)' has the following primary contact person:
    Fullname: 'Yvonne McKay (sample)' 
    Jobtitle: 'Coffee Master' 
    Annualincome: '45000'
Account 'Contoso, Ltd. (sample)' has the following related contacts:
    1) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    2) Nancy Anderson (sample), Activities Manager, $55,500.00
    3) Maria Cambell (sample), Accounts Manager, $31,000.00
    4) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    5) Scott Konersmann (sample), Accounts Manager, $38,000.00
    6) Robert Lyon (sample), Senior Technician, $78,000.00
    7) Paul Cannon (sample), Ski Instructor, $68,500.00
    8) Rene Valdes (sample), Data Analyst III, $86,000.00
    9) Jim Glynn (sample), Senior International Sales Manager, $81,400.00
Account 'Contoso, Ltd. (sample)' has the following tasks:
    1) Task 1, Task 1 description
    2) Task 2, Task 2 description
    3) Task 3, Task 3 description

FetchXML 查询

除了查询筛选器操作,Web API 也支持 FetchXML 查询。 FetchXml 还提供了备用方法,用于定义查询和其他选项来执行聚合。详细信息:使用 FetchXML 构建查询

若要使用 fetch xml,必须编写表示此查询的字符串。 确保查询字符串符合 FetchXML schema。 必须先对该字符串执行 URL 编码,才能将其添加到 URL 中。

我们通常定义的所有查询选项定义(如 $select$filter$orderby)现在通过 XML 定义。 在此操作中,我们查询 fullname 匹配 (sample) 的所有联系人,并将结果按 fullname 排序。 这是此查询的 XML。

<fetch mapping="logical" output-format="xml-platform" version="1.0" distinct="false">
  <entity name="contact">
    <attribute name="fullname" />
    <attribute name="jobtitle" />
    <attribute name="annualincome" />
    <order descending="true"
           attribute="fullname" />
    <filter type="and">
      <condition value="%(sample)%"
                 attribute="fullname"
                 operator="like" />
    </filter>
  </entity>
</fetch>

HTTP 请求

请求查询字符串以编码格式发送到服务器。 编码的头如下所示。

GET http://cc_WebAPI_ServiceURI/contacts?fetchXml=%253Cfetch%2520mapping%253D%2522logical%2522%2520output-format%253D%2522xml-platform%2522%2520version%253D%25221.0%2522%2520distinct%253D%2522false%2522%253E%2520%2520%2520%253Centity%2520name%253D%2522contact%2522%253E%2520%2520%2520%2520%2520%253Cattribute%2520name%253D%2522fullname%2522%2520%252F%253E%2520%2520%2520%2520%2520%253Cattribute%2520name%253D%2522jobtitle%2522%2520%252F%253E%2520%2520%2520%2520%2520%253Cattribute%2520name%253D%2522annualincome%2522%2520%252F%253E%2520%2520%2520%2520%2520%253Corder%2520descending%253D%2522true%2522%2520attribute%253D%2522fullname%2522%2520%252F%253E%2520%2520%2520%2520%2520%253Cfilter%2520type%253D%2522and%2522%253E%2520%2520%2520%2520%2520%2520%2520%253Ccondition%2520value%253D%2522%2525(sample)%2525%2522%2520attribute%253D%2522fullname%2522%2520operator%253D%2522like%2522%2520%252F%253E%2520%2520%2520%2520%2520%253C%252Ffilter%253E%2520%2520%2520%253C%252Fentity%253E%2520%253C%252Ffetch%253E%2520 HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Content-Length: 4345

{
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome,_transactioncurrencyid_value,transactioncurrencyid,contactid)",
   "value":[
      {
         "@odata.etag":"W/\"621502\"",
         "fullname":"Yvonne McKay (sample)",
         "jobtitle":"Coffee Master",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$45,000.00",
         "annualincome":45000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"9255b257-c843-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"621627\"",
         "fullname":"Susanna Stubberod (sample)",
         "jobtitle":"Senior Purchaser",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$52,000.00",
         "annualincome":52000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"9955b257-c843-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"621635\"",
         "fullname":"Scott Konersmann (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$38,000.00",
         "annualincome":38000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"a955b257-c843-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"621637\"",
         "fullname":"Robert Lyon (sample)",
         "jobtitle":"Senior Technician",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$78,000.00",
         "annualincome":78000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"ad55b257-c843-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"621641\"",
         "fullname":"Rene Valdes (sample)",
         "jobtitle":"Data Analyst III",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$86,000.00",
         "annualincome":86000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"b555b257-c843-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"621639\"",
         "fullname":"Paul Cannon (sample)",
         "jobtitle":"Ski Instructor",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$68,500.00",
         "annualincome":68500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"b155b257-c843-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"621629\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"9d55b257-c843-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"621633\"",
         "fullname":"Nancy Anderson (sample)",
         "jobtitle":"Logistics Specialist",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$63,500.00",
         "annualincome":63500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"a555b257-c843-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"621631\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"a155b257-c843-e611-80d5-00155da84802"
      },
      {
         "@odata.etag":"W/\"621643\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"b955b257-c843-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contacts Fetched by fullname containing '(sample)':
    1) Yvonne McKay (sample), Coffee Master, $45,000.00
    2) Susanna Stubberod (sample), Senior Purchaser, $52,000.00
    3) Scott Konersmann (sample), Accounts Manager, $38,000.00
    4) Robert Lyon (sample), Senior Technician, $78,000.00
    5) Rene Valdes (sample), Data Analyst III, $86,000.00
    6) Paul Cannon (sample), Ski Instructor, $68,500.00
    7) Nancy Anderson (sample), Activities Manager, $55,500.00
    8) Nancy Anderson (sample), Logistics Specialist, $63,500.00
    9) Maria Cambell (sample), Accounts Manager, $31,000.00
    10) Jim Glynn (sample), Senior International Sales Manager, $81,400.00

FetchXML 分页

FetchXML 处理分页的方式与查询筛选器处理分页的方式不同。 在 FetchXML 中,您可以指定一个 count 属性,用于指示每页返回多少结果。 在相同请求中,使用 page 属性指定所需页数。 此操作将为上一个 FetchXML 示例返回的第 3 页创建一个请求。 根据我们的示例数据,结果中应该有 10 个联系人。 将每页拆分为每页仅四个实体,这样应该有三页。 第 3 页应仅有两个实体。 如果我们请求第 4 页,将不返回结果。

<fetch mapping="logical"
       output-format="xml-platform"
       version="1.0"
       distinct="false"
       page="3"
       count="4">
  <entity name="contact">
    <attribute name="fullname" />
    <attribute name="jobtitle" />
    <attribute name="annualincome" />
    <order descending="true"
           attribute="fullname" />
    <filter type="and">
      <condition value="%(sample)%"
                 attribute="fullname"
                 operator="like" />
    </filter>
  </entity>
</fetch>

HTTP 请求

请求查询字符串以编码格式发送到服务器。 编码的头如下所示。

GET http://cc_WebAPI_ServiceURI/contacts?fetchXml=%253Cfetch%2520mapping%253D%2522logical%2522%2520output-format%253D%2522xml-platform%2522%2520version%253D%25221.0%2522%2520distinct%253D%2522false%2522%2520page%253D%25223%2522%2520count%253D%25224%2522%253E%2520%2520%2520%253Centity%2520name%253D%2522contact%2522%253E%2520%2520%2520%2520%2520%253Cattribute%2520name%253D%2522fullname%2522%2520%252F%253E%2520%2520%2520%2520%2520%253Cattribute%2520name%253D%2522jobtitle%2522%2520%252F%253E%2520%2520%2520%2520%2520%253Cattribute%2520name%253D%2522annualincome%2522%2520%252F%253E%2520%2520%2520%2520%2520%253Corder%2520descending%253D%2522true%2522%2520attribute%253D%2522fullname%2522%2520%252F%253E%2520%2520%2520%2520%2520%253Cfilter%2520type%253D%2522and%2522%253E%2520%2520%2520%2520%2520%2520%2520%253Ccondition%2520value%253D%2522%2525(sample)%2525%2522%2520attribute%253D%2522fullname%2522%2520operator%253D%2522like%2522%2520%252F%253E%2520%2520%2520%2520%2520%253C%252Ffilter%253E%2520%2520%2520%253C%252Fentity%253E%2520%253C%252Ffetch%253E%2520 HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Content-Length: 1037

{ 
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,jobtitle,annualincome,_transactioncurrencyid_value,transactioncurrencyid,contactid)",
   "value":[ 
      { 
         "@odata.etag":"W/\"621631\"",
         "fullname":"Maria Cambell (sample)",
         "jobtitle":"Accounts Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$31,000.00",
         "annualincome":31000.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"a155b257-c843-e611-80d5-00155da84802"
      },
      { 
         "@odata.etag":"W/\"621643\"",
         "fullname":"Jim Glynn (sample)",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802",
         "contactid":"b955b257-c843-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

Contacts Fetched by fullname containing '(sample)' - Page 3:
    1) Maria Cambell (sample), Accounts Manager, $31,000.00
    2) Jim Glynn (sample), Senior International Sales Manager, $81,400.00

预定义的查询

可以使用 Web API 执行预定义的查询。详细信息:检索和执行预定义的查询

已保存查询

在此操作中,我们将为名称为“有效客户”的已保存查询的 savedqueryid GUID 创建请求。 然后使用此 GUID 和 savedQuery 参数查询有效客户列表。

获取已保存查询的 GUID。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/savedqueries?$select=name,savedqueryid&$filter=name%20eq%20'Active%20Accounts' HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue
Referer: https://localhost:1469/WebAPIQuery.html

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Content-Length: 251

{ 
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#savedqueries(name,savedqueryid)",
   "value":[ 
      { 
         "@odata.etag":"W/\"443067\"",
         "name":"Active Accounts",
         "savedqueryid":"00000000-0000-0000-00aa-000010001002"
      }
   ]
}

使用 savedQuery 参数获取保存的查询的内容

HTTP 请求

GET http://cc_WebAPI_ServiceURI/accounts?savedQuery=00000000-0000-0000-00aa-000010001002 HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
REQ_ID: 2bc532c4-d445-44cd-adae-1909a616d6bc
OData-Version: 4.0
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"
Content-Length: 446

{ 
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#accounts(name,_primarycontactid_value,primarycontactid,accountid)",
   "value":[ 
      { 
         "@odata.etag":"W/\"621613\"",
         "name":"Contoso, Ltd. (sample)",
         "_primarycontactid_value@OData.Community.Display.V1.FormattedValue":"Yvonne McKay (sample)",
         "_primarycontactid_value":"9255b257-c843-e611-80d5-00155da84802",
         "accountid":"9155b257-c843-e611-80d5-00155da84802"
      }
   ]
}

控制台输出

-- Saved Query -- 
Saved Query (Active Accounts):
    1) Contoso, Ltd. (sample)

用户查询

此示例创建一个用户查询,执行该查询,然后将其从系统中删除。 此用户查询请求其 fullname 包含 (sample),jobtitle 包含 manager 且 annualincome 超过 55000 的所有联系人。 我们的示例数据有两个匹配此查询的联系人。

获取用户查询的 GUID。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/userqueries?$select=name,userqueryid,&$filter=name%20eq%20'My%20User%20Query' HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Referer: https://localhost:1469/WebAPIQuery.html

HTTP 响应

Pragma: no-cache
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Content-Length: 246

{ 
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#userqueries(name,userqueryid)",
   "value":[ 
      { 
         "@odata.etag":"W/\"621698\"",
         "name":"My User Query",
         "userqueryid":"7ec390ab-c943-e611-80d5-00155da84802"
      }
   ]
}

获取用户查询的内容将使用 userQuery 参数传递 GUID 值。

HTTP 请求

GET http://cc_WebAPI_ServiceURI/contacts?userQuery=7ec390ab-c943-e611-80d5-00155da84802 HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Prefer: odata.maxpagesize=10, odata.include-annotations=OData.Community.Display.V1.FormattedValue

HTTP 响应

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
OData-Version: 4.0
Content-Length: 1040

{ 
   "@odata.context":"http://cc_WebAPI_ServiceURI/$metadata#contacts(fullname,contactid,jobtitle,annualincome,_transactioncurrencyid_value,transactioncurrencyid)",
   "value":[ 
      { 
         "@odata.etag":"W/\"621643\"",
         "fullname":"Jim Glynn (sample)",
         "contactid":"b955b257-c843-e611-80d5-00155da84802",
         "jobtitle":"Senior International Sales Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$81,400.00",
         "annualincome":81400.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802"
      },
      { 
         "@odata.etag":"W/\"621629\"",
         "fullname":"Nancy Anderson (sample)",
         "contactid":"9d55b257-c843-e611-80d5-00155da84802",
         "jobtitle":"Activities Manager",
         "annualincome@OData.Community.Display.V1.FormattedValue":"$55,500.00",
         "annualincome":55500.0000,
         "_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue":"US Dollar",
         "_transactioncurrencyid_value":"518c78c9-d3f6-e511-80d0-00155da84802"
      }
   ]
}

控制台输出

-- User Query -- 
Saved User Query:
    1) Jim Glynn (sample), Senior International Sales Manager, $81,400.00
    2) Nancy Anderson (sample), Activities Manager, $55,500.00

另请参阅

使用 Microsoft Dynamics 365 Web API
使用 Web API 查询数据
检索和执行预定义的查询
Web API 查询数据示例 (C#)
Web API 查询数据示例(客户端 JavaScript)

Microsoft Dynamics 365

© 2017 Microsoft。 保留所有权利。 版权