spacer
Skip to content

@pnp/graph/onedrive

The ability to manage drives and drive items in Onedrive is a capability introduced in version 1.2.4 of @pnp/graph. Through the methods described you can manage drives and drive items in Onedrive.

IInvitations

Invokable Banner Selective Imports Banner

Scenario Import Statement
Selective import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";
Preset: All import "@pnp/graph/presets/all";

Get the default drive

Using the drive() you can get the default drive from Onedrive

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const drives = await graph.users.getById('user@tenant.onmicrosoft.com').drives();

const drives = await graph.me.drives();

Get all of the drives

Using the drives() you can get the users available drives from Onedrive

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const drives = await graph.users.getById('user@tenant.onmicrosoft.com').drives();

const drives = await graph.me.drives();

Get drive by Id

Using the drives.getById() you can get one of the available drives in Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const drive = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId');

const drive = await graph.me.drives.getById('driveId');

Get the associated list of a drive

Using the list() you get the associated list

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const list = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').list();

const list = await graph.me.drives.getById('driveId').list();

Get the recent files

Using the recent() you get the recent files

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const files = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').recent();

const files = await graph.me.drives.getById('driveId').recent();

Get the files shared with me

Using the sharedWithMe() you get the files shared with the user

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const shared = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').sharedWithMe();

const shared = await graph.me.drives.getById('driveId').sharedWithMe();

Get the Root folder

Using the root() you get the root folder

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const root = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').root();

const root = await graph.me.drives.getById('driveId').root();

Get the Children

Using the children() you get the children

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const rootChildren = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').root.children();

const rootChildren = await graph.me.drives.getById('driveId').root.children();

const itemChildren = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').items.getById('itemId').children();

const itemChildren = await graph.me.drives.getById('driveId').root.items.getById('itemId').children();

Add folder or item

Using the add you can add a folder or an item

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";
import { DriveItem as IDriveItem } from "@microsoft/microsoft-graph-types";

const addFolder = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').root.children.add('New Folder', <IDriveItem>{folder: {}});

const addFolder = await graph.me.drives.getById('driveId').root.children.add('New Folder', <IDriveItem>{folder: {}});

Search items

Using the search() you can search for items, and optionally select properties

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const search = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId')root.search('queryText')();

const search = await graph.me.drives.getById('driveId')root.search('queryText')();

Get specific item in drive

Using the items.getById() you can get a specific item from the current drive

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const item = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').items.getById('itemId');

const item = await graph.me.drives.getById('driveId').items.getById('itemId');

Get thumbnails

Using the thumbnails() you get the thumbnails

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const thumbs = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').items.getById('itemId').thumbnails();

const thumbs = await graph.me.drives.getById('driveId').items.getById('itemId').thumbnails();

Delete drive item

Using the delete() you delete the current item

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const thumbs = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').items.getById('itemId').delete();

const thumbs = await graph.me.drives.getById('driveId').items.getById('itemId').delete();

Update drive item

Using the update() you update the current item

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

const update = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').items.getById('itemId').update({name: "New Name"});

const update = await graph.me.drives.getById('driveId').items.getById('itemId').update({name: "New Name"});

Move drive item

Using the move() you move the current item, and optionally update it

import { graph } from "@pnp/graph";
import "@pnp/graph/onedrive";

// Requires a parentReference to the new folder location
const move = await graph.users.getById('user@tenant.onmicrosoft.com').drives.getById('driveId').items.getById('itemId').move({ parentReference: { id: 'itemId'}}, {name: "New Name"});

const move = await graph.me.drives.getById('driveId').items.getById('itemId').move({ parentReference: { id: 'itemId'}}, {name: "New Name"});