Get list items from SharePoint using SPFx framework(No Javascript Framework)

In 4 Steps Get List Items from SharePoint Using SPFx Framework

One comment

Loading

Get list items from SharePoint using SPFx framework (No JavaScript Framework) โ€“ In this tutorial, we will learn about how we can get list items from the SharePoint list using the SPFx framework or SharePoint framework (no Javascript framework model). This is my fourth article in the SPFx web part learning series. You may refer to my previous articles:

Get list items from SharePoint using SPFx framework(No Javascript Framework)

Now letโ€™s start the web part development. Before that, we should create a custom list on the SharePoint online site. Here I have created the list named โ€œCustomerโ€ with the below columns:

  • Customer Name (Title column name has been renamed to the customer name).
  • Customer ID โ€“ Single line of text type.
  • Customer Contact No โ€“ Single line of text type.
Get SharePoint List Items using SPFx no javascript framework
Get SharePoint List Items using SPFx with no javascript framework

Get list items from SharePoint using SPFx framework (SharePoint API get list items) โ€“ Get Data from SharePoint list in SPFx

So, in this web part, our target is to read the โ€˜customerโ€™ list using the SPFx framework (no javascript framework) and display the items in the web part. Letโ€™s start now. Create SharePoint Framework (SPFx) solution: Step 1: Create a new solution, letโ€™s name it โ€œReadSPListItemsโ€ Open the node.js command prompt and type the below command โ€“ mkdir ReadSPListItems

C:\Temp\SPFx>mkdir ReadSPListItems

Navigate to the above newly created directory ReadSPListItems using the below command:

C:\Temp\SPFx>cd ReadSPListItems

Step 2: Run the Yeoman SharePoint Generator (yo @microsoft/sharepoint) to create the solution.

C:\Temp\SPFx\ReadSPListItems>yo @microsoft/sharepoint

After some time, you will be presented with a wizard with some questionnaires. Solution Name: Hit enter, it will take the default solution name otherwise you can pass your custom name.

Let's create a new SharePoint solution.
? What is your solution name? (read-sp-list-items)

The target for your component(s)? Hit enter on the default selection โ€“ SharePoint Online only (latest) otherwise you can select SharePoint 2016 or 2019 onwards version as well.

Which baseline packages do you want to target for your component(s)? (Use arrow keys)
> SharePoint Online only (latest)
SharePoint 2016 onwards, including 2019 and SharePoint Online
SharePoint 2019 onwards, including SharePoint Online

Select, where do you want to place the files? hit enter on the default selection โ€“ Use the current folder

Let's create a new SharePoint solution.
? What is your solution name? read-sp-list-items
? 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 arrow keys)
> Use the current folder
Create a subfolder with solution name

Select Deployment option โ€“ย  Select Y if you want to allow the app to be deployed instantly to all sites and will be accessible everywhere. Select N if you want to install the app on each site explicitly. Here letโ€™s type โ€œNโ€ and hit enter.

Let's create a new SharePoint solution.
? What is your solution name? read-sp-list-items
? 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
Found npm version 6.13.4
? Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without
running any feature deployment or adding apps in sites? (y/N) N

Select solution permission: Will the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant? (Y/N), type N, and hit enter.

Let's create a new SharePoint solution.
? What is your solution name? read-sp-list-items
? 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
Found npm version 6.13.4
? Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without
running any feature deployment or adding apps in sites? No
? Will the components in the solution require permissions to access web APIs that are unique and not shared with other c
omponents in the tenant? (y/N)N

Type of client-side component to create: We can choose any of the below client-side solutions:

  • WebPart
  • Extension.
  • Library

Letโ€™s,ย  select โ€œWebPartโ€ and hit enter.

Let's create a new SharePoint solution.
? What is your solution name? read-sp-list-items
? 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
Found npm version 6.13.4
? Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without
running any feature deployment or adding apps in sites? No
? Will the components in the solution require permissions to access web APIs that are unique and not shared with other c
omponents in the tenant? No
? Which type of client-side component to create? (Use arrow keys)
> WebPart
Extension
Library

