如何为用户更新管理器?

时间:2019-05-14 11:39:40

标签: c# azure-active-directory microsoft-graph microsoft-graph-sdks

我正在使用以下代码

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属性没有得到更新

1 个答案:

答案 0 :(得分:0)

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

  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中找到一个示例。