Last active
November 8, 2019 12:13
-
-
Save chrisobriensp/59443bde36012ffda8dfa58c2db5abb6 to your computer and use it in GitHub Desktop.
Dynamically populate SPFX web part property dropdown list - using lists in current site. See http://cob-sp.com/SPFx-WP-Props2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* need some imports e.g.: | |
import { IODataList } from '@microsoft/sp-odata-types'; | |
import { SPHttpClient, SPHttpClientConfigurations, SPHttpClientConfiguration, SPHttpClientResponse, ODataVersion, ISPHttpClientConfiguration } from '@microsoft/sp-http'; | |
*/ | |
private dropdownOptions: IPropertyPaneDropdownOption[]; | |
private listsFetched: boolean; | |
// these methods are split out to go step-by-step, but you could refactor and be more direct if you choose.. | |
private fetchLists(url: string) : Promise<any> { | |
return this.context.spHttpClient.get(url, SPHttpClient.configurations.v1).then((response: SPHttpClientResponse) => { | |
if (response.ok) { | |
return response.json(); | |
} else { | |
console.log("WARNING - failed to hit URL " + url + ". Error = " + response.statusText); | |
return null; | |
} | |
}); | |
} | |
private fetchOptions(): Promise<IPropertyPaneDropdownOption[]> { | |
var url = this.context.pageContext.web.absoluteUrl + `/_api/web/lists?$filter=Hidden eq false`; | |
return this.fetchLists(url).then((response) => { | |
var options: Array<IPropertyPaneDropdownOption> = new Array<IPropertyPaneDropdownOption>(); | |
response.value.map((list: IODataList) => { | |
console.log("Found list with title = " + list.Title); | |
options.push( { key: list.Id, text: list.Title }); | |
}); | |
return options; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment