top of page

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. {  

  3. public context: SP.ClientContext;  

  4. public web: SP.Web;      

  5. constructor() 

  6. {  

  7. this.context = SP.ClientContext.get_current();  

  8. this.web = this.context.get_web();      

  9. }  

  10. public GetLists() 

  11. {  

  12. var webInfo = this.web;  

  13. var contextInfo = this.context;          

  14. contextInfo.load(webInfo);          

  15. contextInfo.executeQueryAsync(function () 

  16. {              

  17. console.log(webInfo.get_title());          

  18. }, 

  19. function () {          }

  20. );      

  21. }  

  22. }  

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. {      

  3. SP.SOD.executeOrDelayUntilScriptLoaded(function () 

  4. {          

  5. SP.SOD.executeOrDelayUntilScriptLoaded(() => GetData(), 'SP.js');      

  6. },

  7.  'SP.RunTime.js');  

  8. });  

  9. function GetData() 

  10. {      

  11. let getSite = new GetSiteInfo();      

  12. getSite.GetLists();  

  13. }  


  • 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.


Thus, you have learned about implementing the class, constructors and methods of TypeScript on SharePoint programming.



bottom of page