首页 > 解决方案 > 使用 Microsoft Graph SDK 获取具有特定电子邮件域的所有用户

问题描述

我想使用 Microsoft Graph SDK 对 Microsoft Graph API 进行此查询。我想让所有用户在电子邮件地址中的域是@something.com。

将 $filter 与 endsWith 运算符一起使用

GET ../users?$count=true&$filter=endsWith(mail,'@something.com')

我尝试了以下代码行:

var users= await _graphServiceClient.Users.Request().Filter("mail '@something.com'").Select(u => new {
                u.Mail,
                u.DisplayName,
            }).GetAsync();

我得到的错误是:

    Microsoft.Graph.ServiceException: 'Code: BadRequest
Message: Invalid filter clause

没有过滤器,它工作正常。我错过了什么吗?

参考:高级查询:https ://docs.microsoft.com/en-us/graph/query-parameters Microsoft Graph SDK:https://docs.microsoft.com/en-us/graph/sdks/create-requests?选项卡=CS

标签: c#microsoft-graph-apimicrosoft-graph-sdks

解决方案


如果要使用$count查询参数,则需要添加ConsistencyLevel带有eventual值的标头。

GET /users?$count=true&$filter=endsWith(mail,'@something.com')
ConsistencyLevel: eventual

在 C# 中为请求指定标头选项和查询选项:

var options = new List<Option>();
options.Add(new HeaderOption("ConsistencyLevel", "eventual"));
options.Add(new QueryOption("$count", "true"));

endsWith运算符添加到过滤器。

var users = await _graphServiceClient.Users
    .Request(options)
    .Filter("endsWith(mail,'@something.com')")
    .GetAsync();

推荐阅读