top of page
Writer's pictureNakkeeran Natarajan

SharePoint Basic List Item Operations Using TypeScript

Introduction

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


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 list item operations using ECMA script with the help of TypeScript and object oriented programming concepts.


Class The class will hold the necessary members. This means, the class will hold the necessary variables, constructors and the defined functions. Variables 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. public context: SP.ClientContext;  

  2. public web: SP.Web;  

  3. public list: SP.List;  

  4. public listCollection: SP.ListCollection;  

  5. public listName: string;  

Constructor

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

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

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

  5. this.listCollection = this.web.get_lists();  

  6. this.listName = "TestList";  

  7. }

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

  • Retrieve List Items The list items available on the list are retrieved. From the list collection property defined above, the list is retrieved using getbytitle method and then the items are retrieved with necessary query.

Note Null query retrieves all list items.

  • Retrieve List Item The list item is retrieved, using the item id. Using the list collection variable defined above, the list object is retrieved using getbytitle method and then the item is retrieved using getbyid method. The input parameter is item ID.

  • Create List Item The item can be created on the list using TypeScript. The necessary input parameters are set using list item object, with the help of ListItemCreationInformation method. Using the list object retrieved, the item can be created with add method and input object.

  • Delete List Item The list item can be deleted from the list. The list item is accessed by retrieving list item operation. Subsequently, delete method is used for list item object for deletion.

The collection/objects need to be loaded and executed, using the context object (defined in the constructor). The code snippet, mentioned below shows the list item functions defined inside the class.

  1. class ListItemOperations 

  2. {  

  3. public context: SP.ClientContext;  

  4. public web: SP.Web;  

  5. public list: SP.List;  

  6. public listCollection: SP.ListCollection;  

  7. public listName: string;       

  8. constructor() 

  9. {  

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

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

  12. this.listCollection = this.web.get_lists();  

  13. this.listName = "TestList";      

  14. }  

  15. public GetListItems() 

  16. {  

  17. var query = new SP.CamlQuery();   var listObj = this.listCollection.getByTitle(this.listName).getItems(query);  

  18. this.context.load(listObj);  

  19. this.context.executeQueryAsync(function () 

  20. {  

  21. var listItems = listObj.getEnumerator();  

  22. while (listItems.moveNext()) 

  23. {  

  24. var listItem = listItems.get_current();                  

  25. console.log(listItem.get_item("Title"));              

  26. }          

  27. }, 

  28. function () {          }

  29. );      

  30. }  

  31. public CreateListItem() 

  32. {  

  33. var listItemInfo = new SP.ListItemCreationInformation();   this.list = this.listCollection.getByTitle(this.listName);   var newListItem = this.list.addItem(listItemInfo);          

  34. newListItem.set_item("Title", "ghi");          

  35. newListItem.update();  

  36. this.context.load(newListItem);  

  37. this.context.executeQueryAsync(function () 

  38. {              

  39. console.log("Item Added"):              

  40. console.log(newListItem.get_item("Title"));          

  41. }, 

  42. function (sender, args) {          }

  43. );      

  44. }  

  45. public GetListItem() 

  46. {  

  47. this.list = this.listCollection.getByTitle(this.listName);  

  48. var listItem = this.list.getItemById(1);  

  49. this.context.load(listItem);  

  50. this.context.executeQueryAsync(function () 

  51. {              

  52. console.log(listItem.get_item("Title"));          

  53. }, 

  54. function () {          }

  55. );      

  56. }  

  57. public DeleteListItem() 

  58. {  

  59. this.list = this.listCollection.getByTitle(this.listName);  

  60. var listItem = this.list.getItemById(3);          

  61. listItem.deleteObject();  

  62. this.context.executeQueryAsync(function () 

  63. {              

  64. console.log("List Item Deleted")          

  65. }, 

  66. function () {          }

  67. );      

  68. }  

  69. }  

Execution The class is instantiated with the help of an object. The code snippet, mentioned below triggers the members defined inside the ListItemOperations class.

  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 listOpsObj = new ListItemOperations();      

  12. listOpsObj.GetListItems();  

  13. // listOpsObj.CreateListItem();

  14. // listOpsObj.GetListItem();

  15. // listOpsObj.DeleteListItem();

  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 to above). Summary Thus, you have learned performing the basic SharePoint list item operations, using TypeScript programming with an object oriented approach.

0 comments

Comments


bottom of page