Web Part Name: Type the web part name, here I have given it as ReadSPListItems, you can give any name or can continue with the default name. Hit enter.

Let's create a new SharePoint solution.
? What is your solution name? read-sp-list-items
? 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
Found npm version 6.13.4
? Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without
running any feature deployment or adding apps in sites? No
? Will the components in the solution require permissions to access web APIs that are unique and not shared with other c
omponents in the tenant? No
? Which type of client-side component to create? WebPart
Add new Web part to solution read-sp-list-items.
? What is your Web part name? ReadSPListItems

Web part description: You can give your own web part description or can continue with the default description. Hit enter.

Let's create a new SharePoint solution.
? What is your solution name? read-sp-list-items
? 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
Found npm version 6.13.4
? Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without
running any feature deployment or adding apps in sites? No
? Will the components in the solution require permissions to access web APIs that are unique and not shared with other c
omponents in the tenant? No
? Which type of client-side component to create? WebPart
Add new Web part to solution read-sp-list-items.
? What is your Web part name? ReadSPListItems
? What is your Web part description? (ReadSPListItems description)

Web part framework select: We can select any of the below frameworks:

  • No JavaScript framework
  • React
  • Knockout

Select the No JavaScript framework and hit enter.

Let's create a new SharePoint solution.
? What is your solution name? read-sp-list-items
? 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
Found npm version 6.13.4
? Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without
running any feature deployment or adding apps in sites? No
? Will the components in the solution require permissions to access web APIs that are unique and not shared with other c
omponents in the tenant? No
? Which type of client-side component to create? WebPart
Add new Web part to solution read-sp-list-items.
? What is your Web part name? ReadSPListItems
? What is your Web part description? ReadSPListItems description
? Which framework would you like to use? (Use arrow keys)
> No JavaScript framework
React
Knockout

Finally, the solution scaffolding looks like the below:

Get SharePoint List Items using SPFx no javascript framework - solution scaffolding
Get SharePoint List Items using SPFx no javascript framework โ€“ solution scaffolding

This will take a few minutes to scaffold the solution, letโ€™s wait till that, then we will get the below โ€œCongratulation!โ€ status โ€“ now we can type โ€œcode .โ€ to open the solution in the visual studio code editor.

Get SharePoint List Items using SPFx no javascript framework code editor
Get SharePoint List Items using SPFx no javascript framework code editor

Step 3:

In the command prompt type the below commandย  โ€œcode .โ€ to open the solution files in the visual studio code editor.

C:\Temp\SPFx\ReadSPListItems>code .

Once the solution gets opened, navigate to the โ€œReadSpListItemsWebPart.tsโ€ under the src->webparts\readSPListItems which is the main file, gets created automatically. This file we need to customize in order to add any custom functionality to the default web part.

ReadSpListItemsWebPart.ts - Get SharePoint List Items using SPFx no javascript framework
ReadSpListItemsWebPart.ts โ€“ Get SharePoint List Items using SPFx with no javascript framework

Copy-paste the below code in the above file โ€“ the below-highlighted code or method I have added, the rest of the codes were created by default.

importย {ย Versionย }ย fromย '@microsoft/sp-core-library';

importย {

ย ย IPropertyPaneConfiguration,

ย ย PropertyPaneTextField

}ย fromย '@microsoft/sp-property-pane';

importย {ย BaseClientSideWebPartย }ย fromย '@microsoft/sp-webpart-base';

importย {ย escapeย }ย fromย '@microsoft/sp-lodash-subset';

importย stylesย fromย './ReadSpListItemsWebPart.module.scss';

importย *ย asย stringsย fromย 'ReadSpListItemsWebPartStrings';

importย {

ย ย SPHttpClient,

ย ย SPHttpClientResponseย ย ย 

}ย fromย '@microsoft/sp-http';

importย {

ย ย Environment,

ย ย EnvironmentType

}ย fromย '@microsoft/sp-core-library';

exportย interfaceย IReadSpListItemsWebPartProps

ย {

ย ย description:ย string;

}

exportย interfaceย ISPListsย 

{

ย ย value:ย ISPList[];

}

exportย interfaceย ISPListย 

