import { SPHttpClientResponse } from '@microsoft/sp-http' ; import { SPHttpClient } from '@microsoft/sp-http' ; import { Environment , EnvironmentType } from '@microsoft/sp-core-library' ; import { BaseComponentContext } from '@microsoft/sp-component-base' ; import { ISPLists , IPropertyFieldListPickerHostProps , ISPList } from '../propertyFields/listPicker/IPropertyFieldListPickerHost' ; import { PropertyFieldListPickerOrderBy } from '../propertyFields/listPicker/IPropertyFieldListPicker' ; import SPListPickerMockHttpClient from './SPListPickerMockService' ; /**
 * Service implementation to get list & list items from current SharePoint site
 */ export default class SPListPickerService { private context : BaseComponentContext ; private props : IPropertyFieldListPickerHostProps ; /**
     * Service constructor
     */ constructor ( _props , pageContext ) { this . props = _props ; this . context = pageContext ; } /**
     * Gets the collection of libs in the current SharePoint site, or target site if specified by webRelativeUrl
     */ public async getLibs ( ) { if ( Environment . type === EnvironmentType . Local ) { return this . getLibsFromMock ( ) ; } else { const webAbsoluteUrl = this . props . webAbsoluteUrl ? this . props . webAbsoluteUrl : this . context . pageContext . web . absoluteUrl ; let queryUrl = `${ webAbsoluteUrl }/_api/lists?$select=Title,id,BaseTemplate,RootFolder/ServerRelativeUrl&$expand=RootFolder` ; if ( this . props . orderBy !== null ) { queryUrl += '&$orderby=' ; switch ( this . props . orderBy ) { case PropertyFieldListPickerOrderBy . Id : queryUrl += 'Id' ; break ; case PropertyFieldListPickerOrderBy . Title : queryUrl += 'Title' ; break ; } } if ( this . props . filter ) { queryUrl += `&$filter=${ encodeURIComponent ( this . props . filter ) }` ; } else if ( this . props . baseTemplate !== null && this . props . baseTemplate ) { queryUrl += '&$filter=BaseTemplate%20eq%20' ; queryUrl += this . props . baseTemplate ; if ( this . props . includeHidden === false ) { queryUrl += '%20and%20Hidden%20eq%20false' ; } } else { if ( this . props . includeHidden === false ) { queryUrl += '&$filter=Hidden%20eq%20false' ; } } let response = await this . context . spHttpClient . get ( queryUrl , SPHttpClient . configurations . v1 ) ; let lists = ( await response . json ( ) ) as ISPLists ; if ( this . props . onListsRetrieved ) { let lr = this . props . onListsRetrieved ( lists . value ) ; let output ; if ( lr instanceof Array ) { output = lr ; } else { output = await lr ; } lists = { value : output } ; } return lists ; } } /**
     * Returns 3 fake SharePoint lists for the Mock mode
     */ private getLibsFromMock ( ) { return SPListPickerMockHttpClient . getLists ( this . context . pageContext . web . absoluteUrl ) . then ( ( ) => { const listData = { value : [ { Title : 'Mock List One' , Id : '6770c83b-29e8-494b-87b6-468a2066bcc6' , BaseTemplate : '109' , RootFolder : { ServerRelativeUrl : '/sites/test/MockListOne' } } , { Title : 'Mock List Two' , Id : '2ece98f2-cc5e-48ff-8145-badf5009754c' , BaseTemplate : '109' , RootFolder : { ServerRelativeUrl : '/sites/test/MockListTwo' } } , { Title : 'Mock List Three' , Id : 'bd5dbd33-0e8d-4e12-b289-b276e5ef79c2' , BaseTemplate : '109' , RootFolder : { ServerRelativeUrl : '/sites/test/MockListThree' } } ] } ; return listData ; } ) as Promise < ISPLists > ; } }