SOLVED

Using PnP JS Core: How do I get a list of files from the "Pages" folder of an "Enterprise Wiki"

%3CLINGO-SUB%20id%3D%22lingo-sub-37861%22%20slang%3D%22en-US%22%3EUsing%20PnP%20JS%20Core%3A%20How%20do%20I%20get%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%22Enterprise%20Wiki%22%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-37861%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUsing%20PnP%20JS%20Core%2C%20How%20would%20I%20go%20about%20getting%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%22Enterprise%20Wiki%22%20and%20then%20getting%20the%20properties%20associated%20with%20each%20file%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEventually%20I%20want%20to%20get%20all%20the%20properties%20associated%20with%20each%20file.%20In%20particular%20I'm%20interested%20in%20Title%2C%20ServerRelativePath%20and%20ServerRelativeUrl%20for%20now.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20case%20it%20helps%2C%20we%20use%20an%20%22Enterprise%20Wiki%22%20with%20folders.%20For%20each%20folder%20I%20want%20to%20generate%20a%20table%20of%20contents.%20So%20for%20a%20given%20folder%20I%20would%20want%20to%20filter%20by%20the%20url%20(ServerRelatieUrl%3F)%20for%20only%20files%20in%20that%20folder.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38193%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20JS%20Core%3A%20How%20do%20I%20get%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%22En%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38193%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F29128%22%20target%3D%22_blank%22%3E%40Andrew%20Koltyakov%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20your%20help%20and%20suggestions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38157%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20JS%20Core%3A%20How%20do%20I%20get%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%22En%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38157%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F25364%22%20target%3D%22_blank%22%3E%40Alan%20Trafford%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPnP%20JS%20Core%20is%20mostly%20the%20wrapper%20around%20SharePoint%20REST%20APIs%20with%20syntax%20sugar%20and%20helpers%20it%20doesn't%20cover%20all%20the%20API.%3C%2FP%3E%3CP%3EI%20would%20recommend%20learning%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Foffice%2Fjj860569.aspx%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3ESharePoint%20REST%20API%20reference%3C%2FA%3E%26nbsp%3Band%20PnP%20JS%20Core%20simultaneously.%20Pn%3CSPAN%3EP%20JS%20Core%20has%20nice%20chaining%20logic%20and%20based%20on%20promises.%20During%20chaining%20helpers%20methods%20it%20constructs%20REST%20endpoint%20URL%20and%20builds%20requests%20data%20parameters%20(in%20POST%20calls)%2C%20then%20initiates%20a%20promise.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%20found%20myself%20learning%20such%20open%20source%20libraries%20which%20evolve%26nbsp%3Bfaster%20than%20the%20documentation%20with%20the%26nbsp%3Buse%20of%20%22debug-first%20learning%22%20by%20logging%26nbsp%3Bin%20the%20console%20intermediate%20objects%20and%20results%20and%20checking%20the%20available%20options%2C%20like%20this%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Evar%20requestObject%20%3D%20%24pnp.sp.web.getFolderByServerRelativeUrl(_spPageContextInfo.webServerRelativeUrl%20%2B%20'%2FPages')%0A%20%20%20.files.expand('Files%2FListItemAllFields').select('Title%2CName')%3B%0A%0Aconsole.log('Valuable%20methods%20and%20props'%2C%20requestObject)%3B%0Aconsole.log('Url%20endpoint'%2C%20requestObject.toUrl())%3B%0A%0Avar%20promise%20%3D%20requestObject.get()%3B%0A%0Apromise.then(function(data)%20%7B%0A%20%20%20console.log(data)%3B%0A%7D)%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3Eand%20diving%20in%20the%20sources%20and%20issues%20history.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EIt's%20not%20so%20scary%20like%20it%20seems.%20E.g.%2C%20in%20the%20API%20reference%20documentation%20you%20might%20have%20found%20something%20like%20%60GetFolderByServerRelativeUrl%60%20method%2C%20it%20probably%20can%20be%20already%20implemented%20in%20the%20library%20(we%20know%20it%20is)%2C%20the%20easiest%20way%20to%20check%20it%20is%20to%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-JS-Core%2Fsearch%3Futf8%3D%25E2%259C%2593%26amp%3Bq%3DGetFolderByServerRelativeUrl%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Esearch%3C%2FA%3E%20on%20GitHub%20repo.%20In%20most%20cases%20it's%20rather%20straightforward%20to%20figure%20out%20that%3A%201)%20implemented%20or%20not%2C%202)%20how%20to%20use%20a%20specific%20option.%20Also%2C%20issues%20area%20is%20opened%20for%20issues%20and%20questions%20if%20the%20previously%20described%20learning%20pattern%20can't%20help.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38152%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20JS%20Core%3A%20How%20do%20I%20get%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%22En%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38152%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F29128%22%20target%3D%22_blank%22%3E%40Andrew%20Koltyakov%3C%2FA%3E%2C%20thanks%20for%20your%20help%2C%20it's%20much%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELooks%20like%20I've%20some%20learning%20to%20do!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20visiting%20the%20PnP%20JS%20Core%20API%20pages%20regularly%2C%20but%20they%20lack%20examples.%20I'm%20sure%20this%20will%20make%20more%20sense%20in%20a%20few%20months.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20recommend%20any%20resources%20(articles%2C%20examples%2C%20references)%20for%20learning%20about%20searching%2C%20even%20if%20it's%20just%20terms%20I%20should%20search%20for%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38149%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20JS%20Core%3A%20How%20do%20I%20get%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%22En%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38149%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F25364%22%20target%3D%22_blank%22%3E%40Alan%20Trafford%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20could%20be%20at%20least%202%20strategies%20with%20your%20case%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3EWhen%20a%20number%20of%20articles%20(with%20folders)%26nbsp%3Bis%20relatively%20small%20(less%20than%205000)%20-%20it's%20possible%20to%20use%20one%20single%20query%20for%20getting%20all%20the%20structure%20you%20have%20in%20a%20document%20library%20recursively%20then%20with%20the%20use%20of%20FileDirRef%20build%20your%20custom%20navigation%20UI%3B%3C%2FLI%3E%3CLI%3EWhen%20a%20number%20of%20the%20wiki%20pages%20is%20unpredictably%26nbsp%3Blarge%2C%20then%20an%20on-demand%26nbsp%3Bquery%20per%20folder's%20files%20and%20folders%20during%20a%20category%20(folder)%20drill%20down%20UI%20interaction.%3C%2FLI%3E%3C%2FOL%3E%3CP%3EIn%20case%20of%20the%26nbsp%3Bfirst%20scenario%2C%26nbsp%3BI%20would%20recommend%20using%20%60%24pnp.sp.web.getList(_spPageContextInfo.webServerRelativeUrl%20%2B%20'%2FPages').items%60%20...%20with%20the%20conditions%20and%20so%20on.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20the%20second%20one%2C%20imo%2C%20it's%20better%26nbsp%3Bto%20deal%20with%20%60%24pnp.sp.web.getFolderByServerRelativeUrl('%5B...path%20to%20folder...%5D').files%60%20and%20%60%24pnp.sp.web.getFolderByServerRelativeUrl('%5B...path%20to%20folder...%5D').folders%60%20in%20a%20batch%20request.%20Or%20using%20plain%20REST%20call%20%60%2F_api%2FWeb%2FGetFolderByServerRelativeUrl(%40FolderServerRelativeUrl)%3F%24expand%3DFolders%2CFiles%2CFolders%2FListItemAllFields%2CFiles%2FListItemAllFields%24select%3D...%60.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUsing%20search%20API%20also%20can%20be%20a%20good%20idea.%20A%20lot%20depends%20on%20usage%20scenarios%20and%20data%20amount.%3C%2FP%3E%3CP%3EFor%20instance%2C%20I%20would%20decide%20to%26nbsp%3Bshow%20only%20categories%26nbsp%3Bstructure%20in%20the%20wiki%20menu.%20In%20the%26nbsp%3Bcase%20of%20folders%2C%20it%20can%20be%20easily%20one%20single%20cached%20request%20to%20a%20list's%20folders.%20In%20some%20more%20advanced%20cases%20you%20would%20need%20to%20apply%20multiple%20categories%20to%20the%20wiki%20article%2C%20then%26nbsp%3Bmanaged%20metadata%20is%20better%20(with%20MMD%20you%20can%20even%20use%20OOTB%20tree%20MMD%20navigation).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38146%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20JS%20Core%3A%20How%20do%20I%20get%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%22En%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38146%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F29128%22%20target%3D%22_blank%22%3E%40Andrew%20Koltyakov%3C%2FA%3E%2C%20thanks%20for%20the%20reply.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20some%20investigation%20I%20figured%20out%20that%20'Pages'%20was%20a%20list%20and%20not%20a%20folder.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20our%20%22Enterprise%20Wiki%22%20we%20have%20used%20folders%20to%20create%20a%20hierarchy.%20Your%20last%20code%20snippet%20worked%20great%20for%20getting%20a%20list%20of%20files%2C%20but%20did%20not%20return%20folders.%20I%20modified%20it%20slightly%20(replaced%20.files%20with%20.folders)%20and%20was%20able%20to%20get%20folder%20information.%20So%20it%20now%20looks%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Evar%20files%3B%0A%24pnp.sp.web%0A%20%20%20.getFolderByServerRelativeUrl(_spPageContextInfo.webServerRelativeUrl%20%2B%20'%2FPages')%20%2F%2F%20Here%20comes%20a%20folder%2Fsubfolder%20path%0A%20%20%20.files%0A%20%20%20.expand('Files%2FListItemAllFields')%20%2F%2F%20For%20Metadata%20extraction%0A%20%20%20.select('Title%2CName')%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Fields%20to%20retrieve%0A%20%20%20.get()%3CBR%20%2F%3E%20%20%20.then(function(item)%20%7B%0A%20%20%20%20%20%20files%20%3D%20item%3B%0A%20%20%20%7D)%3B%0A%0Avar%20folders%3B%0A%24pnp.sp.web%0A%20%20%20.getFolderByServerRelativeUrl(_spPageContextInfo.webServerRelativeUrl%20%2B%20'%2FPages')%20%2F%2F%20Here%20comes%20a%20folder%2Fsubfolder%20path%0A%20%20%20.files%0A%20%20%20.expand('Folders%2FListItemAllFields')%20%2F%2F%20For%20Metadata%20extraction%0A%20%20%20.select('Title%2CName')%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Fields%20to%20retrieve%0A%20%20%20.get()%3CBR%20%2F%3E%20%20%20.then(function(item)%20%7B%0A%20%20%20%20%20folders%20%3D%20item%3B%0A%20%20%20%7D)%3B%3C%2FPRE%3E%3CP%3EIs%20this%20OK%20or%20is%20there%20a%20better%20way%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38141%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20JS%20Core%3A%20How%20do%20I%20get%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%22En%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38141%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Alan%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%2C%20'Pages'%20is%20not%20a%20folder%2C%20it's%20a%20document%20library.%3C%2FP%3E%3CP%3EYou%20can%20query%20a%20doc%20lib%20by%20multiple%20available%20methods%20in%20SharePoint%20REST%20API%20and%20PnP%20JS%20Core%20REST%20wrapper%2C%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%2F%2F%20Get%20by%20title%3CBR%20%2F%3E%24pnp.sp.web.lists.getByTitle('Pages').items.get().then(function(item)%20%7B%0A%20%20%20console.log(item)%3B%0A%7D)%3B%3C%2FPRE%3E%3CP%3Eor%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%2F%2F%20Get%20by%20list%20url%20is%20more%20preferable%20way%20as%20list%20renaming%20won't%20crash%20the%20logic%3CBR%20%2F%3E%24pnp.sp.web.getList(_spPageContextInfo.webServerRelativeUrl%20%2B%20'%2FPages').items.get().then(function(item)%20%7B%0A%20%20%20console.log(item)%3B%0A%7D)%3B%3C%2FPRE%3E%3CP%3Eor%20by%20querying%20the%20real%20folder%20objects%20with%20%60%3CSPAN%3EGetFolderByServerRelativeUrl%3C%2FSPAN%3E%60%20method%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24pnp.sp.web%3CBR%20%2F%3E%20%20%20.getFolderByServerRelativeUrl(_spPageContextInfo.webServerRelativeUrl%20%2B%20'%2FPages')%20%2F%2F%20Here%20comes%20a%20folder%2Fsubfolder%20path%0A%20%20%20.files%0A%20%20%20.expand('Files%2FListItemAllFields')%20%2F%2F%20For%20Metadata%20extraction%0A%20%20%20.select('Title%2CName')%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Fields%20to%20retrieve%0A%20%20%20.get().then(function(item)%20%7B%0A%20%20%20%20%20%20console.log(item)%3B%0A%20%20%20%7D)%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38139%22%20slang%3D%22en-US%22%3ERe%3A%20RE%3A%20Using%20PnP%20JS%20Core%3A%20How%20do%20I%20get%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%26amp%3Bquo%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38139%22%20slang%3D%22en-US%22%3E%3CP%3E%40Deleted%2C%20thanks%20for%20the%20reply.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20possible%2C%20I%20really%20want%20to%20do%20this%20in%20JavaScript.%20As%20you%20say%2C%20I%20could%20create%20an%20add-in%20and%20have%20that%20do%20the%20work.%20I'm%20new%20to%20SharePoint%20development%20it%20looks%20like%20the%20JavaScript%20way%20is%20simpler%2C%20in%20that%20I%20don't%20have%20to%20host%20it%20externally.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%2C%20if%20this%20was%20successful%2C%20a%20follow%20on%20project%20would%20be%20to%20create%20an%20enhanced%20left%20hand%20menu.%20It%20seems%20that%20the%20easiest%20way%20to%20do%20this%2C%20without%20creating%2Fediting%20templates%20or%20page%20layouts%2C%20is%20with%20JavaScript%20embedding.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20using%20the%20judgement%2C%20feel%20free%20to%20correct%20me%2C%20that%20add-ins%20are%20for%20users%20to%20add%20to%20pages%20they%20create.%20Whereas%2C%20JavaScript%20embedding%20can%20be%20used%20to%20make%20changes%20to%20all%20pages.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBTW%20I%20never%20used%20Azure%2C%20have%20beginners%20skills%20in%20JavaScript%2C%20average%20skills%20in%20C%23.%20I've%20spent%20most%20of%20the%20last%205%20years%20or%20so%20doing%20PowerShell%20and%20Transact-SQL.%20I%20have%20good%20skills%20on%20both%20of%20these.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38137%22%20slang%3D%22en-US%22%3ERE%3A%20Using%20PnP%20JS%20Core%3A%20How%20do%20I%20get%20a%20list%20of%20files%20from%20the%20%22Pages%22%20folder%20of%20an%20%22Enterprise%20Wiki%22%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38137%22%20slang%3D%22en-US%22%3EWould%20you%20not%20just%20use%20csom%20for%20that%20in%20combination%20with%20JS%20Core%3F%3C%2FLINGO-BODY%3E
Contributor

