SharePoint Programming With TypeScript - Object Oriented Approach

Introduction
 
In this article, you will learn about implementing basic TypeScript examples on SharePoint, using classes and variables, using Visual Studio.
 
In my previous article, you have 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.
Here, we will learn how to retrieve SharePoint site details, using class, constructor, function and variables with the help of TypeScript. In this way, the programmers can develop the components in an object oriented programming language. With the help of ECMAScript approach in TypeScript, the developers can build Applications in object oriented class based approach.
 
Defining Class 
 
Let us define a class with the members. The members can be variables, constructors and functions. The class name in the example is GetSiteInfo. It has four members, which are as follows.
  • Property called context, which is of type 'SP.ClientContext'
  • Property called web, which is of type 'SP.Web'
  • Constructor is used to set the default values. In this case, the client context and Web objects are set.
  • Function to get necessary information.
The class is instantiated with the help of the object. During instantiation,
  • The properties will be loaded.
  • The constructor will set the necessary data for the properties.
In the constructor, the context object and Web object needs to be set, using the respective methods.  
 
In the function, define the local variables for context and Web. Using the local context variable, the Web object is loaded and the query needs to be executed. The code will be very similar to ECMAScript programming. You can also write your custom logic to get the necessary information.
 
Using the class object, the function is called with dot operator. Afterwards, the necessary information is displayed. 
 
The class will look, as shown below. The TypeScript sample, mentioned below gets the site title and displays it on the console.
  1. class GetSiteInfo {  
  2.     public context: SP.ClientContext;  
  3.     public web: SP.Web;  
  4.     constructor() {  
  5.         this.context = SP.ClientContext.get_current();  
  6.         this.web = this.context.get_web();  
  7.     }  
  8.     public GetLists() {  
  9.         var webInfo = this.web;  
  10.         var contextInfo = this.context;  
  11.         contextInfo.load(webInfo);  
  12.         contextInfo.executeQueryAsync(function () {  
  13.             console.log(webInfo.get_title());  
  14.         }, function () {  
  15.         });  
  16.     }  
  17. }   
The necessary SharePoint core files need to be loaded before executing/referring to the class. TTypeScript code shows the logic to load and calls the function defined in the class, mentioned above.
  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 getSite = new GetSiteInfo();  
  9.     getSite.GetLists();  
  10. }   
Note
  • Finally the type script needs to be translated to JavaScript. JavaScript file only should be uploaded and referred to from SharePoint pages. This has been explained in detail in my previous articles.
  • The sample code is attached in this article.  
The snapshot, shown below shows the console output on the Browser.
 
Summary
 
Thus, you have learned about implementing the class, constructors and methods of TypeScript on SharePoint programming.