{

ย ย Title:ย string;

ย ย CustomerID:ย string;

ย ย CustomerContactNoย :string;

}

exportย defaultย classย ReadSpListItemsWebPartย extendsย BaseClientSideWebPartย <IReadSpListItemsWebPartProps>

ย {

ย ย privateย _getListData():ย Promise<ISPLists>

ย ย ย {

ย ย ย ย returnย this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrlย +ย "/_api/web/lists/GetByTitle('Customer')/Items",SPHttpClient.configurations.v1)

ย ย ย ย ย ย ย ย .then((response:ย SPHttpClientResponse)ย =>ย 

ย ย ย ย ย ย ย ย {

ย ย ย ย ย ย ย ย returnย response.json();

ย ย ย ย ย ย ย ย });

ย ย ย ย }

ย ย ย ย privateย _renderListAsync():ย void

ย ย ย ย {

ย ย ย 

ย ย ย ย ย ifย (Environment.typeย ==ย EnvironmentType.SharePointย ||ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย Environment.typeย ==ย EnvironmentType.ClassicSharePoint)ย {

ย ย ย ย ย ย this._getListData()

ย ย ย ย ย ย ย ย .then((response)ย =>ย {

ย ย ย ย ย ย ย ย ย ย this._renderList(response.value);

ย ย ย ย ย ย ย ย });

ย }

ย ย }

ย ย privateย _renderList(items:ย ISPList[]):ย voidย 

ย ย ย {

ย ย ย ย letย html:ย stringย =ย '<tableย border=1ย width=100%ย style="border-collapse:ย collapse;">';

ย ย ย ย htmlย +=ย '<th>Customerย Name</th>ย <th>Customerย Codeย </th><th>Customerย Contactย Number</th>';

ย ย ย ย items.forEach((item:ย ISPList)ย =>ย {

ย ย ย ย ย ย htmlย +=ย `

ย ย ย ย ย ย <tr>ย ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย <td>${item.Title}</td>

ย ย ย ย ย ย ย ย ย ย <td>${item.CustomerID}</td>

ย ย ย ย ย ย ย ย ย ย <td>${item.CustomerContactNo}</td>

ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย </tr>

ย ย ย ย ย ย ย ย ย ย `;

ย ย ย ย });

ย ย ย ย htmlย +=ย '</table>';

ย ย 

ย ย ย ย constย listContainer:ย Elementย =ย this.domElement.querySelector('#spListContainer');

ย ย ย ย listContainer.innerHTMLย =ย html;

ย ย }

ย ย publicย render():ย voidย {

ย ย ย ย this.domElement.innerHTMLย =ย `

ย ย ย ย ย ย <divย class="${ย styles.readSpListItemsย }">

ย ย ย ย ย ย ย ย <divย class="${ย styles.containerย }">

ย ย ย ย ย ย ย ย ย ย <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">

ย ย ย ย ย ย ย ย ย ย <spanย class="ms-font-xlย ms-fontColor-white">Welcomeย toย SharePointย Modernย Developmennt</span>

ย ย ย ย ย ย ย ย ย ย <pย class="ms-font-lย ms-fontColor-white">Loadingย fromย ${this.context.pageContext.web.title}</p>

ย ย ย ย ย ย ย ย ย ย <pย class="ms-font-lย ms-fontColor-white">Retriveย Dataย fromย SharePointย List</p>

ย ย ย ย ย ย ย ย </div>

ย ย ย ย ย ย </div>ย 

ย ย ย ย ย ย ย ย ย ย <divย class="ms-Grid-rowย ms-bgColor-themeDarkย ms-fontColor-whiteย ${styles.row}">

ย ย ย ย ย ย ย ย ย ย <div>Listย Items</div>

ย ย ย ย ย ย ย ย ย ย <br>

ย ย ย ย ย ย ย ย ย ย ย <divย id="spListContainer"ย />

ย ย ย ย ย ย ย ย </div>

ย ย ย ย ย ย </div>`;

ย ย ย ย ย ย this._renderListAsync();

ย ย }
  ย 

ย ย 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

