首页 文章

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

提问于
浏览
0

我正在尝试使用Microsoft Graph API将用户分配给任务来访问Planner . My code works fine when I'm not trying to assign someone (通过省略 assignments 部分),但是当我添加分配部分(following the template here)时,我得到了 BadRequest 错误 .

{“error”:{“code”:“”,“message”:“请求无效:\ r \ nValue不能为空 . \ r \ nParameter name:qualifiedName”,“innerError”:{“request-id “:”......“,”date“:”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 回答

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

相关问题