Skip to content

@pnp/graph/contacts

The ability to manage contacts and folders in Outlook is a capability introduced in version 1.2.2 of @pnp/graph. Through the methods described you can add and edit both contacts and folders in a users Outlook.

Get all of the Contacts

Using the contacts() you can get the users contacts from Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

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

const contacts2 = await graph.me.contacts();

Add a new Contact

Using the contacts.add() you can a add Contact to the users Outlook

import { graph } from "@pnp/graph";
import { EmailAddress } from "@microsoft/microsoft-graph-types";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const addedContact = await graph.users.getById('user@tenant.onmicrosoft.com').contacts.add('Pavel', 'Bansky', [<EmailAddress>{address: 'pavelb@fabrikam.onmicrosoft.com', name: 'Pavel Bansky' }], ['+1 732 555 0102']);

const addedContact2 = await graph.me.contacts.add('Pavel', 'Bansky', [<EmailAddress>{address: 'pavelb@fabrikam.onmicrosoft.com', name: 'Pavel Bansky' }], ['+1 732 555 0102']);

Get Contact by Id

Using the contacts.getById() you can get one of the users Contacts in Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

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

const contact2 = await graph.me.contacts.getById('userId')();

Delete a Contact

Using the delete you can remove one of the users Contacts in Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const delContact = await graph.users.getById('user@tenant.onmicrosoft.com').contacts.getById('userId').delete();

const delContact2 = await graph.me.contacts.getById('userId').delete();

Update a Contact

Using the update you can update one of the users Contacts in Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const updContact = await graph.users.getById('user@tenant.onmicrosoft.com').contacts.getById('userId').update({birthday: "1986-05-30" });

const updContact2 = await graph.me.contacts.getById('userId').update({birthday: "1986-05-30" });

Get all of the Contact Folders

Using the contactFolders.get() you can get the users Contact Folders from Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

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

const contactFolders2 = await graph.me.contactFolders();

Add a new Contact Folder

Using the contactFolders.add() you can a add Contact Folder to the users Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const addedContactFolder = await graph.users.getById('user@tenant.onmicrosoft.com').contactFolders.add('displayName', '<ParentFolderId>');

const addedContactFolder2 = await graph.me.contactFolders.add('displayName', '<ParentFolderId>');

Get Contact Folder by Id

Using the contactFolders.getById() you can get one of the users Contact Folders in Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

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

const contactFolder2 = await graph.me.contactFolders.getById('folderId')();

Delete a Contact Folder

Using the delete you can remove one of the users Contact Folders in Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const delContactFolder = await graph.users.getById('user@tenant.onmicrosoft.com').contactFolders.getById('folderId').delete();

const delContactFolder2 = await graph.me.contactFolders.getById('folderId').delete();

Update a Contact Folder

Using the update you can update one of the users Contact Folders in Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const updContactFolder = await graph.users.getById('user@tenant.onmicrosoft.com').contactFolders.getById('userId').update({displayName: "value" });

const updContactFolder2 = await graph.me.contactFolders.getById('userId').update({displayName: "value" });

Get all of the Contacts from the Contact Folder

Using the contacts.get() in the Contact Folder gets the users Contact from the folder.

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const contactsInContactFolder = await graph.users.getById('user@tenant.onmicrosoft.com').contactFolders.getById('folderId').contacts();

const contactsInContactFolder2 = await graph.me.contactFolders.getById('folderId').contacts();

Get Child Folders of the Contact Folder

Using the childFolders.get() you can get the Child Folders of the current Contact Folder from Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const childFolders = await graph.users.getById('user@tenant.onmicrosoft.com').contactFolders.getById('<id>').childFolders();

const childFolders2 = await graph.me.contactFolders.getById('<id>').childFolders();

Add a new Child Folder

Using the childFolders.add() you can a add Child Folder in a Contact Folder

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const addedChildFolder = await graph.users.getById('user@tenant.onmicrosoft.com').contactFolders.getById('<id>').childFolders.add('displayName', '<ParentFolderId>');

const addedChildFolder2 = await graph.me.contactFolders.getById('<id>').childFolders.add('displayName', '<ParentFolderId>');

Get Child Folder by Id

Using the childFolders.getById() you can get one of the users Child Folders in Outlook

import { graph } from "@pnp/graph";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const childFolder = await graph.users.getById('user@tenant.onmicrosoft.com').contactFolders.getById('<id>').childFolders.getById('folderId')();

const childFolder2 = await graph.me.contactFolders.getById('<id>').childFolders.getById('folderId')();

Add Contact in Child Folder of Contact Folder

Using contacts.add in the Child Folder of a Contact Folder, adds a new Contact to that folder

import { graph } from "@pnp/graph";
import { EmailAddress } from "./@microsoft/microsoft-graph-types";
import "@pnp/graph/users"
import "@pnp/graph/contacts"

const addedContact = await graph.users.getById('user@tenant.onmicrosoft.com').contactFolders.getById('<id>').childFolders.getById('folderId').contacts.add('Pavel', 'Bansky', [<EmailAddress>{address: 'pavelb@fabrikam.onmicrosoft.com', name: 'Pavel Bansky' }], ['+1 732 555 0102']);

const addedContact2 = await graph.me.contactFolders.getById('<id>').childFolders.getById('folderId').contacts.add('Pavel', 'Bansky', [<EmailAddress>{address: 'pavelb@fabrikam.onmicrosoft.com', name: 'Pavel Bansky' }], ['+1 732 555 0102']);
spacer