SharePoint Programming With TypeScript - Part Two

Introduction
 
In this article, you will learn implementing basic TypeScript examples on SharePoint, using Visual Studio.
 
In my previous article, you have seen/learned the TypeScript basics and prerequisites to be available on Visual Studio for SharePoint TypeScript programming.
SharePoint Project with plugins
 
The snapshot, shown below shows the project created with the necessary plugin files. By default, the files will be present under the scripts folder. For this example, I have dragged the files to StyleLibrary folder.
 
Note

In this case, the files to be used for the operations need to be managed/accessed from the site's Style Library.
 
 
 
Adding Custom Files
 
Add the custom type script file to the project. Once you start putting your code into the TypeScript file, the compiler will translate the content from TypeScript to JavaScript code. JavaScript file will be generated automatically. JavaScript file is usually hidden. Please find the steps , mentioned below to include the hidden files in the project. 
  • From the Solution Explorer, click on the project.
  • Click on show all files icon. All the hidden files will be shown.
  • Right click on Sample.js file, select Include In Project method.
  • Now, JavaScript file will hold the content translated from TypeScript file.


The snapshot, shown above shows the project structure, before and after including JavaScript files.
 
TypeScript & JavaScript Code
 
As a basic example, we will see how to get the information about the site. In the sample TypeScript file, paste the code, mentioned below and save it.
  1. /// <reference path="../../StyleLibrary/TSSample/typings/jquery/jquery.d.ts"/>  
  2. /// <reference path="../../StyleLibrary/TSSample/typings/sharepoint/SharePoint.d.ts"/>  
  3.   
  4. $(document).ready(function () {  
  5.     SP.SOD.executeOrDelayUntilScriptLoaded(function () {  
  6.         SP.SOD.executeOrDelayUntilScriptLoaded(()=>GetSiteDetails.LoadLists(), 'SP.js');  
  7.     }, 'SP.RunTime.js');  
  8.   
  9. });  
  10.   
  11. module GetSiteDetails {  
  12.     var context;  
  13.     var web;  
  14.     export function LoadLists() {  
  15.         context = SP.ClientContext.get_current();  
  16.         web = context.get_web();  
  17.         context.load(web);  
  18.         context.executeQueryAsync(success, failure);  
  19.     }  
  20.   
  21.     function success() {  
  22.         $("#siteName").html("<div> " + web.get_title() + " Site Info :: </div>");  
  23.         $("#siteName").append("<div> Description : " + web.get_description() + "</div>");  
  24.         $("#siteName").append("<div> Web Template : " + web.get_webTemplate() + "</div>");  
  25.           
  26.     }  
  27.     function failure() {  
  28.     }  
  29. }  
The code will be compiled and JavaScript code will be generated in JavaScript file. JavaScript content will be similar to the code snippet, mentioned below. Do not change anything in JavaScript file manually. 
  1. /// <reference path="../../StyleLibrary/TSSample/typings/jquery/jquery.d.ts"/>  
  2. /// <reference path="../../StyleLibrary/TSSample/typings/sharepoint/SharePoint.d.ts"/>  
  3. $(document).ready(function () {  
  4.     SP.SOD.executeOrDelayUntilScriptLoaded(function () {  
  5.         SP.SOD.executeOrDelayUntilScriptLoaded(function () { return GetSiteDetails.LoadLists(); }, 'SP.js');  
  6.     }, 'SP.RunTime.js');  
  7. });  
  8. var GetSiteDetails;  
  9. (function (GetSiteDetails) {  
  10.     var context;  
  11.     var web;  
  12.     function LoadLists() {  
  13.         context = SP.ClientContext.get_current();  
  14.         web = context.get_web();  
  15.         context.load(web);  
  16.         context.executeQueryAsync(success, failure);  
  17.     }  
  18.     GetSiteDetails.LoadLists = LoadLists;  
  19.     function success() {  
  20.         $("#siteName").html("<div> " + web.get_title() + " Site Info :: </div>");  
  21.         $("#siteName").append("<div> Description : " + web.get_description() + "</div>");  
  22.         $("#siteName").append("<div> Web Template : " + web.get_webTemplate() + "</div>");  
  23.     }  
  24.     function failure() {  
  25.     }  
  26. })(GetSiteDetails || (GetSiteDetails = {}));  
  27. //# sourceMappingURL=Sample.js.map  
Test the Code
 
To test the functionality, add JavaScript file reference to the page and test it. TypeScript file will not be referred to anywhere in the project. 
 
Note

The operation, mentioned below can be completed manually as well. 
  • Since we are referring to jQuery plugin, add in in the style library folder of Solution explorer. Add the text file to the Style Library and paste the code, mentioned below.
    1. <script type="text/javascript" src="/Style Library/TSSample/jquery-1.11.1.min.js"></script>  
    2. <script type="text/javascript" src="/Style Library/TSSample/Sample.js"></script>  
    3.   
    4. <div id="siteName"></div>  
  • Right click on the project and deploy.
  • The files will be present under http://siteurl/Style Library/TSSample.
  • Navigate to pages library. Create a page. Then add a content editor webpart to the page. (Content Editor webpart will be present under Media Content Category).
  • Edit the Webpart and provide the sample text file path URL in content link text box. Save the page. 
The snapshot, shown below shows the page, which shows the site details.
 
 

Summary
 
Thus, you have learned adding the TypeScript file to the SharePoint project and testing the JavaScript (translated from TypeScript code) on SharePoint page.

Up Next
    Ebook Download
    View all
    Learn
    View all