ย ย ย ย ย ย ย ย ย ย ย ย ย ย })

ย ย ย ย ย ย ย ย ย ย ย ย ]

ย ย ย ย ย ย ย ย ย ย }

ย ย ย ย ย ย ย ย ]

ย ย ย ย ย ย }

ย ย ย ย ]

ย ย };

}

}

ย 

Retrieve SharePoint list items using SPFx โ€“ SharePoint API get list items

Brief description about the above code or how you can display your list data in the web part. Add your list columnโ€™s internal name in the below section:

export interface ISPListย 
{
 ย Title:ย string;
 ย CustomerID:ย string;
 ย CustomerContactNoย :string;
}

In the below method, replace the โ€œCustomerโ€ list with your list title. this.context.pageContext.web.absoluteUrlย +ย โ€œ/_api/web/lists/GetByTitle(โ€˜Customerโ€˜)

private _getListData(): Promise<ISPLists>
 ย ย {
ย ย ย ย returnย this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrlย +ย "/_api/web/lists/GetByTitle('Customer')/Items",SPHttpClient.configurations.v1)
 ย ย ย ย ย ย ย .then((response:ย SPHttpClientResponse)ย =>ย 
 ย ย ย ย ย ย ย {
 ย ย ย ย ย ย ย returnย response.json();
 ย ย ย ย ย ย ย });
 ย ย ย }

Now, in the _renderList method in the html+= line add your all table headers and in the tr section add reference all your SharePoint list columns.

private _renderList(items: ISPList[]): voidย 
 ย ย {
ย ย ย ย letย html:ย stringย =ย '<tableย border=1ย width=100%ย style="border-collapse:ย collapse;">';
 ย ย ย htmlย +=ย '<th>Customerย Name</th>ย <th>Customerย Codeย </th><th>Customerย Contactย Number</th>';
 ย ย ย items.forEach((item:ย ISPList)ย =>ย {
 ย ย ย ย ย htmlย +=ย `
 ย ย ย ย ย <tr>ย ย ย ย ย ย ย ย ย ย ย ย 
 ย ย ย ย ย ย ย ย ย <td>${item.Title}</td>
 ย ย ย ย ย ย ย ย ย <td>${item.CustomerID}</td>
 ย ย ย ย ย ย ย ย ย <td>${item.CustomerContactNo}</td>ย ย ย ย ย ย ย 
 ย ย ย ย ย ย ย ย ย </tr>
ย ย ย ย ย ย ย ย ย ย `;

 ย ย ย });
 ย ย ย html += '</table>'
 ย ย ย constย listContainer:ย Elementย =ย this.domElement.querySelector('#spListContainer');
 ย ย ย listContainer.innerHTML = html;
ย ย }

Get list items from SharePoint using SPFx framework (retrieve SharePoint list items using SPFx) demo

Finally, letโ€™s run the โ€œGulp Serveโ€ command in the same command prompt and once we launch and add the web part in the SharePoint workbench like below globalsharepoint2020.sharepoint.com/sites/allcompany/_layouts/15/workbench.aspx

Get SharePoint List Items using SPFx no javascript framework - Display Data
Get SharePoint List Items using SPFx no javascript framework โ€“ Display Data

Now, we can see all list items from the โ€œCustomerโ€ list have been displayed over here.ย 

Summary: Get list items from SharePoint using SPFx framework (Get Data from SharePoint list in SPFx)

Thus, in this article, we have learned the below with respect to reading the list items from SharePoint online list:

  • How to get list items from SharePoint List using the SPFx framework(No Javascript Framework).
  • How to work with list items with Typescript and REST API.
  • How to create the SPFx web part to retrieve SharePoint List Items using no javascript framework.
  • How to retrieve and display SharePoint List Items using REST API and SPFx

See Also: SharePoint Online tutorial

You may also like the below SharePoint Online tutorials:

About Post Author


Discover more from Global SharePoint

Subscribe to get the latest posts sent to your email.

1 comments on โ€œIn 4 Steps Get List Items from SharePoint Using SPFx Frameworkโ€

Do you have a better solution or question on this topic? Please leave a comment