如何为用户更新管理器? [英] How do I update the manager for user?

查看:58
本文介绍了如何为用户更新管理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码

GraphServiceClient graphClient =
    new GraphServiceClient("https://graph.microsoft.com/v1.0",
        new DelegateAuthenticationProvider(async(requestMessage) =>
        {
            requestMessage.Headers.Authorization =
                new AuthenticationHeaderValue("bearer", await GetTokenAsync(iclientApp));
        })
    );

User currentUser = await graphClient
    .Me
    .Request()
    .GetAsync();

string filter = String.Format("startswith(surname,'{0}')", "ADTest");
var users = await graphClient.Users
    .Request()
    .Filter(filter)
    .GetAsync();

var user = users[0];
DirectoryObject userManager = new DirectoryObject();
userManager.Id = currentUser.Id;

await graphClient
    .Users[user.Id]
    .Request()
    .UpdateAsync(new User()
    {
        Manager = userManager
    });

没有引发错误,但manager属性没有得到更新

No error is throwing but the manager attribute is not getting updated

推荐答案

您在这里遇到了一些问题.

You have a few things going wrong here.

  1. 此操作是一个 PUT ,因此您应使用 PutAsync()而不是 UpdateAsync()(这是一个 POST ).

  1. This action is a PUT so you should use PutAsync() rather than UpdateAsync() (which is a POST).

您正在更新 user.Id ,并将其管理者分配为 user.Id .换句话说,您是在告诉Graph该用户的管理员是用户本身(显然不是这种情况).

You're updating user.Id and assigning its manager as user.Id. In other words, you're telling Graph that this user's manager is the user themselves (which obviously is not the case).

您的代码应更像这样:

// Create your client
GraphServiceClient graphClient =
    new GraphServiceClient("https://graph.microsoft.com/v1.0",
        new DelegateAuthenticationProvider(async(requestMessage) =>
        {
            requestMessage.Headers.Authorization =
                new AuthenticationHeaderValue("bearer", await GetTokenAsync(iclientApp));
        })
    );

// Get your list of users
string filter = String.Format("startswith(surname,'{0}')", "ADTest");
var users = await graphClient.Users
    .Request()
    .Filter(filter)
    .GetAsync();

// Grab the first user returned to use as the manager
var manager = users[0];

// Assign this manager to the user currently signed in
await graphClient.Me.Manager.Reference.Request().PutAsync(manager.Id);

您可以在SDK的 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