SharePoint Basic List Operations Using TypeScript


In this article, you will learn how to perform basic list operations in SharePoint using TypeScript with the help of classes and variables.

In my previous article, you learned TypeScript basics and prerequisites to be available on Visual Studio for SharePoint TypeScript programming.

In my other article, I have explained about implementing TypeScript basic examples in SharePoint Visual Studio SharePoint Project.

This article focuses more on implementing ECMA script with the help of TypeScript, using object oriented programming concepts.


The class will hold the necessary members. This means, the class will hold the necessary variables, constructors and the defined functions.


The necessary variables, which will be common for all the functions are defined as the member variables. In this case, the variables are given below.

  1. declared.  
  2. public context: SP.ClientContext;  
  3. public web: SP.Web;  
  4. public list: SP.List;  
  5. public listCollection: SP.ListCollection;  

The constructor is defined with no parameters. The constructor sets the necessary objects. The context of the site can be set, using the respective method. With the help of context object, the other variables can be set, using the respective methods.
  1. constructor() {  
  2.     this.context = SP.ClientContext.get_current();  
  3.     this.web = this.context.get_web();  
  4.     this.listCollection = this.web.get_lists();  
  5. }  

The required functions are defined inside the class. In this sample, the basic list operations, mentioned below, are defined, using the functions.


  1. Retrieve Lists
    The lists available on the site are retrieved. The list collection defined already is assigned to the local variable.

  2. Retrieve List
    The list is retrieved, using the list name. Using the list collection variable defined above, the list object is retrieved, using getbytitle method.

  3. Create List
    The list can be created on the site. The necessary input parameters are set, using listcreationinformation object. Using the list collection variable defined above, the list can be created with add method and input object.

  4. Delete List
    The list can be deleted from the site. The list is accessed by retrieve list operation. Subsequently, delete method is used for list object to delete a list.

The collection/objects need to be loaded and executed, using the context object (defined in the constructor).

The code snippet, mentioned below shows the functions defined inside the class.

  1. class ListOperations {  
  2.     public context: SP.ClientContext;  
  3.     public web: SP.Web;  
  4.     public list: SP.List;  
  5.     public listCollection: SP.ListCollection;  
  6.     constructor() {  
  7.         this.context = SP.ClientContext.get_current();.this.web = this.context.get_web();  
  8.         this.listCollection = this.web.get_lists();  
  9.     }  
  10.     public GetLists() {  
  11.         var availableLists = this.listCollection;  
  12.         this.context.load(availableLists);.this.context.executeQueryAsync(function() {  
  13.             var lists = availableLists.getEnumerator();  
  14.             while (lists.moveNext()) {  
  15.                 var list = lists.get_current();  
  16.                 console.log(list.get_title()); // Prints on the console  
  17.             }  
  18.         }, function() {});  
  19.     }  
  20.     public CreateList(listName: string) {  
  21.         var listInfo = new SP.ListCreationInformation();  
  22.         listInfo.set_title(listName);  
  23.         listInfo.set_description("List created using TypeScript");  
  24.         listInfo.set_templateType(SP.ListTemplateType.genericList);  
  25.         var newList = this.listCollection.add(listInfo);  
  26.         this.context.load(newList);  
  27.         this.context.executeQueryAsync(function() {  
  28.             console.log(newList.get_title()); // Prints on the console  
  29.         }, function(sender, args) {});  
  30.     }  
  31.     public GetList(listName: string) {  
  32.         var list = this.listCollection.getByTitle(listName);  
  33.         this.context.load(list);  
  34.         this.context.executeQueryAsync(function() {  
  35.             console.log(list.get_title()); // Prints on the console   
  36.         }, function() {});  
  37.     }  
  38.     public DeleteList(listName: string) {  
  39.         var list = this.listCollection.getByTitle(listName);  
  40.         list.deleteObject();  
  41.         this.context.executeQueryAsync(function() {  
  42.             console.log("List Deleted"); // Prints on the console  
  43.         }, function() {});  
  44.     }  
  45. }  

The class is instantiated with the help of an object. The code snippet, mentioned below triggers the members defined inside the ListOperations class.
  1. $(document).ready(function() {  
  2.     SP.SOD.executeOrDelayUntilScriptLoaded(function() {  
  3.         SP.SOD.executeOrDelayUntilScriptLoaded(() => GetData(), 'SP.js');  
  4.     }, 'SP.RunTime.js');  
  5. });  
  7. function GetData() {  
  8.     let listOpsObj = new ListOperations();  
  9.     listOpsObj.GetLists();  
  10.     listOpsObj.CreateList();  
  11.     listOpsObj.GetList();  
  12.     listOpsObj.DeleteList();  
  13. }  


  • TypeScript files needs to be converted to JavaScript and JavaScript file is referred in the respective SharePoint pages (explained in the previous articles referred to above).
  • The sample code is attached in this article for the reference.


Thus, you have learned performing the basic SharePoint list operations, using TypeScript programming with an object oriented approach.