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.

 
 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-03-03 数据库根据两列数据得到层级关系SQL Queries to Manage Hierarchical or Parent-child Relational Rows in SQL Server
2021-03-03 escape from braxis site:us.forums.blizzard.com
2017-03-03 mongodb常用命令
2017-03-03 MongoDB Shell (mongo)
2017-03-03 Getting Started with MongoDB (MongoDB Shell Edition)
2017-03-03 Ioc
2015-03-03 35.3wCF编程
点击右上角即可分享
微信分享提示