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
- import PnpDropDownfrom './components/PnpDropDown';
- import {
- IPnpDropDownProps
- } from './components/IPnpDropDownProps';
- import "@pnp/polyfill-ie11";
- import {
- sp
- } from '@pnp/sp';
- import {
- MYchoices
- } from '../../models';
- export interface IPnpDropDownWebPartProps {
- description: string;
- }
- export default class PnpDropDownWebPart extends BaseClientSideWebPart <IPnpDropDownWebPartProps> {
- private _opchoices: MYchoices[] = [];
- protected onInit(): Promise <void> {
- return new Promise < void > ((resolve: () => void, reject: (error ? : any) => void): void => {
- sp.setup({
- sp: {
- headers: {
- "Accept": "application/json; odata=nometadata"
- }
- }
- });
- resolve();
- });
- }
- public render(): void {
- if (!this.renderedOnce) {
- this._drpdown();
- }
- const element: React.ReactElement <IPnpDropDownProps> = React.createElement(PnpDropDown, {
- description: this.properties.description,
- context: this.context,
- mychoices: this._opchoices
- });
- ReactDom.render(element, this.domElement);
- }
- private _drpdown() {
- let field = sp.web.lists.getByTitle("Program");
- let RiskStatus = field.fields.getByInternalNameOrTitle("RiskStatus");
- RiskStatus.select('Choices').get().then((fieldData5) => {
- this._opchoices = fieldData5;
- this.render();
- });
- }
- protected onDispose(): void {
- ReactDom.unmountComponentAtNode(this.domElement);
- }
- protected getdataVersion(): Version {
- returnVersion.parse('1.0');
- }
- protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
- return {
- pages: [{
- header: {
- description: strings.PropertyPaneDescription
- },
- groups: [{
- groupName: strings.BasicGroupName,
- groupFields: [
- PropertyPaneTextField('description', {
- label: strings.DescriptionFieldLabel
- })
- ]
- }]
- }]
- };
- }
- }
In Pnpdropdown.tsx
- import * asReactfrom 'react';
- import styles from './PnpDropDown.module.scss';
- import {
- IPnpDropDownProps
- } from './IPnpDropDownProps';
- import {
- IPnpDropDownState
- } from './IPnpDropDownState';
- export default class PnpDropDown extends React.Component <IPnpDropDownProps, IPnpDropDownState> {
- constructor(props: IPnpDropDownProps, state: IPnpDropDownState) {
- super(props);
-
-
-
- }
- public render(): React.ReactElement <IPnpDropDownProps> {
- let Mychoice = "";
- if (this.props.mychoices["Choices"]) {
- Mychoice = this.props.mychoices["Choices"].map((item, i: number): JSX.Element => {
- return ( < option value = { item || '' } > { item || ''} < /option>);
- });
- }
- return ( < div className = { styles.container } > < div >
- <select id = "Risk" > { Mychoice } < /select> < /div></div > );
- }
- }
- import {
- WebPartContext
- } from '@microsoft/sp-webpart-base';
- import {
- MYchoices
- } from '../../../models';
- export interface IPnpDropDownProps {
- description: string;
- context: WebPartContext;
- mychoices ? : MYchoices[];
- }
- in Ipnpdropdownstate.ts
- export interface IPnpDropDownState {
- RiskStatus: any[];
- }
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
- export interface MYchoices {
- Choices: any;
- }
2nd ts
index.ts
- export * from'./mynumber';
Here my list name is Program and my choice field name is RiskStatus, install polyfills for working with ie11.
Happy Coding.