如何更新用户的管理器?

How do I update the manager for user?

我正在使用以下代码

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
    });

未抛出任何错误,但管理器属性未更新

这里有些地方出了问题。

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

  2. 您正在更新 user.Id 将其经理指定为 user.Id。换句话说,您是在告诉 Graph 这个用户的经理就是用户自己(显然不是这样)。

您的代码应该更像这样:

// 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 UsersTests unit test 中找到这方面的示例。