简体   繁体   中英

How to add an assignment to a Planner task using Microsoft Graph API

I'm using the Microsoft Graph API to access my Planner tasks from Office 365 and have created an improved front end using Angular and the `@microsoft/microsoft-graph-client. However, I have hit a problem creating new tasks. I can create a new task, but I can't manage to assign it to a user.

My code looks like this:

async addTask(planId: string, bucketId: string, title: string): Promise < Task > {

    let postData = { "planId": planId, "bucketId": bucketId, "title": title };

    let result = await this.graphClient
        .api("/planner/tasks")
        .header("Prefer", "return=representation")
        .version("beta")
        .post(postData);

    // works fine up to here and creates the task

    let newAssignment = new assignment;
    let assignments = `{"${this.userId}": ${newAssignment}}`;

    let newTask = await this.graphClient
        .api(`/planner/tasks/${result.id}`)
        .header("If-Match", result["@odata.etag"])
        .header("Prefer", "return=representation")
        .patch({ "assignments": assignments });

    return newTask;

}

export class assignment {
    "@odata.type": string;
    orderHint: string;

    constructor() {
        this["@odata.type"] = "microsoft.graph.plannerAssignment";
        this.orderHint = " !";
    }
}

The first section works fine and creates the task with nobody assigned, but the second section fails with a 400 error "Property assignments in payload has a value that does not match the schema."

The newAssignments object looks like this:

{
  @odata.type: "microsoft.graph.plannerAssignment", 
  orderHint: " !"
}

I created the assignments class, following the answer to a similar question about C#, previously had just been sending a basic object, like this:

let assignments = `{"${this.userId}": {"@odata.type": "microsoft.graph.plannerAssignment", "orderHint": " !"}}`

but that had also failed with the same error.

One question is whether you amend the assignments property using 'patch' or whether I should be adding an assignment using 'post'. I've tried both and neither work.

I managed to work it out in the end. I don't fully understand the reasons why, so feel free to comment/answer if you have more insight.

The code as written was sending the JSON payload in the wrong form, so in fiddler it was appearing like this:

assignments = {"a5b71811-bc30-4cf2-99cf-8edc14aa96f8": {"@odata.type": "microsoft.graph.plannerAssignment", "orderHint": " !"}}

Adjusting the code as below solved the issue:

let assignments = `{"assignments": {"${this.userId}": {"@odata.type": "microsoft.graph.plannerAssignment", "orderHint": " !"}}}`

let newTask = await this.graphClient
  .api(`/planner/tasks/${result.id}`)
  .header("If-Match", result["@odata.etag"])
  .header("Prefer","return=representation")
  .patch(assignments);

This now sends the correct JSON representation, which appears as below in fiddler. This works properly. As you'll see, the assignment is submitted using 'patch' and not 'post'.

- JSON
  - assignments
    - a5b71811-bc30-4cf2-99cf-8edc14aa96f8
        @odata.type = microsoft.graph.plannerAssignment
        orderHint =  !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM