import { ServiceKey , ServiceScope } from "@microsoft/sp-core-library" ; import { PageContext } from "@microsoft/sp-page-context" ; import { IFolderExplorerService } from "./IFolderExplorerService" ; import { IFolder } from "./IFolderExplorerService" ; import { sp , Web , List , FolderAddResult } from "@pnp/sp" ; export class FolderExplorerService implements IFolderExplorerService { public static readonly serviceKey : ServiceKey < IFolderExplorerService > = ServiceKey . create < IFolderExplorerService > ( 'SPFx:SPService' , FolderExplorerService ) ; constructor ( serviceScope ) { serviceScope . whenFinished ( ( ) => { const pageContext = serviceScope . consume ( PageContext . serviceKey ) ; sp . setup ( { sp : { baseUrl : pageContext . web . absoluteUrl } } ) ; } ) ; } param webAbsoluteUrl public GetDocumentLibraries = async ( webAbsoluteUrl ) : Promise < IFolder [ ] > => { return this . _getDocumentLibraries ( webAbsoluteUrl ) ; } ; param webAbsoluteUrl private _getDocumentLibraries = async ( webAbsoluteUrl ) : Promise < IFolder [ ] > => { let results = [ ] ; try { const web = new Web ( webAbsoluteUrl ) ; const libraries = await web . lists . filter ( 'BaseTemplate eq 101 and Hidden eq false' ) . expand ( 'RootFolder' ) . select ( 'Title' , 'RootFolder/ServerRelativeUrl' ) . orderBy ( 'Title' ) . get ( ) ; results = libraries . map ( ( library ) : IFolder => { return { Name : library . Title , ServerRelativeUrl : library . RootFolder . ServerRelativeUrl } ; } ) ; } catch ( error ) { console . error ( 'Error loading folders' , error ) ; } return results ; } ; param webAbsoluteUrl param folderRelativeUrl public GetFolders = async ( webAbsoluteUrl , folderRelativeUrl ) : Promise < IFolder [ ] > => { return this . _getFolders ( webAbsoluteUrl , folderRelativeUrl ) ; } ; param webAbsoluteUrl param folderRelativeUrl private _getFolders = async ( webAbsoluteUrl , folderRelativeUrl ) : Promise < IFolder [ ] > => { let results = [ ] ; try { const web = new Web ( webAbsoluteUrl ) ; folderRelativeUrl = folderRelativeUrl . replace ( /\'/ig , "''" ) ; let foldersResult = await web . getFolderByServerRelativePath ( encodeURIComponent ( folderRelativeUrl ) ) . folders . select ( 'Name' , 'ServerRelativeUrl' ) . orderBy ( 'Name' ) . get ( ) ; results = foldersResult . filter ( f => f . Name !== "Forms" ) ; } catch ( error ) { console . error ( 'Error loading folders' , error ) ; } return results ; } ; param webAbsoluteUrl param folderRelativeUrl param name public AddFolder = async ( webAbsoluteUrl , folderRelativeUrl , name ) : Promise < IFolder > => { return this . _addFolder ( webAbsoluteUrl , folderRelativeUrl , name ) ; } ; param webAbsoluteUrl param folderRelativeUrl param name private _addFolder = async ( webAbsoluteUrl , folderRelativeUrl , name ) : Promise < IFolder > => { let folder = null ; try { const web = new Web ( webAbsoluteUrl ) ; folderRelativeUrl = folderRelativeUrl . replace ( /\'/ig , "''" ) ; let folderAddResult = await web . getFolderByServerRelativePath ( encodeURIComponent ( folderRelativeUrl ) ) . folders . addUsingPath ( encodeURIComponent ( name ) ) ; if ( folderAddResult && folderAddResult . data ) { folder = { Name : folderAddResult . data . Name , ServerRelativeUrl : folderAddResult . data . ServerRelativeUrl } ; } } catch ( error ) { console . error ( 'Error adding folder' , error ) ; } return folder ; } ; }