Microsoft Graph API is a unified API that enables us to access the entire Microsoft 365 service. With the help of the PnP JS library, we can do unlimited activities in SharePoint Framework or other applications.
In this blog, I would like to show you the script to fetch all the channels from my teams. Below is the sample script which you can use in any application by adding the PnP Js library as a dependency.
import { graph } from "@pnp/graph"
import "@pnp/graph/users"
import "@pnp/graph/teams"
import "@pnp/graph/channels"
(async () => {
const { context } = await (window as any).moduleLoaderPromise
graph.setup({
spfxContext: context
});
getmyTeams();
})().catch(console.log)
async function getmyTeams() {
const myteams = await graph.me.joinedTeams();
console.log("My Teams: " + myteams.length);
myteams.forEach((team) => {
getChannels(team);
});
}
async function getChannels(team: any) {
const channels = await graph.teams.getById(team.id).channels()
channels.forEach((c) => {
console.log(team.displayName + " - " + c.displayName);
});
}
Hope this script helps in retrieving the teams and channels for the current user. Happy coding :)