使用 Microsoft Graph .NET 客户端库分配经理

Assign a manager using Microsoft Graph .NET Client Library

有人可以提示我如何使用 Microsoft Graph .NET Client Library? I know how to do this via a direct REST call 设置 User.Manager 字段但希望尽可能避免这些调用(绕过包装器)。使用下面的代码,我能够清除管理器 属性.

client = new GraphClient(...);
var builder = new DirectoryObjectWithReferenceRequestBuilder(
    client.Users["<userid>"].Manager.Request().RequestUrl,client
);
builder.Request().DeleteAsync().Wait()

但是,我仍然无法弄清楚哪个 class 允许我为成员引用 (as described here) 构建 PUT 请求。我尝试了以下代码:

var mgr = new DirectoryObject();
mgr.Id = "<Id of the user that should be set as manager>";

var usrPatch = new User();
usrPatch.Manager = mgr;

client.Users["<Id of the user to be updated>"].Request().UpdateAsync(usrPatch).Wait();

此代码不会引发异常,但也不会更新管理器。请求是错误的。上面的代码将 PATCH 而不是 PUT 发送到 "base" 对象。

上面代码生成的请求:

PATCH https://graph.microsoft.com/v1.0/users/[Id of the user to be updated] HTTP/1.1
SdkVersion: graph-dotnet-1.0.1 
Content-Type: application/json 
Host: graph.microsoft.com 
Content-Length: 45 Expect: 100-continue

{"id":"[Id of the user that should be set as manager]"}

响应是 204。

我看到以下删除管理员的信息:

graphClient.Users[newUser.Id].Manager.Reference.Request().DeleteAsync();

但是我们应该有类似下面的东西来分配经理:

graphClient.Users[newUser.Id].Manager.Reference.Request().AddAsync(manager);

我会为此提交错误并在修复后更新。

完全支持更新 "manager" 之前的解决方法:

var authToken = "<get your token here>";
var client = new GraphClient(...);

var usrId = "<id of the user to update>"
var mgrId = "<id of the manager>"

var url = client.Users[usrId].Manager.Reference.Request().RequestUrl;

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue("Bearer", authToken);

var content = new StringContent(
    client.HttpProvider.Serializer.SerializeObject(
        new ReferenceRequestBody
        {
            ODataId =
                $"{client.BaseUrl}/directoryObjects/{mgrId}"
        }),
    Encoding.Default,
    "application/json");

var resp = httpClient.PutAsync(url, content).Result;
if (!resp.IsSuccessStatusCode)
{
    // throw exception/log etc
}

两年后,这仍然不起作用。

我在使用 await graphClient.Users[user.Id].Manager.Request().UpdateAsync(usrPatch);

时遇到以下异常
Microsoft.Graph.ServiceException: 'Code: BadRequest
Message: Write requests are only supported on contained entities

我不得不使用 stefboe 的变通逻辑来更新管理器。

Microsoft.Graph DLL 版本为 1.12。

使用 PutAsync() 设置使用图形库的管理器: graphClient.Users[user.Id].Manager.Reference.Request().PutAsync(manager.Id);