-
Notifications
You must be signed in to change notification settings - Fork 233
Configuration
The library is designed to work in most scenarios without additional configuration - however there are some cases where it is unavoidable. This article covers those cases and how to perform the necessary setup.
If you have an unpatched version of SharePoint 2013 as discussed in this blog post you will need to setup the library to use verbose OData mode. This is done using the global headers entry. This will apply that header value to all requests sent to the server and avoid the need to configure it on each request.
pnp.setup({
headers: {
"Accept": "application/json;odata=verbose",
}
});
Similarly you can set any other headers globally that will be set with all requests. This can be helpful for authentication scenarios or custom security filtering in your farm.
pnp.setup({
headers: {
"X-Custom": "My Custom Header Value",
"X-Custom2": "My Custom2 Header Value",
}
});
You can also set the cache settings globally that will be applied to all requests built using the usingCaching method in the chain. See the article on caching for more details on using caching.
pnp.setup({
defaultCachingStore: "session", // or "local"
defaultCachingTimeoutSeconds: 30,
globalCacheDisable: false // or true to disable caching in case of debugging/testing
});
There are two scenarios requiring you to set a different fetch client, working in nodejs and deploying to an add-in web. Both ways make use of the fetchClientFactory property in the configuration object. This should be set using a factory method taking no parameters and returning a valid instance which implements HttpClientImpl. You can also create a Custom HttpClientImpl should the need arise and return it from the factory.
When operating directly from Node you will need to setup the node fetch client.
import { setup, Web, NodeFetchClient } from "sp-pnp-js";
setup({
fetchClientFactory: () => {
return new NodeFetchClient("{site url}", "{client id}", "{client secret}");
}
});
let w = new Web("{site url}");
w.select("Title").get().then(w => {
console.log(w);
});
When operating in the context of an add-in web you will need to setup the SPRequestExecutorClient.
import { setup, sp, SPRequestExecutorClient } from "sp-pnp-js";
setup({
fetchClientFactory: () => {
return new SPRequestExecutorClient();
}
});
let w = sp.crossDomainWeb("{add-in web url}", "{host web url}");
w.select("Title").get().then(w => {
console.log(w);
});
export interface LibraryConfiguration {
/**
* Any headers to apply to all requests
*/
headers?: TypedHash<string>;
/**
* Allows caching to be global disabled, default: false
*/
globalCacheDisable?: boolean;
/**
* Defines the default store used by the usingCaching method, default: session
*/
defaultCachingStore?: "session" | "local";
/**
* Defines the default timeout in seconds used by the usingCaching method, default 30
*/
defaultCachingTimeoutSeconds?: number;
/**
* Defines a factory method used to create fetch clients
*/
fetchClientFactory?: () => HttpClientImpl;
}
Sharing is caring!