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