Mastering SharePoint Search with PnPjs
In modern SharePoint development, efficiently fetching and managing content is crucial. While SharePoint REST API provides a direct way to query content, PnPjs offers a more developer-friendly and robust approach to interacting with SharePoint, including performing searches. In this blog, we’ll explore how to leverage PnPjs to perform search queries, filter results, and handle data effectively.
What is PnPjs?
PnPjs is a fluent JavaScript library that simplifies interactions with the SharePoint REST API. It provides a set of reusable components for working with lists, libraries, search, taxonomy, and more. With TypeScript support and clean syntax, it’s a favorite for SPFx (SharePoint Framework) developers.
Setting Up PnPjs
Before we dive into search, ensure your SPFx project is set up to use PnPjs:
Step 1. Create a New SPFx Project
yo @microsoft/sharepoint
Select the following options:
Web Part
React (for UI flexibility)
SharePoint Online only
This scaffolds a basic SPFx web part project.
Step 2. Install PnPjs
Inside your SPFx project folder, run:
npm install @pnp/sp @pnp/logging @pnp/common @pnp/odata --save
These packages cover core SharePoint operations with logging and OData support.
Step 3. Configure PnPjs
Open your web part src/webparts/<YourWebPart>/<YourWebPart>.ts and initialize PnPjs:
import { sp } from "@pnp/sp";
import { Web } from "@pnp/sp/webs";
import { spfi, SPFx } from "@pnp/sp";
export interface IMyWebPartProps {
description: string;
}
export default class MyWebPart extends BaseClientSideWebPart<IMyWebPartProps> {
private _sp: ReturnType<typeof spfi>;
public onInit(): Promise<void> {
return super.onInit().then(_ => {
// Initialize PnPjs with SPFx context using spfi factory
this._sp = spfi().using(SPFx(this.context));
});
}
}
This ensures that all subsequent PnPjs calls are aware of your SPFx context.
1. Performing a Search with PnPjs
This is the primary search API in PnPjs. It wraps the SharePoint Search REST endpoint ( _api/search/query ) and allows you to perform queries using KQL (Keyword Query Language).
Syntax
const results = await sp.search({
Querytext: "SharePoint",
RowLimit: 10,
StartRow: 0,
SelectProperties: ["Title", "Path", "Author"],
TrimDuplicates: true,
SortList: [{ Property: "Created", Direction: "Desc" }]
});
Key Parameters
| Parameter | Description |
|---|---|
Querytext | The search query in KQL. |
RowLimit | Max number of results to return. |
StartRow | Pagination start index (useful for paging). |
SelectProperties | Array of managed properties to include in results. |
TrimDuplicates | Remove duplicate results. |
SortList | Sort results by one or more managed properties. |
SourceId | GUID of a result source (like a custom search scope). |
RefinementFilters | Filters for refiners (like file type, author). |
Refiners | Array of refiners to include in the response (like FileType , Author ). |
Returns
The sp.search() method returns an object like:

Join the conversation! Your thoughts help the community grow.