Introduction
Here, let us look at performing basic list operations, using SharePoint JavaScript Object Model (JSOM) on SharePoint Framework Solutions (SPFx).
Microsoft provides the packages for supporting JSOM operations. Usually in the traditional approach, JavaScript files required for JSOM operations are MicrosoftAjax.js, SP.js, SP.runtime.js etc. For SPFx solutions, the required files can be included by installing the required typings.
In my previous article, you will have learned building the code, using JSOM approach on SharePoint Framework Solutions.
The support files are installed, using the typings. Install the required typings (Microsoft.ajax and Sharepoint) before creating the project.
Create the project with no JavaScript framework option, using the command yo @microsoft/sharepoint. In Render method, insert the necessary HTML wrapper to display the data.
Declare other methods to process the data. Four methods are declared to retrieve SharePoint data.
- Get Lists
- Get List by Title
- Create List
- Delete List
Get lists
Let us look at retrieving all the lists from SharePoint site, using JSOM approach.
The context of the object is set and then the list collection will be retrieved, using the Web object. The list collection is loaded and executed with the context before looking at the data. Now, using Get Enumerator method, individual list properties are retrieved and displayed.
- public GetLists(){
- var htmlContext = this;
- let siteContent:string = "";
- const context: SP.ClientContext = new SP.ClientContext(this.context.pageContext.web.absoluteUrl);
- const web: SP.Web = context.get_web();
- const lists: SP.ListCollection = web.get_lists();
- context.load(lists);
- context.executeQueryAsync(function () {
- var listenum = lists.getEnumerator();
- while (listenum.moveNext()) {
- var listItem = listenum.get_current();
- siteContent += `<div>
- <span>${listItem.get_title()}</span>
- </div>`;
- }
- htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;
- },
- function(sender,args){
- console.log(args.get_message());
- });
- }
Get list by Title
Let us see retrieving the required list, using the title with JSOM approach.
Like the above method, the context is used to retrieve the list collection, using the Web object with context URL. From the list collection, the required list is extracted with getbytitle() method. Now, the list is loaded and executed, using the context. In the success method, the properties are displayed.
- public GetList(){
- var htmlContext = this;
- let siteContent:string = "";
- const context: SP.ClientContext = new SP.ClientContext(this.context.pageContext.web.absoluteUrl);
- const web: SP.Web = context.get_web();
- const lists: SP.ListCollection = web.get_lists();
- const list: SP.List = lists.getByTitle("TestList");
- context.load(list);
- context.executeQueryAsync(function () {
- siteContent += `<div>
- <div><h2>Title : ${list.get_title()}</h2></div>
- <div>Description : ${list.get_description()}</div>
- <div>Template : ${list.get_baseTemplate()}</div>
- <div>Total Items : ${list.get_itemCount()}</div>
- </div>`;
- htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;
- },
- function(sender,args){
- console.log(args.get_message());
- });
- }

Join the conversation! Your thoughts help the community grow.