SPFx webpart to fetch tasks assigned for a user from planner

Sudheer Madduru, Naga 0 Reputation points
2025-01-01T12:55:58.66+00:00

Hi,

I want to fetch tasks assigned for a user from planner through spfx webpart.

Please share any reference links of the same.

Regards,

Sudheer

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,200 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ling Zhou_MSFT 20,710 Reputation points Microsoft Vendor
    2025-01-02T02:28:33.04+00:00

    Hi @Sudheer Madduru, Naga,

    Thanks for reaching out to us. We are very pleased to assist you.

    According to the information you provided and the research I have done, we can use Microsoft Graph API and PnPjs to fetch tasks assigned for a user in an SPFx web part.

    Steps are as follows:

    1. Register an Azure AD application and add necessary permissions to this application. For fetching tasks, you typically need Tasks.Read or Tasks.ReadWrite permissions. For specific permissions, see this:User's image

    2. Use the Microsoft Graph API to fetch tasks assigned for a user.

    GET https://graph.microsoft.com/v1.0/users/{user-id}/planner/tasks
    

    Parse the JSON response to extract the task details you need. Here is an example of what the response might look like.

    3. Configure your SPFx web part to use the MSAL.js library for authentication and token acquisition. Here is a detailed guide on how to integrate MSAL.js with SPFx

    4. PnPjs provides a convenient way to interact with Microsoft Graph. Here's how you can use it to fetch tasks: Reference: @pnp/graph/planner

    First, install PnPjs:

       npm install @pnp/graph @pnp/logging @pnp/common @pnp/odata
    

    Second, import and Configure PnPjs:

       import { graphfi, GraphFI } from "@pnp/graph";
       import { SPFx } from "@pnp/spfx-context";
       
       const graph: GraphFI = graphfi().using(SPFx(this.context));
    

    Third, fetch Tasks:

       // Function to fetch tasks for a specific user
       async function fetchUserTasks(userId: string) {
       try {
       const userTasks = await graph.users.getById(userId).planner.tasks();
       console.log(userTasks);
       } catch (error) {
       console.error("Error fetching tasks: ", error);
       }
       }
       
       // Call the function with the specific user ID
       fetchUserTasks("user-id");
    

    Also:

    There is a sample web part in GitHub that allows users to manage scheduled tasks in a SharePoint site.

    However, it only includes the ability to search for tasks by name and filter by progress status, to which you can add the ability to fetch tasks assigned for a user. This way you don't have to create the SPFx web part from scratch.

    If you have any questions, please do not hesitate to contact me.

    Moreover, if the issue can be fixed successfully, please click "Accept Answer" so that we can better archive the case and the other community members who are suffering the same issue can benefit from it.

    Your kind contribution is much appreciated.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.