使用Microsoft Graph .NET Client Library分配管理器

时间:2016-04-29 11:23:48

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

有人可以给我一个提示如何使用Microsoft Graph .NET Client Library设置User.Manager字段吗?我知道how to do this via a direct REST call但是想尽可能避免这些调用(绕过包装器)。通过下面的代码,我可以清除经理财产。

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

但是,我仍然无法弄清楚哪个类允许我为成员引用(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。

4 个答案:

答案 0 :(得分:2)

我看到以下内容删除了一位经理:

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

但是我们应该像以下一样来分配经理:

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

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

答案 1 :(得分:0)

解决方法直到更新&#34;经理&#34;完全支持:

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
}

答案 2 :(得分:0)

两年后,这仍然无法正常工作。

使用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。

答案 3 :(得分:0)

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