SharePoint Basic List View Operations Using TypeScript

Introduction
 
In this article, you will learn how to perform basic list view operations in SharePoint, using TypeScript with the help of classes and variables. You will see how to create, retrieve, update, and delete the list views.
 
To learn about TypeScript basics and prerequisites to be available on Visual Studio for SharePoint TypeScript programming, you can refer to the following article.
In my other article, I have explained about implementing TypeScript basic examples in SharePoint Visual Studio SharePoint Project.
This article focuses more on implementing the list view operations using ECMA script with the help of TypeScript and object oriented programming concepts.
 
Class
 
Class holds the necessary members. This means, the class will hold necessary variables, constructors, and the defined functions.
 
Variables
 
Necessary variables, common for all the functions, are defined as the member variables. In this case, the variables are given below.
  1. public context: SP.ClientContext;  
  2. public web: SP.Web;  
  3. public list: SP.List;  
  4. public listCollection: SP.ListCollection;  
  5. public listName: string;  
  6. public listViews: SP.ViewCollection;  
Constructor
 
The constructor can be defined with no parameters or with parameters, depending on the requirement. In this case, the list name is passed as parameter. 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. The variables/objects set, using the construct, are context, web, list collection, list name, list and list views.
  1. constructor(listTitle: string) {  
  2.     this.context = SP.ClientContext.get_current();  
  3.     this.web = this.context.get_web();  
  4.     this.listCollection = this.web.get_lists();  
  5.     this.listName = listTitle;  
  6.     this.list = this.listCollection.getByTitle(this.listName);  
  7.     this.listViews = this.list.get_views();  
  8. }  
Functions
 
The required functions are defined inside the class. In this sample, the basic list view operations, mentioned below, are defined using the functions.
  1. Retrieve List Views
    The views available for the list are retrieved. Since the list views object is set already in the constructor, here, only the object is loaded and executed to get the necessary information with the help of context.

  2. Create List View
    The view can be created for the list using TypeScript. The necessary input parameters are set using list view object, with the help of ViewCreationInformation method. Using the list collection retrieved, the new view will be added with add method and input object. The input parameters can be title, view query, row limit, view fields, view type, etc. Only title is mandatory field and other parameters can be empty.

  3. Retrieve List View
    The list view is retrieved, using the view name. Using the list view collection object set above, the list view object is retrieved using getbytitle method. The input parameter is view name.

  4. Update List View
    The list view can be updated using TypeScript. The view needs to be retrieved using the title and then information can be changed for the view object.

  5. Delete List View
    The list view can be deleted for the list. The list view is accessed by retrieving list view operation. Subsequently, delete method is used for list view object for deletion.
Note

The collection/objects need to be loaded and executed for all the operations, using the context object (defined in the constructor). The code snippet, mentioned below, shows the list view functions defined inside the class.
  1. class ListViewOperations {  
  2.     public context: SP.ClientContext;  
  3.     public web: SP.Web;  
  4.     public list: SP.List;  
  5.     public listCollection: SP.ListCollection;  
  6.     public listName: string;  
  7.     public listViews: SP.ViewCollection;  
  8.   
  9.     constructor(listTitle: string) {  
  10.         this.context = SP.ClientContext.get_current();  
  11.         this.web = this.context.get_web();  
  12.         this.listCollection = this.web.get_lists();  
  13.         this.listName = listTitle;  
  14.         this.list = this.listCollection.getByTitle(this.listName);  
  15.         this.listViews = this.list.get_views();  
  16.     }  
  17.   
  18.     public GetListViews() {  
  19.         let views = this.listViews;  
  20.         this.context.load(views);  
  21.         this.context.executeQueryAsync(function () {  
  22.             let viewsEnum = views.getEnumerator();  
  23.             while (viewsEnum.moveNext()) {  
  24.                 let listView = viewsEnum.get_current();  
  25.                 console.log(listView.get_title());  
  26.             }  
  27.         }, function () {  
  28.         });  
  29.     }  
  30.   
  31.     public CreateListView() {  
  32.         let listView = new SP.ViewCreationInformation();  
  33.         let fields: string[] = ["Title""Author"];  
  34.   
  35.         listView.set_title("Custom View");  
  36.         listView.set_viewFields(fields);  
  37.         this.listViews.add(listView);  
  38.   
  39.         let query: string = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>abc</Value></Eq></Where>";  
  40.         listView.set_query(query);  
  41.         let viewType = SP.ViewType.none;  
  42.         listView.set_viewTypeKind(viewType);  
  43.   
  44.         let viewCollection = this.listViews;  
  45.         viewCollection.add(listView);  
  46.   
  47.         this.context.load(viewCollection);  
  48.         this.context.executeQueryAsync(function () {  
  49.             console.log("View Created");  
  50.         },  
  51.             function (sender, args) {  
  52.   
  53.             });  
  54.   
  55.     }  
  56.   
  57.     public GetListView() {  
  58.         let viewName: string = "Custom View";  
  59.         let view: SP.View = this.listViews.getByTitle(viewName);  
  60.         this.context.load(view);  
  61.         this.context.executeQueryAsync(function () {  
  62.             console.log(view.get_title());  
  63.         }, function () {  
  64.         });  
  65.     }  
  66.   
  67.     public UpdateListView() {  
  68.         let viewName: string = "Custom View";  
  69.         let listView: SP.View = this.listViews.getByTitle(viewName);  
  70.   
  71.         let query: string = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>def</Value></Eq></Where>";  
  72.         listView.set_viewQuery(query);  
  73.         listView.update();  
  74.   
  75.         this.context.load(listView);  
  76.         this.context.executeQueryAsync(function () {  
  77.             console.log("List View Updated");  
  78.         }, function () {  
  79.         });  
  80.     }  
  81.   
  82.     public DeleteListView() {  
  83.         let viewName: string = "Custom View";  
  84.         let listView: SP.View = this.listViews.getByTitle(viewName);  
  85.         listView.deleteObject();  
  86.         this.context.executeQueryAsync(function () {  
  87.             console.log("List View Deleted");  
  88.         }, function () {  
  89.         });  
  90.     }  
  91. }
Execution
 
The class is instantiated with the help of an object. The code snippet, mentioned below, triggers the members defined inside the ListViewOperations class.
  1. $(document).ready(function () {  
  2.     SP.SOD.executeOrDelayUntilScriptLoaded(function () {  
  3.         SP.SOD.executeOrDelayUntilScriptLoaded(() => GetData(), 'SP.js');  
  4.     }, 'SP.RunTime.js');  
  5.   
  6. });  
  7. function GetData() {  
  8.     let listName = "TestList";  
  9.     let listOpsObj = new ListViewOperations(listName);  
  10.     listOpsObj.GetListViews();  
  11.     // Uncomment necessary methods before execution  
  12.     //listOpsObj.CreateListView();  
  13.     //listOpsObj.GetListView();      
  14.     //listOpsObj.UpdateListView();  
  15.     //listOpsObj.DeleteListView();  
  16. }
Note
 
TypeScript files need to be converted to JavaScript and JavaScript file is referred to in the respective SharePoint pages (explained in the previous articles referred above).
 
Summary
 
Thus, you have learnt the basic list view operations that can be performed on SharePoint lists, using TypeScript programming with object oriented approach.