Condividi tramite


Esempio di dati di query API Web

 

Data di pubblicazione: gennaio 2017

Si applica a: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

Il gruppo di esempi illustra come eseguire query dei dati utilizzando l'API Web di Microsoft Dynamics 365. Questo esempio è implementato come un progetto distinto per le lingue seguenti:

In questo argomento viene descritto un set comune di operazioni implementato da ogni esempio in questo gruppo. In questo argomento vengono descritte le richieste HTTP, le risposte e l'output di testo che ogni esempio di questo gruppo eseguirà senza dettagli specifici della lingua. Vedere le descrizioni specifiche della lingua e gli esempi individuali per informazioni su come queste operazioni vengono eseguite.

Dimostra

Questo esempio è suddiviso nelle sezioni principali seguenti, contenenti operazioni dati di query dell'API Web che vengono descritte in maggiore dettaglio negli argomenti concettuali associati.

Sezione dell'argomento

Argomenti associati

Selezionare le proprietà specifiche

Recupera proprietà specifiche

Includere valori formattati

Utilizzo delle funzioni di query

Filtrare i risultati

Funzionalità di query standard

Comporre una query con le funzioni

Web API Query Function Reference

Utilizzo degli operatori

Operatori standard del filtro

Precedenza delle impostazioni

Operatori standard del filtro

Ordinare i risultati

Ordinare i risultati

Filtrare i risultati

Alias di parametro

Usare gli alias di parametro con le opzioni di query di sistema

Limitare i risultati

Limitare i risultati

Limita il numero di entità restituite

Espandere i risultati

Recupero entità correlate espandendo le proprietà di navigazione

query FetchXML

FetchXML schema

Set di risultati di grandi dimensioni della pagina con FetchXML

Utilizza FetchXML personalizzato

Query predefinite

Recupera e esegui le query predefinite

userquery EntityType

savedquery EntityType

Le sezioni seguenti contengono una breve discussione delle operazioni dell'API Web di Dynamics 365 eseguite insieme ai messaggi HTTP corrispondenti e all'output associato alla console.

Dati di esempio

Per garantire il corretto funzionamento delle query di questo esempio, un insieme predefinito di record di esempio viene creato dall'esempio. I record di esempio verranno eliminati a meno che l'utente scelga di non eliminarli. Si tratta dei dati per cui l'esempio eseguirà la query. È possibile ottenere risultati diversi in base a tutti i dati esistenti nell'ambiente.

I dati vengono aggiunti tramite inserimento deep in un'unica richiesta POST e corrispondono alla struttura seguente:

    {
           "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" }
                   ]
               }
           ]
       }

Selezionare le proprietà specifiche

Creare sempre le query utilizzando un'opzione di query $select, altrimenti il server restituirà tutte le proprietà di ogni entità che limita le prestazioni. In questo esempio viene illustrato come creare una query di base selezionando tre proprietà di un contact EntityType Le proprietà sono fullname, jobtitle, annualincome. Nella sezione vengono illustrate anche le differenze tra i valori formattati e non formattati come visualizzati nei risultati della proprietà annualincome del contatto.Ulteriori informazioni:Proprietà specifiche della richiesta, Includere valori formattati.

In questo esempio, verrà richiesto un contatto specifico. In questo caso, si tratta del contatto primario dell'account Yvonne McKay (sample).

Richiesta 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

Risposta 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"
}

Output della console

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

Utilizzo delle funzioni di query

Utilizzare le opzioni di filtro per impostare i criteri per i risultati desiderati. È possibile creare filtri da semplici a complessi utilizzando una combinazione di funzioni di query, operatori di confronto e operatori logici.Ulteriori informazioni:Filtrare i risultati.

Le funzioni di query sono funzioni che possono essere utilizzate come criterio di filtro in una query. Sono disponibili funzioni di query standard e funzioni di query specifiche di Dynamics 365. Le funzioni accettano i parametri e restituiscono un valore Boolean. In questo esempio viene illustrato come creare una query per ogni tipo.

Funzionalità di query standard

