import “@pnp/sp/webs”;
import “@pnp/sp/site-users”;
export default class ListItemsWebPart extends BaseClientSideWebPart<IListItemsWebPartProps> {
private listsFetched: boolean;
private userDropDownOptions: IPropertyPaneDropdownOption[];
private grpDropDownOptions: IPropertyPaneDropdownOption[];
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields:[
PropertyPaneDropdown(‘userDropDownOptions’,{
label:strings.userField,
options:this.userDropDownOptions
}),
PropertyPaneDropdown(‘grpDropDownOptions’,{
label:strings.grpField,
options:this.grpDropDownOptions
}),
]
}
]
}
]
};
}
private getusersForSite():Promise<any>{
var users = sp.web.siteUsers
return users.get().then((data) =>{
console.log(‘Total number of lists are ‘ + data.concat);
return data;
});
}
private getgroupsForSite():Promise<any>{
return sp.web.siteGroups.get().then((data) =>{
console.log(‘Total number of lists are ‘ + data.concat);
return data;
});
}
define([], function() {
return {
“userField”: “SiteUsers”,
“grpField”: “SiteGroups”
}
});
declare interface IListItemsWebPartStrings {
PropertyPaneDescription: string;
userFieldlabel: string;
groupFieldlabel: string;
}
declare module ‘ListItemsWebPartStrings’ {
const strings: IListItemsWebPartStrings;
export = strings;
}
protected onPropertyPaneConfigurationStart():void{
this.UserDropdownOptions = !this.listDropDownOptions;
this.context.propertyPane.refresh();
this.context.statusRenderer.displayLoadingIndicator(this.domElement, ‘users’);
this.getusersForSite().then((response) =>{
for(let i=0 ; i< response.length;i++){
//now populate the listdropdown array
this.userDropDownOptions.push({key:response[i].Title,text:response[i].Title});
}
this.userDropDownOptions = false;
this.context.propertyPane.refresh();
this.context.statusRenderer.clearLoadingIndicator(this.domElement);
this.render();
});
this.GrpDropdownOptions = !this.GrpDropdownOptions;
this.context.propertyPane.refresh();
this.context.statusRenderer.displayLoadingIndicator(this.domElement, ‘groups’);
this.getgroupsForSite().then((response) =>{
for(let i=0 ; i< response.length;i++){
this.grpDropDownOptions.push({key:response[i].Title,text:response[i].Title});
}
this.GrpDropdownOptions = false;
this.context.propertyPane.refresh();
this.context.statusRenderer.clearLoadingIndicator(this.domElement);
this.render();
});
Conclusion:
Dear viewers in this blog I am displaying site users and site groups of the SharePoint site in the web part by using react in spfx.
Keyword :
“Retrieve site users and site groups in spfx”.