Forum Discussion
Using PnP JS Core: How do I get a list of files from the "Pages" folder of an "Enterprise Wiki"
- Jan 07, 2017
Hi Alan,
First, 'Pages' is not a folder, it's a document library.
You can query a doc lib by multiple available methods in SharePoint REST API and PnP JS Core REST wrapper, like:
// Get by title
$pnp.sp.web.lists.getByTitle('Pages').items.get().then(function(item) { console.log(item); });or
// Get by list url is more preferable way as list renaming won't crash the logic
$pnp.sp.web.getList(_spPageContextInfo.webServerRelativeUrl + '/Pages').items.get().then(function(item) { console.log(item); });or by querying the real folder objects with `GetFolderByServerRelativeUrl` method like:
$pnp.sp.web
.getFolderByServerRelativeUrl(_spPageContextInfo.webServerRelativeUrl + '/Pages') // Here comes a folder/subfolder path .files .expand('Files/ListItemAllFields') // For Metadata extraction .select('Title,Name') // Fields to retrieve .get().then(function(item) { console.log(item); });Cheers
PnP JS Core is mostly the wrapper around SharePoint REST APIs with syntax sugar and helpers it doesn't cover all the API.
I would recommend learning SharePoint REST API reference and PnP JS Core simultaneously. PnP JS Core has nice chaining logic and based on promises. During chaining helpers methods it constructs REST endpoint URL and builds requests data parameters (in POST calls), then initiates a promise.
I found myself learning such open source libraries which evolve faster than the documentation with the use of "debug-first learning" by logging in the console intermediate objects and results and checking the available options, like this:
var requestObject = $pnp.sp.web.getFolderByServerRelativeUrl(_spPageContextInfo.webServerRelativeUrl + '/Pages')
.files.expand('Files/ListItemAllFields').select('Title,Name');
console.log('Valuable methods and props', requestObject);
console.log('Url endpoint', requestObject.toUrl());
var promise = requestObject.get();
promise.then(function(data) {
console.log(data);
});
and diving in the sources and issues history.
It's not so scary like it seems. E.g., in the API reference documentation you might have found something like `GetFolderByServerRelativeUrl` method, it probably can be already implemented in the library (we know it is), the easiest way to check it is to search on GitHub repo. In most cases it's rather straightforward to figure out that: 1) implemented or not, 2) how to use a specific option. Also, issues area is opened for issues and questions if the previously described learning pattern can't help.