Dynamics 365 supporta un piccolo sottoinsieme delle funzioni di query incorporate di OData: contains, endswith e startswith. Ad esempio, la funzione di query standard contains consente di filtrare le proprietà che corrispondono a una stringa. In questa operazione, verrà effettuata una query per tutti i contatti con fullname contenente la stringa (sample).Ulteriori informazioni:Funzionalità di query standard.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Funzioni di query di Dynamics 365

Le funzioni di query di Dynamics 365 offrono un numero elevato di opzioni per creare query appropriate per Dynamics 365. Per un elenco completo di queste funzioni, vedere Web API Query Function Reference.Ulteriori informazioni:Comporre una query con le funzioni

Queste funzioni di query verranno utilizzate in modo analogo alle funzioni di query standard. La differenza principale quando si utilizzano le query Dynamics 365 consiste nel fatto che è necessario indicare il nome completo della funzione inclusi i nomi dei parametri. Ad esempio, per ottenere l'elenco dei contatti creati nell'ultima ora, è necessario creare una query utilizzando la LastXHours Function.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Utilizzo degli operatori

Per perfezionare i risultati, è possibile utilizzare Operatori standard del filtro (eq,ne,gt,ge,lt,le,and,or,not). In questo esempio, è necessario ottenere un elenco di tutti i contatti con fullname contenente (sample) e ricavi annuali maggiori di 55000.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Precedenza delle impostazioni

Verranno utilizzate le parentesi per definire l'ordine in cui le condizioni verranno valutate.

In questo esempio, è necessario ottenere un elenco di tutti i contatti con fullname contenente (sample), jobtitle contenente senior o specialist e con annualincome maggiori di 55000. Per ottenere i risultati desiderati, vengono utilizzate le parentesi per raggruppare i filtri jobtitle. Poiché tutti gli operatori hanno la stessa precedenza, omettendo le parentesi attribuiremo all'operatore or la stessa precedenza degli operatori and. I filtri vengono applicati da destra a sinistra. L'ordine in cui le istruzioni vengono visualizzate nel filtro può influire sui risultati. La query di questo esempio sarà come segue: $filter=contains(fullname,'(sample)') and (contains(jobtitle,'senior') or contains(jobtitle,'specialist')) and annualincome gt 55000.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Ordinare i risultati

È possibile specificare un ordine crescente o decrescente per i risultati utilizzando l'opzione di filtro $orderby. In questo esempio, verrà eseguita una query per tutti i contatti con fullname contenente (sample) e verranno richiesti i dati in ordine crescente base al valore di una proprietà jobtitle e quindi in ordine decrescente in base al valore della proprietà annualincome utilizzando questa sintassi: $orderby=jobtitle asc, annualincome desc.Ulteriori informazioni:Ordinare i risultati.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Alias di parametro

Utilizzare gli alias dei parametri per semplificare il riutilizzo dei parametri nei filtri. Gli alias con parametri possono essere utilizzati nelle opzioni $filter e $orderby. Se l'alias non è assegnato a un valore, si presuppone essere null. È anche possibile usare gli alias di parametro per la chiamata delle funzioni.Ulteriori informazioni:Utilizzare le funzioni API Web, Usare gli alias di parametro con le opzioni di query di sistema. Prendendo in considerazione l'operazione dei risultati di ordinamento ad esempio, è possibile scrivere di nuovo la query utilizzando i parametri e ottenendo gli stessi risultati di output.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Limitare i risultati

La restituzione di una quantità di dati superiore al necessario ha un effetto negativo sulle prestazioni. Il server restituirà un massimo di 5000 entità per richiesta. È possibile limitare il numero di risultati restituiti utilizzando l'opzione di query $top o aggiungendo odata.maxpagesize nell'intestazione della richiesta. L'opzione di query $top restituisce solo il numero massimo di entità del set di risultati e ignora le restanti colonne. L'intestazione di richiesta odata.maxpagesize specifica il numero di entità da restituire per pagina con una proprietà @odata.nextLink** per ottenere i risultati della pagina successiva. Per ulteriori informazioni su **odata.maxpagesize, vedere la sezione su Impaginazione e Limita il numero di entità restituite.

Risultati principali

