microsoftgraph / msgraph-sdk-dotnet

Microsoft Graph Client Library for .NET!
https://graph.microsoft.com
Other
703 stars 249 forks source link

add "assignedBy" and "createdBy" in PlannerTask #1776

Closed manojSpant closed 1 year ago

manojSpant commented 1 year ago

We are using application permission, and need to create a planner task. All things are working fine, but we need to add "assignedBy" and "CreatedBy" users (We have the object Id's of users).

By default when we are creating a Planner task with ClientSecretCredential[appication permission], it passes application id(Client Id of Azure AD registered application ) "assignedBy" and "CreatedBy", but we need to pass the user object Id also, So then in planner, It shows "assigned to" and "assigned by" user also.

Note:

  1. Application permission and Microsoft.Graph V5.3.0

Below is the code which we are using:

            string tenentId = "xxxxxxxx";
            string clientId = "xxxxxxx";
            string clientScret = "xxxxxxxxx";

           // Application permission 
            ClientSecretCredential clientSecretCredential = new ClientSecretCredential(tenentId, clientId, clientScret);
            GraphServiceClient gsClient = new GraphServiceClient(clientSecretCredential);

            var plannerAssignments = new PlannerAssignments();

           // Add assign to
            var plannerAssignment = new PlannerAssignment();
            plannerAssignment.OrderHint = " !";
            plannerAssignment.OdataType = "#microsoft.graph.plannerAssignment";             

            plannerAssignments.AdditionalData.Add("af0c0624-3138-446d-9d9e-f86159890745", plannerAssignment);           

            var requestBody = new PlannerTask
            {
                PlanId = "wOJWGexB0U-a3n-LZ9nX_2UAD-xT",
                BucketId = "RWBdSfn0h0OsFR9XBksEKmUAIgaG",
                Title = "Update client list 100",

                Assignments = plannerAssignments,

                CreatedBy = new IdentitySet() { User = new Identity() {  DisplayName= "Adele Vance", Id = "ba8ebefe-fdb9-4ef6-b6c0-80c745d12a0e", OdataType = "#microsoft.graph.identitySet" } }

            };               

            var plannerTaskRes = await gsClient.Planner.Tasks.PostAsync(requestBody);

Response: { "activeChecklistItemCount": 0, "appliedCategories": { "additionalData": {}, "backingStore": { "initializationCompleted": true, "returnOnlyChangedValues": false }, "odataType": null }, "assignedToTaskBoardFormat": null, "assigneePriority": "", "assignments": { "additionalData": { "af0c0624-3138-446d-9d9e-f86159890745": { "@odata.type": "#microsoft.graph.plannerAssignment", "assignedDateTime": "2023-03-28T11:27:41.7231301Z", "orderHint": "8585216040838169557Pp", "assignedBy": { "application": { "displayName": null, "id": "9456de51-6b8c-4d96-9c70-f944f30d76d9" } } } }, "backingStore": { "initializationCompleted": true, "returnOnlyChangedValues": false }, "odataType": null }, "bucketId": "RWBdSfn0h0OsFR9XBksEKmUAIgaG", "bucketTaskBoardFormat": null, "checklistItemCount": 0, "completedBy": null, "completedDateTime": null, "conversationThreadId": null, "createdBy": { "additionalData": {}, "application": { "additionalData": {}, "backingStore": { "initializationCompleted": true, "returnOnlyChangedValues": false }, "displayName": null, "id": "9456de51-6b8c-4d96-9c70-f944f30d76d9", "odataType": null }, "backingStore": { "initializationCompleted": true, "returnOnlyChangedValues": false }, "device": null, "odataType": null, "user": null }, "createdDateTime": "2023-03-28T11:27:41.7231301+00:00", "details": null, "dueDateTime": null, "hasDescription": false, "orderHint": "8585216040237544506", "percentComplete": 0, "planId": "wOJWGexB0U-a3n-LZ9nX_2UAD-xT", "previewType": 0, "priority": 5, "progressTaskBoardFormat": null, "referenceCount": 0, "startDateTime": null, "title": "Update client list 100", "additionalData": { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#planner/tasks/$entity", "@odata.etag": "W/\"JzEtVGFzayAgQEBAQEBAQEBAQEBAQEBARCc=\"" }, "backingStore": { "initializationCompleted": true, "returnOnlyChangedValues": false }, "id": "y7OI68HfpUa4CQpuhp7pvWUACxKu", "odataType": null }

andrueastman commented 1 year ago

Thanks for raising this @manojSpant

Any chance using this makes it work out?

        var plannerAssignment = new PlannerAssignment();
        plannerAssignment.OrderHint = " !";
        plannerAssignment.OdataType = "#microsoft.graph.plannerAssignment";
        plannerAssignment.AdditionalData["assignedBy"] = new IdentitySet
        {
            User = new Identity
            {
                Id =  "user-id"
            }
        };
manojSpant commented 1 year ago

Thanks for the reply @andrueastman,

We have applied the above code but it is showing an exception:

The request is invalid: An unexpected 'StartObject' node was found for property named 'assignedBy' when reading from the JSON reader. A 'PrimitiveValue' node was expected.

image

Code:

                 string tenentId =  "xxxxxxx";
                string clientId = " "xxxxxxx";
                string clientScret = "xxxxxxx";

                ClientSecretCredential clientSecretCredential = new ClientSecretCredential(tenentId, clientId, clientScret);
                GraphServiceClient gsClient = new GraphServiceClient(clientSecretCredential);

                var plannerAssignments = new PlannerAssignments();

                var plannerAssignment = new PlannerAssignment();
                plannerAssignment.OrderHint = " !";
                plannerAssignment.OdataType = "#microsoft.graph.plannerAssignment";           

               // Adding AssignedBy
                plannerAssignment.AdditionalData["assignedBy"] = new IdentitySet
                {
                    User = new Identity
                    {
                        Id = "ba8ebefe-fdb9-4ef6-b6c0-80c745d12a0e"
                    }
                };

                plannerAssignments.AdditionalData.Add("af0c0624-3138-446d-9d9e-f86159890745", plannerAssignment);           

                var requestBody = new PlannerTask
                {
                    PlanId = "wOJWGexB0U-a3n-LZ9nX_2UAD-xT",
                    BucketId = "RWBdSfn0h0OsFR9XBksEKmUAIgaG",
                    Title = "Update client list 100",                    

                    Assignments = plannerAssignments           
                };

                var plannerTaskRes = await gsClient.Planner.Tasks.PostAsync(requestBody);
andrueastman commented 1 year ago

Thanks for this @manojSpant

It looks like the property may not be updatable from client side from the error and is autogenerated from the identities performing the task as the same error is reproducible in graph explorer. image

Also mentioned at the answer at https://learn.microsoft.com/en-us/answers/questions/1193981/add-assignedby-and-createdby-in-plannertask

As this is a request for the API side functionality and not the client library, would you be willing to create a request at the link below for the API owners to implement? It would be great if you could post back the link so that anyone who comes across this may also find it.

https://techcommunity.microsoft.com/t5/microsoft-365-developer-platform/idb-p/Microsoft365DeveloperPlatform

manojSpant commented 1 year ago

Thanks for the guidance @andrueastman,

andrueastman commented 1 year ago

We'll be closing this one for now as this requires action from the API side and not from the client library.