Usar la API de búsqueda de Microsoft para buscar contenido en OneDrive y SharePointUse the Microsoft Search API to search content in OneDrive and SharePoint

Use la API de búsqueda de Microsoft para buscar contenido almacenado en OneDrive o SharePoint: archivos, carpetas, listas, elementos de lista o sitios.Use the Microsoft Search API to search content stored in OneDrive or SharePoint: files, folders, lists, list items, or sites.

Precaución

El esquema de la API de búsqueda ha cambiado con algunas propiedades en una solicitud de búsqueda y se ha quitado el nombre y la respuesta.The search API schema has changed with some properties in a search request and response renamed or removed. Vea más detalles sobre el desuso.See more details about the deprecation. Los ejemplos de este tema muestran el esquema que se va a usar en el futuro.Examples in this topic show the schema to use going forward.

La API de búsqueda le permite definir el ámbito de los tipos de contenido que se van a recuperar en OneDrive o SharePoint especificando la propiedad entityTypes en el searchRequest.The Search API lets you scope the types of content to retrieve in OneDrive or SharePoint by specifying the entityTypes property on the searchRequest. En la parte posterior de este artículo se muestran algunos ejemplos:The later part of this article shows a few examples:

Especificar propiedades de selecciónSpecify select properties

Puede especificar los campos que desea volver a la respuesta, como parte de la subpropiedad Fields de un objeto searchHit en la respuesta.You can specify the fields you want back in the response, as part of the fields sub-property of a searchHit object in the response. Esta es una forma de recortar la respuesta a través del hilo o solicitar algunas propiedades específicas que no formen parte del esquema de presentación.This is a way to either trim down the response over the wire, or to request some specific properties that are not part of the out-of-the-box schema.

Tenga en cuenta que la selección de propiedades solo está disponible para ListItem , ya que es la única entidad de SharePoint en Microsoft Graph que admite propiedades personalizadas.Note that property selection is only available for listItem since this is the only SharePoint entity in Microsoft Graph that supports custom properties.

Para recuperar una propiedad personalizada de un driveItem, consulte ListItem en su lugar.To retrieve a custom property for a driveItem, query listItem instead.

SolicitudRequest

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "listItem"
      ],
      "query": {
        "queryString": "contoso"
      },
      "fields": [
          "title",
          "contentclass"
      ]
    }
  ]
}

RespuestaResponse

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "contoso.sharepoint.com,6598ee0b-0f5f-4416-a0ae-66d864efb43a,60024ce8-e74d-4d63-a939-ad00cd738670",
              "rank": 1,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.listItem",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/contoso-designs.docx",
                "parentReference": {
                  "siteId": "m365x231305.sharepoint.com,5724d91f-650c-4810-83cc-61a8818917d6,c3ba25dc-2c9f-48cb-83be-74cdf68ea5a0"
                },
                "fields": {
                  "contentclass": "STS_ListItem_GenericList",
                  "title": "Contoso issue "
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

Usar filtros en las consultas de búsquedaUse filters in search queries

Puede usar KQL en términos de búsqueda de consultas para OneDrive y SharePoint.You can use KQL in search terms of queries for OneDrive and SharePoint. Por ejemplo:For example:

  • "query": "contoso filetype:docx OR filetype:doc" asigna el ámbito de la consulta a los documentos de Word."query": "contoso filetype:docx OR filetype:doc" scopes the query to Word documents.
  • "query": "test path:\"https://contoso.sharepoint.com/sites/Team Site/Documents/Project\\"" asigna el ámbito de la consulta a una carpeta concreta dentro de un sitio."query": "test path:\"https://contoso.sharepoint.com/sites/Team Site/Documents/Project\\"" scopes the query to a particular folder within a site.
  • "query": "contoso AND isDocument=true" limita la consulta para que solo devuelva documentos."query": "contoso AND isDocument=true" scopes the query to only return documents. No se devolverá ningún contenedor (carpeta, biblioteca de documentos).Any container (folder, document library) will not be returned.
  • "query": "contoso contentclass:STS_List_Events" limita la consulta a los eventos de calendario almacenados en SharePoint."query": "contoso contentclass:STS_List_Events" scopes the query to Calendar events stored in SharePoint.

Para que sea válido, la restricción de propiedades debe especificar un nombre de propiedad administrada que se puede consultar válido en la condición.In order to be valid, properties restriction should specify a valid, queryable managed property name in the condition.

Ejemplo 1: buscar archivosExample 1: Search files

SolicitudRequest

POST https://graph.microsoft.com/beta/search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "driveItem"
      ],
      "query": {
        "queryString": "contoso"
      }
    }
  ]
}

RespuestaResponse

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "FlULeN/ui/1GjLx1rUfio5UAAEl",
              "rank": 1,
              "summary": "<c0>Contoso</c0> Detailed Design <ddd/>",
              "resource": {
                "@odata.type": "#microsoft.graph.driveItem",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "lastModifiedDateTime": "2019-06-10T06:37:43Z",
                "name": "web_part_test_long Notebook",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/contoso-designs.docx",
                "createdBy": {
                 "user": {
                   "displayName": "Michaelvincent Santos;Provisioning User"
                  }
                },
                "lastModifiedBy": {
                  "user": {
                    "displayName": "Richard Mayer"
                  }
                },
                "parentReference": {
                  "siteId": "m365x231305.sharepoint.com,5724d91f-650c-4810-83cc-61a8818917d6,c3ba25dc-2c9f-48cb-83be-74cdf68ea5a0"
                }
                "fileSystemInfo": {
                  "createdDateTime": "2019-06-10T06:37:43Z",
                  "lastModifiedDateTime": "2019-06-10T06:37:43Z"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

Ejemplo 2: buscar elementos de listaExample 2: Search list items

SolicitudRequest

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "listItem"
      ],
      "query": {
        "queryString": "contoso"
      }
    }
  ]
}

