PnPjs Documentation

repository·version-4·Indexed 21 days ago

https://github.com/pnp/pnpjs

A fluent, type-safe JavaScript API for interacting with SharePoint and Microsoft Graph REST APIs. Compatible with SharePoint Framework (SPFx), Node.js, and general web applications. Includes guidance on ESM configuration for Azure Functions v4, certificate-based authentication in Node.js, and creating optimized custom bundles using Webpack or Rollup.

Tokens
233.4K
Snippets
793
Records
900
Agent score
72%

What's inside PnPjs

  1. Overview of PnPjs

    version-4

    PnPjs is a fluent JavaScript API designed for consuming SharePoint and Microsoft Graph REST APIs in a type-safe manner. It is an open-source initiative that complements official Microsoft SDKs. It is compatible with various environments, including:

    • SharePoint Framework (SPFx)
    • Node.js
    • Standard JavaScript projects
  2. Overview of PnPjs packages

    version-4

    PnPjs is a collection of fluent, type-safe libraries designed to consume SharePoint, Microsoft Graph, and Office 365 REST APIs. It is compatible with SharePoint Framework (SPFx), Node.js, and general JavaScript projects.

    All packages are published as a set and depend on their peers within the @pnp scope. Key packages include:

    • @pnp/core: Shared functionality across all PnP libraries.
    • @pnp/queryable: Shared query functionality and base classes.
    • @pnp/sp: Fluent API for SharePoint REST.
    • @pnp/graph: Fluent API for Microsoft Graph.
    • @pnp/sp-admin: Fluent API for M365 Tenant admin methods.
    • @pnp/nodejs: Enables PnP libraries to function within Node.js environments.
    • @pnp/logging: A lightweight, subscribable logging framework.
    • @pnp/msaljsclient: An MSAL wrapper for authentication.
    • @pnp/azidjsclient: An Azure Identity wrapper for authentication.

    If you are migrating from version 2, refer to the transition guide in the repository.

  3. Manage SharePoint site users with @pnp/sp/site-users

    version-4
    The @pnp/sp/site-users module provides methods to retrieve, create, update, and remove users within a SharePoint site collection. To use these methods, you must import the core @pnp/sp/webs module and the specific site users module (e.g., @pnp/sp/site-users/web).
  4. Use the @pnp/sp/social API to track followed content

    version-4

    The @pnp/sp/social module allows you to manage and retrieve social interactions in SharePoint, such as following sites, people, documents, or tags.

    Important Note: Most social methods require the context of a logged-in user and will not work with app-only permissions.

    import { spfi } from "@pnp/sp";
    import "@pnp/sp/social";
    
    const sp = spfi(...);
    // Access social features via sp.social
  5. Manage column defaults in @pnp/sp/column-defaults

    version-4
    The @pnp/sp/column-defaults sub-module allows you to manage default column values for specific folders or an entire document library. This is useful for ensuring new items created within specific locations automatically inherit certain metadata.
  6. Interact with Microsoft Graph compliance and privacy APIs

    version-4

    The @pnp/graph/compliance module provides access to Microsoft Graph compliance and privacy APIs, specifically for managing Subject Rights Requests (SRR) within Microsoft Purview. To use these features, you must import the compliance module alongside @pnp/graph to extend the graphfi instance.

    import { graphfi } from "@pnp/graph";
    import "@pnp/graph/compliance";
    
    const graph = graphfi(...);
  7. Manage Microsoft Graph Mail Rules

    version-4
    PnPjs provides a fluent API to manage IMessageRule and IMessageRules via Microsoft Graph. You can retrieve, add, update, and delete message rules for specific mail folders (such as the inbox) using the @pnp/graph package. Ensure you import @pnp/graph/users and @pnp/graph/mail to access the necessary mail folder and rule endpoints.
  8. Use Aliased Parameters in @pnp/sp to avoid URL length limits

    version-4

    When working with long file or folder paths in @pnp/sp, you may hit SharePoint URL length limits. You can bypass this by using Aliased Parameters. This feature allows you to replace long strings with short labels in the URL path, while the actual values are moved into the query string.

    Syntax

    To create an alias, use the following pattern within your string: !@{label name}::{value}

    Rules for Aliases:

    • Prefix: You must prepend the string with an exclamation mark (!) to trigger the replacement.
    • Separator: Use a double colon (::) between the label and the value.
    • Label Naming: Labels must start with an @ followed by a letter (e.g., @p1, @p2).
    • Uniqueness: You are responsible for ensuring that aliases do not conflict (e.g., use @p1 and @p2 for different parameters in the same query).

    How it works

    When the request is generated, the path segment is replaced by the label (e.g., @p1), and the original value is appended to the query string as a parameter (e.g., ?@p1='/your/long/path').

    // Pattern: !@{label name}::{value}
    // Example: 
    const query = sp.web.getFolderByServerRelativeUrl("!@p1::/sites/dev/Shared Documents/").files.select("Title").top(3);
    
    // Resulting Request URL structure:
    // _api/web/getFolderByServerRelativeUrl(@p1)/files?@p1='/sites/dev/Shared Documents/'&$select=Title&$top=3
  9. Use local-module-resolver for ESM module loading

    version-4
    When running in ESM (ECMAScript Modules) mode, specifically when using the hooks model, the esm.ts resolver is used to ensure the built module output is loaded correctly. This is a custom resolver designed to handle the specific path resolution requirements of the built PnPjs modules in an ESM environment.
  10. Manage Microsoft Graph Subscriptions with @pnp/graph/subscriptions

    version-4

    The @pnp/graph/subscriptions module allows client applications to receive notifications about data changes in Microsoft Graph. This capability was introduced in version 1.2.9 of @pnp/graph.

    Supported resources for subscriptions include:

    • Mail, events, and contacts from Outlook.
    • Conversations from Office Groups.
    • Drive root items from OneDrive.
    • Users and Groups from Azure Active Directory.
    • Alerts from the Microsoft Graph Security API.
  11. Manage Web Navigation (Quick Launch and Top Navigation)

    version-4

    PnPjs provides access to SharePoint web navigation through two main properties on the web.navigation object:

    1. topNavigationBar
    2. quicklaunch

    Both properties expose the same set of methods for managing navigation nodes. To use these, you must import @pnp/sp/webs and @pnp/sp/navigation.

    import { spfi } from "@pnp/sp";
    import "@pnp/sp/webs";
    import "@pnp/sp/navigation";
    
    const sp = spfi(...);
    
    // Get references to the navigation objects
    const top = await sp.web.navigation.topNavigationBar();
    const quick = await sp.web.navigation.quicklaunch();
  12. Understand Observer Inheritance in Timelines

    version-4

    Timelines created from other timelines (such as those generated by the sp and graph libraries) inherit all observers from their parent. Observers added to a parent apply to all children.

    Important: Inheritance Breaking Inheritance is broken as soon as you modify the set of observers on a child using subscription methods (like append, prepend, or replace).

    • Once inheritance is broken, changes to the parent no longer affect that child.
    • Changes to a child never affect the parent.
    • This behavior applies to all moments on any timeline.

    Example of inheritance and breaking:

    const sp = new spfi().using(...lots of behaviors);
    
    // web inherits all observers from "sp"
    const web = sp.web;
    
    // Inheritance is now broken for 'web'. 
    // 'web' has its own observers and no longer tracks changes to 'sp'.
    web.on.log(...);
    
    // web2 is a fresh instance from 'sp', so it still inherits from 'sp'.
    const web2 = sp.web;
    
    // 'list' inherits from 'web' (including the 'log' observer added to 'web').
    const list = web.lists.getById("");