Utiliser l’API Microsoft Recherche pour rechercher des messages Teams
Article
Utilisez l’API Microsoft Recherche dans Microsoft Graph pour rechercher des informations dans les messages Teams, retourner les messages classés par pertinence et afficher une expérience de recherche dédiée. La recherche s’applique au corps et aux pièces jointes des messages dans les messages Teams de l’utilisateur connecté.
Attention
Le schéma de l’API de recherche a changé dans la version bêta. Certaines propriétés d’une demande de recherche et d’une réponse ont été renommées ou supprimées. Pour plus d’informations, consultez Avertissement de dépréciation de modification de schéma. Les exemples de cette rubrique montrent le schéma à jour.
La recherche de messages Teams recherche également les pièces jointes. Les types de fichiers pris en charge pour la recherche de pièces jointes de message sont les mêmes que ceux de la recherche SharePoint Online.
Exemples
Exemple 1 : message de conversation Recherche Teams
Demande
L’exemple suivant interroge les messages de conversation Teams dans le stockage de conversation Teams de l’utilisateur connecté qui contient la chaîne « test » dans n’importe quelle partie du message de conversation (nom de l’expéditeur, corps du message ou pièces jointes). La requête retourne les 25 premiers résultats. Les résultats de la recherche sont classés par dateTime décroissant.
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Search.Query;
using Microsoft.Graph.Beta.Models;
var requestBody = new QueryPostRequestBody
{
Requests = new List<SearchRequest>
{
new SearchRequest
{
EntityTypes = new List<EntityType?>
{
EntityType.ChatMessage,
},
Query = new SearchQuery
{
QueryString = "test",
},
From = 0,
Size = 25,
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Search.Query.PostAsQueryPostResponseAsync(requestBody);
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.beta.search.query.QueryPostRequestBody queryPostRequestBody = new com.microsoft.graph.beta.search.query.QueryPostRequestBody();
LinkedList<SearchRequest> requests = new LinkedList<SearchRequest>();
SearchRequest searchRequest = new SearchRequest();
LinkedList<EntityType> entityTypes = new LinkedList<EntityType>();
entityTypes.add(EntityType.ChatMessage);
searchRequest.setEntityTypes(entityTypes);
SearchQuery query = new SearchQuery();
query.setQueryString("test");
searchRequest.setQuery(query);
searchRequest.setFrom(0);
searchRequest.setSize(25);
requests.add(searchRequest);
queryPostRequestBody.setRequests(requests);
var result = graphClient.search().query().post(queryPostRequestBody);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Search\Query\QueryPostRequestBody;
use Microsoft\Graph\Beta\Generated\Models\SearchRequest;
use Microsoft\Graph\Beta\Generated\Models\EntityType;
use Microsoft\Graph\Beta\Generated\Models\SearchQuery;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new QueryPostRequestBody();
$requestsSearchRequest1 = new SearchRequest();
$requestsSearchRequest1->setEntityTypes([new EntityType('chatMessage'), ]);
$requestsSearchRequest1Query = new SearchQuery();
$requestsSearchRequest1Query->setQueryString('test');
$requestsSearchRequest1->setQuery($requestsSearchRequest1Query);
$requestsSearchRequest1->setFrom(0);
$requestsSearchRequest1->setSize(25);
$requestsArray []= $requestsSearchRequest1;
$requestBody->setRequests($requestsArray);
$result = $graphServiceClient->search()->query()->post($requestBody)->wait();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.search.query.query_post_request_body import QueryPostRequestBody
from msgraph_beta.generated.models.search_request import SearchRequest
from msgraph_beta.generated.models.entity_type import EntityType
from msgraph_beta.generated.models.search_query import SearchQuery
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = QueryPostRequestBody(
requests = [
SearchRequest(
entity_types = [
EntityType.ChatMessage,
],
query = SearchQuery(
query_string = "test",
),
from = 0,
size = 25,
),
],
)
result = await graph_client.search.query.post(request_body)
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Search.Query;
using Microsoft.Graph.Models;
var requestBody = new QueryPostRequestBody
{
Requests = new List<SearchRequest>
{
new SearchRequest
{
EntityTypes = new List<EntityType?>
{
EntityType.ChatMessage,
},
Query = new SearchQuery
{
QueryString = "test",
},
From = 0,
Size = 15,
EnableTopResults = true,
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Search.Query.PostAsQueryPostResponseAsync(requestBody);
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.search.query.QueryPostRequestBody queryPostRequestBody = new com.microsoft.graph.search.query.QueryPostRequestBody();
LinkedList<SearchRequest> requests = new LinkedList<SearchRequest>();
SearchRequest searchRequest = new SearchRequest();
LinkedList<EntityType> entityTypes = new LinkedList<EntityType>();
entityTypes.add(EntityType.ChatMessage);
searchRequest.setEntityTypes(entityTypes);
SearchQuery query = new SearchQuery();
query.setQueryString("test");
searchRequest.setQuery(query);
searchRequest.setFrom(0);
searchRequest.setSize(15);
searchRequest.setEnableTopResults(true);
requests.add(searchRequest);
queryPostRequestBody.setRequests(requests);
var result = graphClient.search().query().post(queryPostRequestBody);
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Search\Query\QueryPostRequestBody;
use Microsoft\Graph\Generated\Models\SearchRequest;
use Microsoft\Graph\Generated\Models\EntityType;
use Microsoft\Graph\Generated\Models\SearchQuery;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new QueryPostRequestBody();
$requestsSearchRequest1 = new SearchRequest();
$requestsSearchRequest1->setEntityTypes([new EntityType('chatMessage'), ]);
$requestsSearchRequest1Query = new SearchQuery();
$requestsSearchRequest1Query->setQueryString('test');
$requestsSearchRequest1->setQuery($requestsSearchRequest1Query);
$requestsSearchRequest1->setFrom(0);
$requestsSearchRequest1->setSize(15);
$requestsSearchRequest1->setEnableTopResults(true);
$requestsArray []= $requestsSearchRequest1;
$requestBody->setRequests($requestsArray);
$result = $graphServiceClient->search()->query()->post($requestBody)->wait();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.search.query.query_post_request_body import QueryPostRequestBody
from msgraph.generated.models.search_request import SearchRequest
from msgraph.generated.models.entity_type import EntityType
from msgraph.generated.models.search_query import SearchQuery
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = QueryPostRequestBody(
requests = [
SearchRequest(
entity_types = [
EntityType.ChatMessage,
],
query = SearchQuery(
query_string = "test",
),
from = 0,
size = 15,
enable_top_results = True,
),
],
)
result = await graph_client.search.query.post(request_body)
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Search.Query;
using Microsoft.Graph.Models;
var requestBody = new QueryPostRequestBody
{
Requests = new List<SearchRequest>
{
new SearchRequest
{
EntityTypes = new List<EntityType?>
{
EntityType.ChatMessage,
},
Query = new SearchQuery
{
QueryString = "contoso from:bob to:alice sent>2022-07-14",
},
From = 0,
Size = 15,
EnableTopResults = true,
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Search.Query.PostAsQueryPostResponseAsync(requestBody);
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.search.query.QueryPostRequestBody queryPostRequestBody = new com.microsoft.graph.search.query.QueryPostRequestBody();
LinkedList<SearchRequest> requests = new LinkedList<SearchRequest>();
SearchRequest searchRequest = new SearchRequest();
LinkedList<EntityType> entityTypes = new LinkedList<EntityType>();
entityTypes.add(EntityType.ChatMessage);
searchRequest.setEntityTypes(entityTypes);
SearchQuery query = new SearchQuery();
query.setQueryString("contoso from:bob to:alice sent>2022-07-14");
searchRequest.setQuery(query);
searchRequest.setFrom(0);
searchRequest.setSize(15);
searchRequest.setEnableTopResults(true);
requests.add(searchRequest);
queryPostRequestBody.setRequests(requests);
var result = graphClient.search().query().post(queryPostRequestBody);
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.search.query.query_post_request_body import QueryPostRequestBody
from msgraph.generated.models.search_request import SearchRequest
from msgraph.generated.models.entity_type import EntityType
from msgraph.generated.models.search_query import SearchQuery
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = QueryPostRequestBody(
requests = [
SearchRequest(
entity_types = [
EntityType.ChatMessage,
],
query = SearchQuery(
query_string = "contoso from:bob to:alice sent>2022-07-14",
),
from = 0,
size = 15,
enable_top_results = True,
),
],
)
result = await graph_client.search.query.post(request_body)
Vous pouvez accéder uniquement au message Teams de l’utilisateur connecté ou au message dans lequel l’utilisateur est inclus.
L’API de recherche Teams ne retourne pas toutes les propriétés définies dans chatMessage. Vous pouvez utiliser l’API Teams pour récupérer plus de détails sur n’importe quel message.
Pour les messages Teams, la propriété total du type searchHitsContainer contient le nombre de résultats sur la page, et non le nombre total de résultats correspondants.
Le tri des résultats n’est pas pris en charge pour les messages.
Vous ne pouvez pas utiliser cette API avec d’autres types d’entités pour l’instant.
Représentation JSON
Voici une représentation JSON de toutes les propriétés récupérables actuellement disponibles pour la recherche chatMessage .