使用Microsoft Planner API将用户分配给新任务

时间:2018-01-30 12:29:00

标签: c# microsoft-graph restsharp

我正在尝试使用Microsoft Graph API将用户分配给任务来访问Planner。 当我没有尝试分配某人时,我的代码工作正常(通过省略assignments部分),但当我添加分配部分(following the template here)时,我得到了一个BadRequest错误回复。

  

{      “错误”:{       “代码”:“”,       “message”:“请求无效:\ r \ nValue不能为空。\ r \ nParameter name:qualifiedName”,       “innerError”:{         “request-id”:“......”,         “日期”:“2018-01-30T12:23:59”       }      }   }

public TaskResponse CreateTask(string planId, string bucketId, string title, Dictionary<string, Assignment> assignments = null, DateTime? dueDateTime = null)
{
    RestRequest TaskRequest = new RestRequest("planner/tasks", Method.POST)
    {
        RequestFormat = DataFormat.Json
    };
    Dictionary<string, object> assignees = new Dictionary<string, object>();
    foreach(string assignment in assignments.Keys)
    {
        assignees.Add(assignment, new
        {
        });
    }
    var body = new
    {
        planId,
        bucketId,
        title,
        assignments = assignees,
        dueDateTime
    };
    TaskRequest.AddParameter("application/json", JsonConvert.SerializeObject(body), "application/json", ParameterType.RequestBody);
    IRestResponse TaskResponse = GraphClient.Execute(TaskRequest);
    if (!TaskResponse.IsSuccessful)
    {
        return null;
    }
    var result = JsonConvert.DeserializeObject<TaskResponse>(TaskResponse.Content);
    return result;
}

如果有人知道为什么回复表明我没有提供文档中从未提及的参数,我会很感激......

1 个答案:

答案 0 :(得分:1)

我设法通过使用适当的类而不是匿名类型来解决这个问题。通过这样做,我能够使用@odata.type注释该属性,这不能作为变量名称接受。

public class NewAssignment
{
    [JsonProperty("@odata.type")]
    public string ODataType { get; set; }

    [JsonProperty("orderHint")]
    public string OrderHint { get; set; }

    public NewAssignment()
    {
        ODataType = "#microsoft.graph.plannerAssignment";
        OrderHint = " !";
    }
}

这允许我使用以下代码:

Dictionary<string, object> assignees = new Dictionary<string, object>();
foreach (string assignment in assignments.Keys)
{
    assignees.Add(assignment, new NewAssignment());
}
var body = new
{
    planId,
    bucketId,
    title,
    assignments = assignees,
    dueDateTime
};