Hi

 

Using PnP JS Core, How would I go about getting a list of files from the "Pages" folder of an "Enterprise Wiki" and then getting the properties associated with each file?

 

Eventually I want to get all the properties associated with each file. In particular I'm interested in Title, ServerRelativePath and ServerRelativeUrl for now.

 

In case it helps, we use an "Enterprise Wiki" with folders. For each folder I want to generate a table of contents. So for a given folder I would want to filter by the url (ServerRelatieUrl?) for only files in that folder.

 

Thanks.

8 Replies
Would you not just use csom for that in combination with JS Core?

@Deleted, thanks for the reply.

 

If possible, I really want to do this in JavaScript. As you say, I could create an add-in and have that do the work. I'm new to SharePoint development it looks like the JavaScript way is simpler, in that I don't have to host it externally.

 

Also, if this was successful, a follow on project would be to create an enhanced left hand menu. It seems that the easiest way to do this, without creating/editing templates or page layouts, is with JavaScript embedding.

 

I'm using the judgement, feel free to correct me, that add-ins are for users to add to pages they create. Whereas, JavaScript embedding can be used to make changes to all pages.

 

BTW I never used Azure, have beginners skills in JavaScript, average skills in C#. I've spent most of the last 5 years or so doing PowerShell and Transact-SQL. I have good skills on both of these.

