spacer
Skip to content

Graph Mail Folders

More information can be found in the official Graph documentation:

Selective Imports Banner

Get Mail folder listing

import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";

const graph = graphfi(...);

// This can be any folder id or Well-known folder names
const currentUserInbox = await graph.me.mailFolders.getById("inbox")();

// Get folders
const folders = await graph.me.mailFolders();

// Get folders and include hidden folders
const allFolders = await graph.me.mailFolders.includeHidden();

Get Specific Folder by Id or Well0know folder name

import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";

const graph = graphfi(...);

// Get folder by Well-known folder names
const currentUserInbox = await graph.me.mailFolders.getById("inbox")();

// Get folders
const folders = await graph.me.mailFolders();
// Get folder by folder id
const currentUserFolder = await graph.me.mailFolders.getById(folders[0].id)();

Get Folders - Delta

import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";
import {IMailFolderDelta} from "@pnp/graph/mail"

const graph = graphfi(...);

const currentUser = graph.me;

// retrieve a list of mail folder changes
const folders = await currentUser.mailFolders.delta()();

//You can also loop through the delta changes using the async iterator
const folders = currentUser.mailFolders.delta();
for await (const f of folders) {
    // array of changes
    console.log(f);
}

Add Folder or Search Folder

import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";

const graph = graphfi(...);

// Add Mail Folder
const draftFolder: IMailFolder = {
    displayName: "PnP Test Folder",
    isHidden: false,
};

const folder = await graph.users.getById(testUserName).mailFolders.add(draftFolder);

// Add Child Folder

const childFolder = await graph.users.getById(testUserName).mailFolders.getById({mailFolderId}).childFolders.add(draftFolder);

// Add Search Folder
const currentUserInbox = await graph.me.mailFolders.getById("inbox")();

const draftSearchFolder: IMailSearchFolder = {
    displayName: "PnP Test Search Folder",
    sourceFolderIds: [currentUserInbox.id],
    filterQuery: "{Your Query Goes Here}"
};

const searchFolder = await graph.users.getById(testUserName).mailFolders.getById(currentUserInbox.id).childFolders.add(draftSearchFolder);

Update Folder

import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";

const graph = graphfi(...);

const folder = await graph.users.getById(testUserName).mailFolders.update({displayName: "New Display Name"});

Delete Folder

import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";

const graph = graphfi(...);

await graph.users.getById(testUserName).mailFolders.getById({mailFolderId}).delete();

Copy/Move Folder

import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";

const graph = graphfi(...);

const destinationFolderId = "...";
// Move folder to destination folder
await graph.users.getById(testUserName).mailFolders.getById({mailFolderId}).move(destinationFolderId);
// Copy folder to destination folder
await graph.users.getById(testUserName).mailFolders.getById({mailFolderId}).copy(destinationFolderId);

Get Child Folders

import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";

const graph = graphfi(...);

await graph.users.getById(testUserName).mailFolders.getById({mailFolderId}).childFolders();

Get Folder's Messages

import { graphfi } from "@pnp/graph";
import "@pnp/graph/users";
import "@pnp/graph/mail";

const graph = graphfi(...);

await graph.users.getById(testUserName).mailFolders.getById({mailFolderId}).messages();

Messages

For more information on Messages