È possibile applicare l'opzione di query $top per limitare l'operazione di query di base ai primi cinque contatti con fullname contenente (sample). In questo caso, la richiesta produrrà almeno 10 risultati, ma solo le prime 5 voci verranno restituite nella risposta.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Conteggio risultati

È possibile visualizzare solo il numero di record in base a una proprietà raccolta o un conteggio delle entità corrispondenti in un filtro. L'utilizzo del conteggio indica il numero di entità possibili nei risultati. Tuttavia, Dynamics 365 il server restituirà 5000 come conteggio massimo anche se il risultato potrebbe essere più elevato. In questo esempio, è stato creato un filtro con jobtitle contenente Senior o Manager ed è stato inoltre richiesto un $count del risultato. La risposta contiene il conteggio nella proprietà @odata.count nonché i risultati della query.Ulteriori informazioni:Recuperare un conteggio di entità.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Impaginazione

Per recuperare un sottoinsieme sequenziale dei risultati per una query che restituisce un numero elevato entità, utilizzare odata.maxpagesize anziché $top.Ulteriori informazioni:Specifica il numero di entità da restituire in una pagina.

In questo esempio, si chiede un $count e si imposta odata.maxpagesize su 4. Il filtro corrisponde a 10 contatti, ma ne verranno recuperati solo 4 alla volta. Viene anche utilizzato il conteggio e la dimensione massima di pagina per determinare quante pagine totali si otterranno. Il risultato della prima pagina viene restituito in questa richiesta.

Richiesta 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

Risposta 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"
}

Output della console

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

Per recuperare la pagina 2, utilizzare una richiesta GET con il valore della proprietà @odata.nextLink.

Richiesta 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

Risposta 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"
}

Output della console

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

Espandere i risultati

Per recuperare le informazioni sulle entità associate, utilizzare l'opzione di query $expand nelle proprietà di navigazione.Ulteriori informazioni:Recupero entità correlate espandendo le proprietà di navigazione.

Espandere in base a una proprietà di navigazione a valore singolo

Una proprietà di navigazione a valore singolo rappresenta una relazione molti-a-uno. Nei dati di esempio, l'account presenta una relazione con un contatto tramite l'attributo primarycontactid. In questa relazione, l'account può avere un solo contatto primario. Utilizzando account EntityType, è possibile creare una query per ottenere informazioni sull'account ed espandere le informazioni sul contatto primario.

Richiesta 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

Risposta 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"
   }
}

Output della console

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

Espandere in base alla proprietà partner

Ogni proprietà di navigazione include una proprietà "partner" corrispondente. Dopo aver eseguito un'associazione, è possibile recuperare le informazioni tramite tale associazione. Quale attributo viene utilizzato dipende dall'entità di base per cui viene eseguita la query. Ad esempio, nell'operazione precedente, abbiamo creato una query per account EntityType per ottenere ulteriori informazioni sul contatto primario. Questa operazione è stata effettuata tramite l'attributo primarycontactid. Se si cerca account EntityType nella sezione Single-valued navigation properties, è possibile vedere che la proprietà partner che corrisponde a primarycontactid è la proprietà di navigazione con valori di raccolta account_primary_contact trovata in contact EntityType.

Scrivendo una query per un contatto, è possibile espandere in base all'attributo account_primary_contact per ottenere informazioni sugli account in cui il contatto è il contatto primario. Nei dati di esempio, Yvonne McKay (sample) è la persona di contatto primario per un solo account. Tuttavia, potenzialmente può essere assegnata ad altri account come contatto primario. Poiché la proprietà account_primary_contact presenta una relazione molti-a-uno, il risultato viene restituito come un array di entità account.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Espandere in base a una proprietà di navigazione con i valori della raccolta

Le proprietà di navigazione con valori della raccolta supportano relazioni uno-a-molti o molti-a-molti. Ad esempio, nei dati di esempio, l'account presenta una relazione con molti contatti tramite l'attributo contact_customer_accounts.

Utilizzando account EntityType, è possibile creare una query per ottenere informazioni sull'account ed espandere le informazioni sui relativi contatti. In questo caso, Contoso, Ltd. (sample) è associato a nove altri contatti tramite la proprietà di navigazione con i valori della raccolta contact_customer_accounts.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Espandere in base a più proprietà di navigazione

