Showing posts with label Sharepoint Framework. Show all posts
Showing posts with label Sharepoint Framework. Show all posts

Thursday, 22 January 2026

Bind dropdown list in property pane spfx


Prerequisite:

Make sure installation of spfx is already done and we are using pnp to access the SharePoint lists from current site so make sure pnp is also installed.

How to bind property pane Dropdown list in Client side webpart in SharePoint Framework.

We have a scenario suppose that we have to bind a dropdown list with current site’ s lists/libraries.
 Just need to follow below instruction step by step  
1.       Create a Solution  .
2.       Add a Webpart to the solution.
3.       Declare the property pane  “PropertyPaneDropdown” for dropdown list.

import {
  BaseClientSideWebPart,
  IPropertyPaneConfiguration,
  PropertyPaneDropdown,
} from '@microsoft/sp-webpart-base';

4.       Come to the property pane under “getPropertyPaneConfiguration”.
5.       Define your own group of property pane or leave  the default and add under existing group fields.
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneDropdown('dropdownLists', {
                  label: "Lists",
                  options:this._dropdownOptions,
                  disabled:this.listsDropdownDisabled
                })
              ]
            }
          ]
        }
      ]
    };
  }

6.       Declare Interface “IPropertyPaneDropdownOption”  and “IODataList” to map the dropdown list with the list
                  
                        export interface IPropertyPaneDropdownOption
{
  key:string;
text:string;
}
export interface IODataList
{
  Id:string;
 Title:string;
}
7.       Define two variables for dropdown option and to disable the dropdown while lists loading just below the .
      
export default class HellowWebpartWebPart extends BaseClientSideWebPart<IHellowWebpartWebPartProps> {

         private _dropdownOptions: IPropertyPaneDropdownOption[];
  private listsDropdownDisabled: boolean = true;

8.       Now override the existing method onPropertyPaneConfigurationStart() and paste the below code

protected onPropertyPaneConfigurationStart(): void
  {
    //Bind DropDown List in Peropert pane
    this.listsDropdownDisabled = !this._dropdownOptions;
    if (this._dropdownOptions)
    {
      return;
    }
      this.context.statusRenderer.displayLoadingIndicator(this.domElement, '_dropdownOptions');
      pnp.sp.web.lists.get()
      .then((listOptions: IODataList[]): void =>
      {
          var options: Array<IPropertyPaneDropdownOption> = new Array<IPropertyPaneDropdownOption>();
          listOptions.map((list: IODataList) => {
          options.push( { key: list.Id, text: list.Title });
      });
        this._dropdownOptions=options
        this.listsDropdownDisabled = false;
        this.context.propertyPane.refresh();
        this.context.statusRenderer.clearLoadingIndicator(this.domElement);
        this.render();
      });
  }

9.       Start terminal and run gulp bundle
10.   Now run the gulp serve –nobrowser command in terminal
11.   Open workbench on the site open direct link https://tenant.sharepoint.com/sites/demo/_layouts/15/workbench.aspx
12.   Add client side webpart on the bench.
13.   Click on Edit properties
14.   You see on  right side in properties will be dropdown list bind with current site’s lists/libraries

Happy coding  !!!


Wednesday, 21 January 2026

ERR_SSL_PROTOCOL_ERROR in spfx workbench

Issue error on local workbaench:


This site can’t provide a secure connection localhost sent an invalid response.
Try running Windows Network Diagnostics.
ERR_SSL_PROTOCOL_ERROR

Solution:

1.Open package.json file
2. go to below section

before

  "scripts": {
    "build": "gulp bundle",
    "clean": "gulp clean",
    "test": "gulp test"
  }

and add "dev" : "set NODE_NO_HTTP2=1&& gulp serve",

after

  "scripts": {
    "build": "gulp bundle",
    "clean": "gulp clean",
    "test": "gulp test",
    "dev" : "set NODE_NO_HTTP2=1&& gulp serve"
  }

3.  Run "npm run dev" command in terminal or command window
4. if you  still face the issue then run command "gulp trust-dev-cert" then run "npm run dev" command

Sunday, 17 March 2019

SPfx Crud operation using pnp

import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneTextField,
PropertyPaneDropdown,
PropertyPaneToggle,
PropertyPaneSlider
} from '@microsoft/sp-webpart-base';
import { escape } from '@microsoft/sp-lodash-subset';
import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';
import * as jQuery from 'jquery';
import * as pnp from 'sp-pnp-js';
import 'jqueryui';
import{IListItem} from './IListItem';

import styles from './HellowWebpartWebPart.module.scss';
import * as strings from 'HellowWebpartWebPartStrings';

