Binding Dropdown Values From SharePoint List Choice Column Using SPfx And Pnpjs

Open a command prompt. Create a directory for an SPFx solution.
 
md spfx-pnp-DropDown
 
Navigate to the above created directory.
 
cd spfx-pnp-DropDown
 
Run the Yeoman SharePoint Generator to create the solution.
 
yo @microsoft/sharepoint
 
Solution Name
 
Hit Enter to have default name (spfx-pnp-DropDown in this case) or type in any other name for your solution.
 
Select choice - Hit Enter
 
Target for the component
 
Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
 
Selected choice: SharePoint Online only (latest)
 
Place of files
 
We may choose to use the same folder or create a subfolder for our solution.
 
Selected choice: Same folder
Deployment option
Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
Selected choice: N (install on each site explicitly)
 
Permissions to access web APIs
 
Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
 
Selected choice: N (solution contains unique permissions)
 
Type of client-side component to create
 
We can choose to create a client-side web part or an extension. Choose web part option.
 
Selected choice: WebPart
 
Web part name
 
Hit Enter to select the default name or type in any other name. 
 
Selected choice: PnpDropDown
 
Web part description
 
Hit Enter to select the default description or type in any other value.
 
Framework to use
 
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice: React
 
Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running below command.
 
npm shrinkwrap
 
In the command prompt, type below command to open the solution in the code editor of your choice.
 
code .
 
NPM Packages Used,
 
@pnp/sp(https://www.npmjs.com/package/@pnp/sp)
 
On the command prompt, run below command.
 
npm i @pnp/logging @pnp/common @pnp/odata @pnp/sp --save
 
for Pollyfills
 
npm install --save @pnp/polyfill-ie11
 
in PnpDropDownWebpart.ts
  1. import PnpDropDownfrom './components/PnpDropDown';  
  2. import {  
  3.     IPnpDropDownProps  
  4. } from './components/IPnpDropDownProps';  
  5. import "@pnp/polyfill-ie11";  
  6. import {  
  7.     sp  
  8. } from '@pnp/sp';  
  9. import {  
  10.     MYchoices  
  11. } from '../../models';  
  12. export interface IPnpDropDownWebPartProps {  
  13.     description: string;  
  14. }  
  15. export default class PnpDropDownWebPart extends BaseClientSideWebPart <IPnpDropDownWebPartProps> {  
  16.     private _opchoices: MYchoices[] = [];  
  17.     protected onInit(): Promise <void> {  
  18.         return new Promise < void > ((resolve: () => void, reject: (error ? : any) => void): void => {  
  19.             sp.setup({  
  20.                 sp: {  
  21.                     headers: {  
  22.                         "Accept""application/json; odata=nometadata"  
  23.                     }  
  24.                 }  
  25.             });  
  26.             resolve();  
  27.         });  
  28.     }  
  29.     public render(): void {  
  30.         if (!this.renderedOnce) {  
  31.             this._drpdown();  
  32.         }  
  33.         const element: React.ReactElement <IPnpDropDownProps> = React.createElement(PnpDropDown, {  
  34.             description: this.properties.description,  
  35.             context: this.context,  
  36.             mychoices: this._opchoices  
  37.         });  
  38.         ReactDom.render(element, this.domElement);  
  39.     }  
  40.     private _drpdown() {  
  41.         let field = sp.web.lists.getByTitle("Program");  
  42.         let RiskStatus = field.fields.getByInternalNameOrTitle("RiskStatus");  
  43.         RiskStatus.select('Choices').get().then((fieldData5) => {  
  44.             this._opchoices = fieldData5;  
  45.             this.render();  
  46.         });  
  47.     }  
  48.     protected onDispose(): void {  
  49.         ReactDom.unmountComponentAtNode(this.domElement);  
  50.     }  
  51.     protected getdataVersion(): Version {  
  52.         returnVersion.parse('1.0');  
  53.     }  
  54.     protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {  
  55.         return {  
  56.             pages: [{  
  57.                 header: {  
  58.                     description: strings.PropertyPaneDescription  
  59.                 },  
  60.                 groups: [{  
  61.                     groupName: strings.BasicGroupName,  
  62.                     groupFields: [  
  63.                         PropertyPaneTextField('description', {  
  64.                             label: strings.DescriptionFieldLabel  
  65.                         })  
  66.                     ]  
  67.                 }]  
  68.             }]  
  69.         };  
  70.     }  
  71. }  
In Pnpdropdown.tsx
  1. import * asReactfrom 'react';  
  2. import styles from './PnpDropDown.module.scss';  
  3. import {  
  4.     IPnpDropDownProps  
  5. } from './IPnpDropDownProps';  
  6. import {  
  7.     IPnpDropDownState  
  8. } from './IPnpDropDownState';  
  9. export default class PnpDropDown extends React.Component <IPnpDropDownProps, IPnpDropDownState> {  
  10.     constructor(props: IPnpDropDownProps, state: IPnpDropDownState) {  
  11.         super(props);  
  12.         /* this.state = { 
  13.         RiskStatus: [{ Choices: "" }] 
  14.         };*/  
  15.     }  
  16.     public render(): React.ReactElement <IPnpDropDownProps> {  
  17.         let Mychoice = "";  
  18.         if (this.props.mychoices["Choices"]) {  
  19.             Mychoice = this.props.mychoices["Choices"].map((item, i: number): JSX.Element => {  
  20.                 return ( < option value = {  item || '' } > {  item || ''} < /option>);  
  21.             });  
  22.         }  
  23.         return ( < div className = { styles.container  } > < div > 
  24. <select id = "Risk" > {   Mychoice  } < /select> < /div></div > );  
  25.     }  
  26.  
In IpnpdropdownProps.ts
  1. import {  
  2.     WebPartContext  
  3. } from '@microsoft/sp-webpart-base';  
  4. import {  
  5.     MYchoices  
  6. } from '../../../models';  
  7. export interface IPnpDropDownProps {  
  8.     description: string;  
  9.     context: WebPartContext;  
  10.     mychoices ? : MYchoices[];  



  11. in Ipnpdropdownstate.ts  
  12. export interface IPnpDropDownState {  
  13.     RiskStatus: any[];  
  14. }  
In the model folder place upcoming 2 ts files... Model folder is placed in src folder directly not inside webparts folder
 
1st ts 
 
mynumber.ts
  1. export interface MYchoices {  
  2.    Choices: any;  
  3. }  
2nd ts
 
index.ts 
  1. export * from'./mynumber';  
Here my list name is Program and my choice field name is RiskStatus, install polyfills for working with ie11.

Happy Coding.