È possibile espandere in base a tutte le proprietà di navigazione necessarie per la query. Tuttavia, l'opzione $expand potrà disporre solo di profondità di un livello.

In questo esempio, vengono estese le proprietà di navigazione primarycontactid, contact_customer_accounts e Account_Tasks di account EntityType. La query restituisce una risposta contenente le informazioni sull'account e due raccolte: una raccolta di contatti e una raccolta di attività. L'esempio di codice elabora queste raccolte separatamente.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

-- 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

query FetchXML

Oltre alle operazioni del filtro query, l'API Web supporta anche query FetchXML. FetchXml offre un metodo alternativo per definire le query e opzioni aggiuntive per eseguire le aggregazioni.Ulteriori informazioni:Creare query con FetchXML

Per utilizzare FetchXML è necessario creare una stringa che rappresenta la query. Verificare che la stringa di query sia conforme allo FetchXML schema. Prima di includere la stringa nell'URL è necessario codificare con URL la stringa.

Tutte le opzioni di query che in genere definiremmo, come $select, $filter e $orderby, vengono in questo caso definite nell'XML. In questa operazione, verrà eseguita una query per tutti i contatti il cui fullname corrisponde a (sample) e verranno ordinati i risultati ottenuti in ordine decrescente in base al fullname. Questo è il codice XML per la query.

<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>

Richiesta HTTP

La stringa di query di richiesta viene inviata al server in forma codificata. L'intestazione codificata è analoga alla seguente.

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

Risposta 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"
      }
   ]
}

Output della console

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

Impaginazioni FetchXML

Il modo in cui FetchXML gestisce l'impaginazione è diverso del modo in cui il filtro lo gestisce. In FetchXML, è possibile specificare un attributo count che indicherà il numero di risultati da restituire per ogni pagina. Nella stessa richiesta, si utilizza l'attributo page per specificare il numero di pagine desiderato. Questa operazione eseguirà una richiesta per la pagina 3 dell'esempio FetchXML precedente. A seconda dei dati di esempio, dovremmo ottenere 10 contatti nei risultati. Scomponendo ogni pagina solo a quattro entità per pagina, dovremmo ottenere tre pagine. La pagina 3 deve contenere solo due entità. Se quindi chiediamo la pagina 4, il sistema restituirà zero risultati.

<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>

Richiesta HTTP

La stringa di query di richiesta viene inviata al server in forma codificata. L'intestazione codificata è analoga alla seguente.

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

Risposta 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"
      }
   ]
}

Output della console

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

Query predefinite

È possibile utilizzare l'API Web per eseguire query predefinite.Ulteriori informazioni:Recupera e esegui le query predefinite.

Query salvata

In questa operazione, faremo una richiesta per il GUID savedqueryid della query salvata con nome Account attivi. In utilizzando il GUID e il parametro savedQuery, eseguiremo una query per ottenere un elenco di account attivi.

Ottenere il GUID della query salvata.

Richiesta 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

Risposta 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"
      }
   ]
}

Ottenere il contenuto della query salvata utilizzando il parametro savedQuery

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

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

Query utente

In questo esempio viene creata una query utente, viene eseguita e quindi eliminata dal sistema. La query utente richiede tutti i contatti il cui fullname contiene (sample), jobtitle contiene manager e con annualincome maggiore di 55000. Nei dati di esempio sono disponibili due contatti che soddisfano la query.

Ottenere il GUID della query utente.

Richiesta 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

Risposta 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"
      }
   ]
}

Ottenere il contenuto della query utente passando il valore GUID con il parametro userQuery.

Richiesta 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

Risposta 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"
      }
   ]
}

Output della console

-- 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

Vedere anche

Utilizzare l'API Web di Microsoft Dynamics 365
Query di dati tramite l'API Web
Recupera e esegui le query predefinite
Esempio di dati di query API Web (C#)
Esempio di dati di query API Web (JavaScript lato client)

Microsoft Dynamics 365

© 2017 Microsoft. Tutti i diritti sono riservati. Copyright