Introduction
In this blog, we are going to discuss the creation of a new webpart using SPFx (no JavaScript framework). This webpart will represent the data of the corresponding site such as website name, website URL, Total List Count. It retrieves the name of all custom lists and documents.
Let's get started …
Step A - (Creation of the Webpart)
Open the Command Prompt. Go to the drive where which you want to create the Project. Here I have created the project in “c-drive”.
Run the below commands:
“cd C:\”
“md Webpart”
Refer to image-1 for more details.
Refer to image-2 and run the command “yo @microsoft/sharepoint”. Then enter the requested details the same as mentioned in Image-2 to create the webpart. The webpart creation may take some time, so wait to complete the installation.
After completion, you will see a congratulation message (Check Image-3).
Then run the command “code .” to open the Visual studio code.
Expand src-> webparts (Refer image-4) to open the files.
Step B - (Create a New File)
Create a new file inside C: \WebPart\src\webparts\dynamicWp. Name it as “GetList.ts” as shown in image:5.

Step C - (Change the code)
Here I have mentioned the code for corresponding files. So please refer to the instructions accordingly and add/change the code.
In “DynamicWPWebPart.ts” replace the below code.
- import pnp from "sp-pnp-js";
- import {
- Version
- } from '@microsoft/sp-core-library';
- import {
- IPropertyPaneConfiguration,
- PropertyPaneTextField,
- PropertyPaneDropdown
- } from '@microsoft/sp-property-pane';
- import {
- BaseClientSideWebPart
- } from '@microsoft/sp-webpart-base';
- import {
- escape
- } from '@microsoft/sp-lodash-subset';
- import styles from './DynamicWpWebPart.module.scss';
- import * as strings from 'DynamicWpWebPartStrings';
- import MockHttpClient from './GetList';
- import {
- SPHttpClient,
- SPHttpClientResponse
- } from '@microsoft/sp-http';
- import {
- Environment,
- EnvironmentType
- } from '@microsoft/sp-core-library';
- import "@pnp/sp/webs";
- import "@pnp/sp/lists";
- import {
- _List
- } from '@pnp/sp/lists/types';
- export interface IDynamicWpWebPartProps {
- description: string;
- List: string;
- }
- export interface ISPLists {
- value: ISPList[];
- }
- export interface ISPList {
- Title: string;
- Id: string;
- BaseTemplate: string;
- }
- let array: string[];
- export default class DynamicWpWebPart extends BaseClientSideWebPart < IDynamicWpWebPartProps > {
- private _renderList(): void {
- if (Environment.type === EnvironmentType.Local) {
- this._getItemsFromList().then((response) => {
- this._listDesign(response.value);
- });
- } else if (Environment.type == EnvironmentType.SharePoint || Environment.type == EnvironmentType.ClassicSharePoint) {
- this._getListData().then((response) => {
- this._listDesign(response.value);
- });
- }
- }
- private _listDesign(items: ISPList[]): void {
- let html: string = '';
- let i = 0;
- html += `
- <div>
- <p class="${ styles.label} style="margin:5px;5px;5px;5px; font-weight: bold">Total List Count : ${items.length}</p>
- </div>`;
- html +=`
- <div>
- <b style="font-size:20px;">Document</b>`
- items.forEach((item: ISPList) => {
- if (item.BaseTemplate=='101') {
- html += `
- <ul class="${styles.list}">
- <li class="${styles.listItem}">
- <span class="ms-font-l">Document Title :
- <b>${item.Title}</b>
- </span>
- </li>
- </ul>`;
- }
- });
- html += ` </div>`;
- html += `<div><b style="font-size:20px;">List</b>`
- items.forEach((item1: ISPList) => {
- if (item1.BaseTemplate == '100') {
- html += `
- <ul class="${styles.list}">
- <li class="${styles.listItem}">
- <span class="ms-font">List Title :
- <b>${item1.Title}</b>
- </span>
- </li>
- </ul>`
- }
- });
- html += ` </div>`;
- const listContainer: Element = this.domElement.querySelector('#spListContainer');
- listContainer.innerHTML = html;
- }
- private _getListData(): Promise < ISPLists > {
- return this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + `/_api/web/lists?$filter=Hidden eq false`, SPHttpClient.configurations.v1).then((response: SPHttpClientResponse) => {
- return response.json();
- });
- }
- private _getItemsFromList(): Promise < ISPLists > {
- return MockHttpClient.get().then((data: ISPList[]) => {
- var listData: ISPLists = {
- value: data
- };
- return listData;
- }) as Promise < ISPLists > ;
- }
- public render(): void {
- this.domElement.innerHTML = `
- <textarea placeholder="DynamicWP" aria-label="Add a title" style="height: 23px; border:none;font-size:20px;font-weight: bold; width: 100%;"></textarea>
- <div class="${ styles.dynamicWp }">
- <div class="${ styles.container }">
- <div class="${ styles.row }">
- <div class="${ styles.column }">
- <div>
- <p class="${ styles.label} style="margin:5px;5px;5px;5px; font-weight: bold">WebSite Name : ${escape(this.context.pageContext.web.title)}</p>
- </div>
- <div>
- <p class="${ styles.label} style="margin:5px;5px;5px;5px; font-weight: bold">WebSite URL : ${escape(this.context.pageContext.site.absoluteUrl)}</p>
- </div>
- <div id="spListContainer"/>
- <p class="${ styles.description }">${escape(this.properties.description)}</p>
- </div>
- </div>
- </div>
- </div>`;
- this._renderList();
- }
- protected get dataVersion(): Version {
- return Version.parse('1.0');
- }
- protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
- return {
- pages: [{
- header: {
- description: strings.PropertyPaneDescription,
- list: strings.List
- },
- groups: [{
- groupName: strings.BasicGroupName,
- groupFields: [
- PropertyPaneTextField('Description', {
- label: strings.DescriptionFieldLabel
- })
- ]
- }]
- }]
- };
- }
- }

Join the conversation! Your thoughts help the community grow.