RespuestaResponse

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "FlULeN/ui/1GjLx1rUfio5UAAEl",
              "rank": 1,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.listItem",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "lastModifiedDateTime": "2019-06-10T06:37:43Z",
                "name": "web_part_test_long Notebook",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/Lists/Issue tracker list/DispForm.aspx?ID=1"
                "createdBy": {
                 "user": {
                   "displayName": "Michaelvincent Santos;Provisioning User"
                  }
                },
                "lastModifiedBy": {
                  "user": {
                    "displayName": "Richard Mayer"
                  }
                },
                "parentReference": {
                  "siteId": "m365x231305.sharepoint.com,5724d91f-650c-4810-83cc-61a8818917d6,c3ba25dc-2c9f-48cb-83be-74cdf68ea5a0"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

Ejemplo 3: buscar sitiosExample 3: Search sites

SolicitudRequest

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "site"
      ],
      "query": {
        "queryString": "contoso"
      }
    }
  ]
}

RespuestaResponse

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "contoso.sharepoint.com,6598ee0b-0f5f-4416-a0ae-66d864efb43a,60024ce8-e74d-4d63-a939-ad00cd738670",
              "rank": 1,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.site",
                "id": "contoso.sharepoint.com,6598ee0b-0f5f-4416-a0ae-66d864efb43a,60024ce8-e74d-4d63-a939-ad00cd738670",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "description": "Contoso Communication Site",
                "lastModifiedDateTime": "2020-08-30T06:41:56Z",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/"
              }
            }
          ]
        }
      ]
    }
  ]
}

Ejemplo 4: buscar en todo el contenido de OneDrive y SharePointExample 4: Search all content in OneDrive and SharePoint

En este ejemplo se consulta todo el contenido de los sitios de OneDrive y SharePoint al que el usuario que ha iniciado sesión tiene acceso de lectura.This example queries all the content in OneDrive and SharePoint sites to which the signed-in user has read access. La propiedad Resource de la respuesta devuelve resultados que son archivos y carpetas como objetos driveItem , coincidencias que son contenedores (listas de SharePoint) como Listy todas las demás coincidencias como ListItem.The resource property in the response returns matches that are files and folders as driveItem objects, matches that are containers (SharePoint lists) as list, and all other matches as listItem.

SolicitudRequest

POST /search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
        "driveItem", "listItem", "list"
      ],
      "query": {
        "queryString": "contoso"
      }
    }
  ]
}

RespuestaResponse

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "contoso"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "@odata.type": "#microsoft.graph.searchHitsContainer",
              "hitId": "FlULeN/ui/1GjLx1rUfio5UAAEl",
              "rank": 1,
              "summary": "<c0>Contoso</c0> Detailed Design <ddd/>",
              "resource": {
                "@odata.type": "#microsoft.graph.driveItem",
                "createdDateTime": "2019-06-10T06:37:43Z",
                "lastModifiedDateTime": "2019-06-10T06:37:43Z",
                "name": "web_part_test_long Notebook",
                "webUrl": "https://contoso.sharepoint.com/sites/contoso-team/contoso-designs.docx",
                "createdBy": {
                 "user": {
                   "displayName": "Michaelvincent Santos;Provisioning User"
                  }
                },
                "lastModifiedBy": {
                  "user": {
                    "displayName": "Richard Mayer"
                  }
                },
                "parentReference": {
                  "siteId": "m365x231305.sharepoint.com,5724d91f-650c-4810-83cc-61a8818917d6,c3ba25dc-2c9f-48cb-83be-74cdf68ea5a0"
                }
                "fileSystemInfo": {
                  "createdDateTime": "2019-06-10T06:37:43Z",
                  "lastModifiedDateTime": "2019-06-10T06:37:43Z"
                }
              }
            },
            {
              "@odata.type": "#microsoft.graph.searchHit",
              "hitId": "51eef59e-5d49-4d28-96f0-864cf90765e0",
              "rank": 2,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.list",
                "displayName": "Contoso - Documents",
                "id": "51eef59e-5d49-4d28-96f0-864cf90765e0",
                "description": "",
                "lastModifiedDateTime": "2020-07-08T18:17:59+00:00",
                "name": "Shared Documents",
                "parentReference": {
                "siteId": "microsoft.sharepoint-df.com,220fd155-0ea2-477c-a816-5c08fdc45f5d,fad16ab6-0736-4fbc-a053-087296b47c99"
                },
                "webUrl": "https://microsoft.sharepoint-df.com/teams/spoppe/collab/TaskBoard/Contoso/Shared Documents/Forms/AllItems.aspx"
              }
            }
          ]
        }
      ]
    }
  ]
}

Pasos siguientesNext steps