export interface IHellowWebpartWebPartProps {
description: string;
dropdown:string;
}
export interface ISPList {
Id: number;
Title: string;
Salary: string;
Company:string;
Department:string;
}
export default class HellowWebpartWebPart extends BaseClientSideWebPart<IHellowWebpartWebPartProps> {

public render(): void {
this.domElement.innerHTML =`
<div class="${ styles.container }">
<div class="${ styles.container }">
<div class="${ styles.row }">
<div class="${ styles.column }">
<span class="${ styles.title }">CRUD operations</span>
<p class="${ styles.subTitle }">No Framework</p>
<div class="${ styles.container }">
<div class="${ styles.row }">
<div class="${ styles.column }">Item id</div><div class="${ styles.column }"><input type="text" class="${styles.description} itemid"></div>
</div>
<div class="${ styles.row }">
<div class="${ styles.column }">Name</div><div class="${ styles.column }"><input type="text" class="${styles.description} name"></div>
</div>
<div class="${ styles.row }">
<div class="${ styles.column }">Salary</div><div class="${ styles.column }"><input type="text" class="${styles.description} salary"></div>
</div>
<div class="${ styles.row }">
<div class="${ styles.column }">Company</div><div class="${ styles.column }"><input type="text" class="${styles.description} company"></div>
</div>
<div class="${ styles.row }">
<div class="${ styles.column }">Department</div><div class="${ styles.column }"><input type="text" class="${styles.description} department"></div>
</div>
</div>
<br/>
<div class='itemTable'>
</div>
<br/>
<div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
<div class="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1">
<button class="${styles.button} create-Button">
<span class="${styles.label}">Create item</span>
</button>
<button class="${styles.button} read-Button">
<span class="${styles.label}">Read item</span>
</button>
<button class="${styles.button} update-Button">
<span class="${styles.label}">Update item</span>
</button>
<button class="${styles.button} delete-Button">
<span class="${styles.label}">Delete item</span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>`;
this.setButtonsEventHandlers();

}
private setButtonsEventHandlers(): void
{
const webPart: HellowWebpartWebPart = this;
jQuery(".create-Button").click(function()
{
webPart.createItem();
});
jQuery(".update-Button").click(function()
{
webPart.updateItem();
});
jQuery(".delete-Button").click(function()
{
webPart.deleteItem();
});
jQuery(".read-Button").click(function()
{
webPart.readItem();
});
webPart.readItem();
}
private createItem(): void
{
const webPart: HellowWebpartWebPart = this;
pnp.sp.web.lists.getByTitle("Employee").items.add(
{
Title:jQuery('.name').val(),
Salary:jQuery('.salary').val(),
Company:jQuery('.company').val(),
Department:jQuery('.department').val()
}).then(function(response)
{
webPart.readItem();
alert("Item Added Succes fully")
}).catch(function(exception){

alert("Something went wrong :"+exception);
});
}
private readItem(): void
{
const webPart: HellowWebpartWebPart = this;
pnp.sp.web.lists.getByTitle("Employee").items.getAll().then(function(response)
{
let items: ISPList[]=response;
let html: string = '<table class="TFtable" border=1 width=style="bordercollapse: collapse;">';
html += `<th></th><th>ProfileId</th><th>Name</th><th>Salary</th><th>Company</th><th>Department</th>`;
if (items.length>0)
{
items.forEach((item: ISPList) => {
html += `
<tr>
<td><input type="radio" id="ProfileId" class='profileid' name="ProfileId" value="${item.Id}"> <br> </td>
<td>${item.Id}</td>
<td>${item.Title}</td>
<td>${item.Salary}</td>
<td>${item.Company}</td>
<td>${item.Department}</td>
</tr>
`;
});
}
else
{
html +="No records...";
}
html += `</table>`;
jQuery('.itemTable').html(html);
jQuery('.profileid').click(function()
{
webPart.readListItemById(jQuery(this).val());
});

}).catch(function(exception){
alert("Something went wrong :"+exception);
});
}
private readListItemById(itemid:number):void
{
pnp.sp.web.lists.getByTitle("Employee").items.getById(itemid).get().then(function(response)
{
let item:ISPList=response;
jQuery('.itemid').val(item.Id),
jQuery('.name').val(item.Title),
jQuery('.salary').val(item.Salary),
jQuery('.company').val(item.Company),
jQuery('.department').val(item.Department)
}).catch(function(exception){
alert("Something went wrong :"+exception);
});
}
private updateItem(): void
{
const webPart: HellowWebpartWebPart = this;
pnp.sp.web.lists.getByTitle("Employee").items.getById(jQuery('.itemid').val()).update(
{
Title:jQuery('.name').val(),
Salary:jQuery('.salary').val(),
Company:jQuery('.company').val(),
Department:jQuery('.department').val()
}).then(function(response)
{
webPart.readItem();
alert("Item updated Succesfully");
}).catch(function(exception){

alert("Something went wrong :"+exception);
});


} private deleteItem(): void
{
const webPart: HellowWebpartWebPart = this;
pnp.sp.web.lists.getByTitle("Employee").items.getById(jQuery('.itemid').val()).delete().then(function(response)
{
webPart.readItem();
alert("Item deleted Succesfully");
}).catch(function(exception){
alert("Something went wrong :"+exception);
});;
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}


protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('description', {
label: strings.DescriptionFieldLabel
}),
PropertyPaneDropdown('dropdown', {
label: "City Name",
options:[{key:'1',text:'Saharanpur'},
{key:'2',text:'Banglore'},
{key:'3',text:'Meerut'},
{key:'4',text:'New Delhi'},
{key:'5',text:'Agra'}]
}),
PropertyPaneToggle('toggle',{
label:"Toggle button"
}),
PropertyPaneSlider('Slider',{
label:"Slider",min:1,max:10
})
]
}
]
}
]
};
}
}