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:
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.