best response confirmed by Alan Trafford (Contributor)
Solution

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

@Andrew Koltyakov, thanks for the reply.

 

After some investigation I figured out that 'Pages' was a list and not a folder.

 

In our "Enterprise Wiki" we have used folders to create a hierarchy. Your last code snippet worked great for getting a list of files, but did not return folders. I modified it slightly (replaced .files with .folders) and was able to get folder information. So it now looks like this:

 

 

var files;
$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) { files = item; }); var folders; $pnp.sp.web .getFolderByServerRelativeUrl(_spPageContextInfo.webServerRelativeUrl + '/Pages') // Here comes a folder/subfolder path .files .expand('Folders/ListItemAllFields') // For Metadata extraction .select('Title,Name') // Fields to retrieve .get()
.then(function(item) { folders = item; });

Is this OK or is there a better way?

 

Thanks.

 

Hi @Alan Trafford,

 

There could be at least 2 strategies with your case:

 

  1. When a number of articles (with folders) is relatively small (less than 5000) - it's possible to use one single query for getting all the structure you have in a document library recursively then with the use of FileDirRef build your custom navigation UI;
  2. When a number of the wiki pages is unpredictably large, then an on-demand query per folder's files and folders during a category (folder) drill down UI interaction.

In case of the first scenario, I would recommend using `$pnp.sp.web.getList(_spPageContextInfo.webServerRelativeUrl + '/Pages').items` ... with the conditions and so on.

 

With the second one, imo, it's better to deal with `$pnp.sp.web.getFolderByServerRelativeUrl('[...path to folder...]').files` and `$pnp.sp.web.getFolderByServerRelativeUrl('[...path to folder...]').folders` in a batch request. Or using plain REST call `/_api/Web/GetFolderByServerRelativeUrl(@FolderServerRelativeUrl)?$expand=Folders,Files,Folders/ListItemAllFields,Files/ListItemAllFields$select=...`.

 

Using search API also can be a good idea. A lot depends on usage scenarios and data amount.

For instance, I would decide to show only categories structure in the wiki menu. In the case of folders, it can be easily one single cached request to a list's folders. In some more advanced cases you would need to apply multiple categories to the wiki article, then managed metadata is better (with MMD you can even use OOTB tree MMD navigation).

@Andrew Koltyakov, thanks for your help, it's much appreciated.

 

Looks like I've some learning to do!

 

I'm visiting the PnP JS Core API pages regularly, but they lack examples. I'm sure this will make more sense in a few months.

 

Can you recommend any resources (articles, examples, references) for learning about searching, even if it's just terms I should search for?

 

Thanks.

 

 

@Alan Trafford,

 

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.

 

Hi @Andrew Koltyakov

 

Thanks for your help and suggestions.