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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用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编程