How can I use search in Microsoft Graph?

How can I use search in Microsoft Graph?

You can use the $search query parameter to search for items in Microsoft Graph. The $search parameter performs a full-text search on the default search property of each resource type, which is usually the displayName property for most resources. Here's an example of how to use the $search parameter in a request to search for users whose display name contains the string "John":

var options = new List<QueryOption>()
{
    new QueryOption("$search", "John")
};

var users = await graphClient.Users
    .Request(options)
    .GetAsync();

In this example, the graphClient is an instance of the GraphServiceClient class, which is used to interact with the Microsoft Graph API. The Users property of the graphClient represents the collection of user resources in the directory. The Request method is used to create a new request for users, and the $search parameter is added to the request options to search for users whose display name contains the string "John". The GetAsync method is then used to execute the request and retrieve the search results.

Note that the $search parameter is only supported by certain resource types in Microsoft Graph, and the search behavior may vary depending on the resource type. You can find more information about the $search parameter and its usage in the Microsoft Graph documentation.

 

What if I need search on DisplayName and mail?

You can use the $search query parameter to search across multiple fields in Microsoft Graph. You can specify the fields to search in by listing them in the query parameter, separated by commas.

Here's an example that searches for users with a display name or email address that contains the search term "john":

var queryOptions = new List<QueryOption>()
{
    new QueryOption("$search", "displayName:john OR mail:john")
};

var users = await graphServiceClient.Users
    .Request(queryOptions)
    .GetAsync();

In this example, the $search query parameter specifies that we want to search for users whose display name contains "john" OR whose email address contains "john".

Note that the $search query parameter is only available for a limited set of Microsoft Graph resources, such as users, messages, and groups. The specific resources and fields that can be searched vary depending on the resource type.

 

需要注意的是,search的两个条件使用OR连接的,并且2个条件都各自用了独立的字符串

Write this request in c# :     GET https://graph.microsoft.com/v1.0/users?$search="displayName:wa" OR "displayName:ad"&$orderbydisplayName&$count=true
ConsistencyLevel: eventual

To send this request in C# using the Microsoft Graph SDK, you can use the following code: 注意代码里面的双引号转义

// Create a GraphServiceClient instance
var graphClient = new GraphServiceClient(authProvider);

// Set the query options
List<QueryOption> queryOptions = new List<QueryOption>()
{
    new QueryOption("$search", "\"displayName:wa\" OR \"displayName:ad\""),
    new QueryOption("$orderby", "displayName"),
    new QueryOption("$count", "true")
};

// Send the request
var users = await graphClient.Users
    .Request(queryOptions)
    .WithConsistencyLevel(ConsistencyLevel.Eventual)
    .GetAsync();

This code sets the search query to search for users whose display name contains "wa" OR "ad", orders the results by display name, and includes a count of the total number of users in the result set. The WithConsistencyLevel method is used to set the consistency level of the request to eventual.

 
 

 

posted @ 2023-03-03 12:06  ChuckLu  阅读(48)  评论(0)    收藏  举报