Display Username And Group Name Of A SharePoint Site In A WebPart By Using React In SPFx

First create your solution named  “itemwebpart” by using “yo @microsoft/Sharepoint as given below,
 
Display UserName and GroupName of a SharePoint Site in a webpart by
 
After that you have to give the information as mentioned below.
  • What is your solution name? Listview-webpart
  • Which baseline packages do you want to target for your component(s)? SharePoint Online only (latest)
  • Where do you want to place the files? Use the current folder
  • Which type of client-side component to create? Web Part
  • What is your Web part name? List items
  • What is your Web part description? Shows list items from the selected list
  • Which framework would you like to use? React
Display UserName and GroupName of a SharePoint Site in a webpart by 
 
Then you have to go to your solution.
 
After that you have to import the sp, site users and site groups from @pnp/sp library in your .ts file as given below.
 
Display UserName and GroupName of a SharePoint Site in a webpart by
  1. import "@pnp/sp/webs";  
  2. import "@pnp/sp/site-users";  
First step is to declare two collections for site users and site groups in your .ts file, so that you can use them getPropertyPaneConfiguration () method as given below.
 
Display UserName and GroupName of a SharePoint Site in a webpart by
  1. export default class ListItemsWebPart extends BaseClientSideWebPart < IListItemsWebPartProps > {  
  2.         private listsFetched: boolean;  
  3.         private userDropDownOptions: IPropertyPaneDropdownOption[];  
  4.         private grpDropDownOptions: IPropertyPaneDropdownOption[];  
Now use the above two collections getPropertyPaneConfiguration () method as mentioned below,
 
Display UserName and GroupName of a SharePoint Site in a webpart by
  1. protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {  
  2.     return {  
  3.         pages: [{  
  4.             header: {  
  5.                 description: strings.PropertyPaneDescription  
  6.             },  
  7.             groups: [{  
  8.                 groupName: strings.BasicGroupName,  
  9.                 groupFields: [  
  10.                     PropertyPaneDropdown('userDropDownOptions', {  
  11.                         label: strings.userField,  
  12.                         options: this.userDropDownOptions  
  13.                     }),  
  14.                     PropertyPaneDropdown('grpDropDownOptions', {  
  15.                         label: strings.grpField,  
  16.                         options: this.grpDropDownOptions  
  17.                     }),  
  18.                 ]  
  19.             }]  
  20.         }]  
  21.     };  
  22. }  
Then you have to create functions for site users and site groups.
 
For site users create a function in your .ts file as mentioned below,
 
Display UserName and GroupName of a SharePoint Site in a webpart by
  1. private getusersForSite(): Promise < any > {  
  2.     var users = sp.web.siteUsers  
  3.     return users.get().then((data) => {  
  4.         console.log('Total number of lists are ' + data.concat);  
  5.         return data;  
  6.     });  
  7. }  
For Site groups create a function in your .ts file as mentioned below,
Display UserName and GroupName of a SharePoint Site in a webpart by
  1. private getgroupsForSite(): Promise < any > {  
  2.     return sp.web.siteGroups.get().then((data) => {  
  3.         console.log('Total number of lists are ' + data.concat);  
  4.         return data;  
  5.     });  
  6. }   
After that you have to make two dropdown labels for site users and site groups respectively in your en-us. J’s file as mentioned below,
 
Display UserName and GroupName of a SharePoint Site in a webpart by
  1. define([], function() {  
  2.     return {  
  3.         "userField""SiteUsers",  
  4.         "grpField""SiteGroups"  
  5.     }  
  6. });  
Then you have to declare the dropdown labels for site groups and site users in your mystrings.d.ts file as mentioned below,
 
Display UserName and GroupName of a SharePoint Site in a webpart by
  1. declare interface IListItemsWebPartStrings {  
  2.     PropertyPaneDescription: string;  
  3.     userFieldlabel: string;  
  4.     groupFieldlabel: string;  
  5. }  
  6. declare module 'ListItemsWebPartStrings' {  
  7.     const strings: IListItemsWebPartStrings;  
  8.     export = strings;  
  9. }  
Then you have to call the functions in onPropertyPaneConfigurationStart() function in your .ts file as mentioned below,
 
Display UserName and GroupName of a SharePoint Site in a webpart by 
  1. protected onPropertyPaneConfigurationStart(): void {  
  2.         this.UserDropdownOptions = !this.listDropDownOptions;  
  3.         this.context.propertyPane.refresh();  
  4.         this.context.statusRenderer.displayLoadingIndicator(this.domElement, 'users');  
  5.         this.getusersForSite().then((response) => {  
  6.             for (let i = 0; i < response.length; i++) {  
  7.                 //now populate the listdropdown array  
  8.                 this.userDropDownOptions.push({  
  9.                     key: response[i].Title,  
  10.                     text: response[i].Title  
  11.                 });  
  12.             }  
  13.             this.userDropDownOptions = false;  
  14.             this.context.propertyPane.refresh();  
  15.             this.context.statusRenderer.clearLoadingIndicator(this.domElement);  
  16.             this.render();  
  17.         });  
  18.         this.GrpDropdownOptions = !this.GrpDropdownOptions;  
  19.         this.context.propertyPane.refresh();  
  20.         this.context.statusRenderer.displayLoadingIndicator(this.domElement, 'groups');  
  21.         this.getgroupsForSite().then((response) => {  
  22.             for (let i = 0; i < response.length; i++) {  
  23.                 this.grpDropDownOptions.push({  
  24.                     key: response[i].Title,  
  25.                     text: response[i].Title  
  26.                 });  
  27.             }  
  28.             this.GrpDropdownOptions = false;  
  29.             this.context.propertyPane.refresh();  
  30.             this.context.statusRenderer.clearLoadingIndicator(this.domElement);  
  31.             this.render();  
  32.  });  
After all above these steps you have to run gulp serve in your solution in command prompt.
 
After that, open web part in your SharePoint site and edit it as mentioned below,
 
Display UserName and GroupName of a SharePoint Site in a webpart by
 
Then check site users and site groups property pane and notice that site users and site groups on your site are visible in the property pane as mentioned below,
 
Display UserName and GroupName of a SharePoint Site in a webpart by
 
Display UserName and GroupName of a SharePoint Site in a webpart by 
 
Display UserName and GroupName of a SharePoint Site in a webpart by 
 

Conclusion

 
Dear viewers of this blog, I am displaying site users and site groups on the SharePoint site in the web part by using react in spfx.
 
Display UserName and GroupName of a SharePoint Site in a webpart by
 
Display UserName and GroupName of a SharePoint Site in a webpart by