π SharePoint REST API Proxy for local Front-end development tool-chains
Allows performing API calls to local Express application with forwarding the queries to a remote SharePoint instance.
Original concept of the proxy was created to show how it could be easy to implements real world data communications for SharePoint Framework local serve mode during web parts debug without deployment to SharePoint tenant. Now the tool is used with multiple teams for modern front-end solutions rapid development.
In a nutshell
Supports SPFx and PnP JS
Supported SharePoint versions
SharePoint Online
SharePoint On-Prem (2019, 2016, 2013)
SharePoint On-Prem 2010 (limited support)
Node.js version
>= 10.19
Development paradigms
SPA development (Angular, React, Vue.js, etc.) in serve mode against real data for On-Prem and Online
6. Navigate to http://localhost:8080 (or whatever in settings)
7. Ajax REST calls as if you were in SharePoint site page context:
REST Client Example
8. Tests.
Tests Example
Webpack Dev Server
TypeScript support
In early days of sp-rest-proxy, the library was written in ES6 and used module.exports which was kept after migrating to TypeScript later on for the backward compatibility reasons.
In TypeScript, it's better to import the lib from sp-rest-proxy/dist/RestProxy to get advantages of types:
Authentication settings
The proxy provides wizard-like approach for building and managing config files for node-sp-auth (Node.js to SharePoint unattended http authentication).
SharePoint Online:
User credentials (SAML/ADFS)
Add-In Only permissions
On-Demand authentication (using Electron popup)
SharePoint 2019, 2016, 2013:
User credentials (NTLM, NTLM v2)
ADFS user credentials
Form-based authentication (FBA)
Form-based authentication (Forefront TMG)
Add-In Only permissions
On-Demand authentication (using Electron popup)
SharePoint 2010:
User credentials (NTLM, NTMLv2)
Form-based authentication (FBA)
Form-based authentication (Forefront TMG)
For more information please check node-sp-auth credential options and wiki pages. Auth settings are stored inside ./config/private.json.
PnPjs
sp-rest-proxy works with PnPjs (check out brief notice how to configure).
PnP JS + sp-rest-proxy
Load page context helper
sp-rest-proxy includes helper method for configuring page context - loadPageContext.
JSOM (SharePoint JavaScript Object Model)
JSOM can be used in local development mode with sp-rest-proxy with some additional setup.
The local development workbench page should contain JSOM init scripts:
import { loadPageContext } from 'sp-rest-proxy/dist/utils/env';
import { Web } from '@pnp/sp';
// loadPageContext - gets correct URL in localhost and SP environments
loadPageContext().then(async () => {
// In both localhost and published to SharePoint page
// `_spPageContextInfo` will contain correct info for vital props
// PnPjs's Web object should be created in the following way
const web = new Web(_spPageContextInfo.webAbsoluteUrl);
// Then goes ordinary PnPjs code
const batch = web.createBatch();
const list = web.getList(`${_spPageContextInfo.webServerRelativeUrl}/List/ListName`);
const entityName = await list.getListItemEntityTypeFullName();
[1, 2, 3, 4].forEach((el) => {
list.items.inBatch(batch).add({
Title: `${el}`
}, entityName);
});
await batch.execute();
console.log('Done');
}).catch(console.warn);