Introduction
In this article, you will learn how to perform the basic field/column operations (both site scoped and list scoped columns) on SharePoint sites or lists, using TypeScript, with the help of the classes and the variables. In this sample, the field operations for the site and the list are implemented, using the inheritance concept.
In my previous article, you saw how to create, retrieve, update, and delete the site fields with the help of an Object Oriented approach.
To learn about TypeScript basics and for the prerequisites to be available on Visual Studio for SharePoint TypeScript programming, you can refer to the article given below.
In my other article, I explained about implementing TypeScript basic examples in SharePoint Visual Studio projects.
This article focuses more on implementing the site column operations for SharePoint sites and the lists, using ECMA script, with the help of TypeScript, using Object Oriented programming concepts (with inheritance).
Class
The class holds the necessary members. This means, the class will hold the necessary variables, constructor and the functions. In this example, two classes will be considered.
  • The base class will hold all the necessary functions with the variables and the constructors related to the site operations.
  • Another class will hold only the variables and the constructor, inheriting the base class.
Variables
The necessary variables, common for all the functions, are defined as the member variables. In this case, all the variables to be defined in the base class are given below.
  1. public context: SP.ClientContext;
  2. public site: SP.Site;
  3. public web: SP.Web;
  4. public fields: SP.FieldCollection;
The snippet given below shows the variables used for the second class. The other variables are inherited from the base class.
  1. public lists: SP.ListCollection;
  2. public list: SP.List;
  3. public listFields: SP.FieldCollection;
Constructor
The base class constructor is defined with no parameter. Here, the context of the site is set, the respective Web object is retrieved, and then the site fields are retrieved, using the corresponding methods.
  1. constructor() {
  2. this.context = new SP.ClientContext();
  3. this.site = this.context.get_site();
  4. this.web = this.site.get_rootWeb();
  5. this.fields = this.web.get_fields();
  6. }
Another class holds the constructor with the list name as a parameter. Since the class inherits the base class, only list object and the list fields are retrieved, using the corresponding methods.
  1. constructor(listName: string) {
  2. super();
  3. this.lists = this.web.get_lists();
  4. this.list = this.lists.getByTitle(listName);
  5. this.fields = this.list.get_fields();
  6. }
Functions
The required functions are defined in the base class. In this sample, the basic field operations mentioned below are defined, using the functions. The functions are inherited to the list class as well.
1. Retrieve Fields
The fields available in the site/list are retrieved. For the site fields, the field collection is retrieved with the help of the Web object. For the list fields, the collection is retrieved from the list object.
2. Retrieve Field
The field from the Web/list object is retrieved, using the field title or name. After retrieving all the fields, using the operation mentioned above, the particular field is accessed, using the getbytitle method. Subsequently, the query will be executed. The input parameter is the field title.
3. Create Field
The field can be created on the site collection with the help of field XML and the necessary content type information. It is added to the field collection (based on the field scope). The sample given below shows creating a text field.
4. Delete Field
The field to be deleted needs to be retrieved, using the retrieval operation, mentioned above. Now, the field object is deleted, using the delete method before executing the query.
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 functions defined inside both the classes.
  1. // Variables and functions related to site
  2. class SPFieldOperations {
  3. // Variables or Objects
  4. public context: SP.ClientContext;
  5. public site: SP.Site;
  6. public web: SP.Web;
  7. public fields: SP.FieldCollection;
  8. // Set objects
  9. constructor() {
  10. this.context = new SP.ClientContext();
  11. this.site = this.context.get_site();
  12. this.web = this.site.get_rootWeb();
  13. this.fields = this.web.get_fields();
  14. }
  15. // Retrieves all fields from site collection
  16. public GetFields() {
  17. let availFields = this.fields;
  18. this.context.load(availFields);
  19. this.context.executeQueryAsync(
  20. function () {
  21. let siteColumns = availFields.getEnumerator();
  22. while (siteColumns.moveNext()) {
  23. let siteColumn = siteColumns.get_current();
  24. console.log(siteColumn.get_title());
  25. }
  26. console.log("Total Fields Available: " + availFields.get_count());
  27. },
  28. function (sender, args) {
  29. console.log(args.get_message());
  30. }
  31. );
  32. }
  33. // Retrieves particular field
  34. public GetField(columnName: string) {
  35. let availField = this.fields.getByTitle(columnName);
  36. this.context.load(availField);
  37. this.context.executeQueryAsync(
  38. function () {
  39. console.log(availField.get_id());
  40. console.log(availField.get_description());
  41. console.log(availField.get_defaultValue());
  42. console.log(availField.get_group());
  43. console.log(availField.get_scope());
  44. },
  45. function (sender, args) {
  46. console.log(args.get_message());
  47. }
  48. );
  49. }
  50. ;
  51. // Creates a field
  52. public CreateField(columnName: string) {
  53. let newField = this.context.castTo(this.fields.addFieldAsXml('<Field Type="Text" DisplayName="' + columnName + '" Name="' + columnName+'" />',
  54. true, SP.AddFieldOptions.addToDefaultContentType),
  55. SP.FieldText);
  56. this.context.load(newField);
  57. this.context.executeQueryAsync(
  58. function () {
  59. console.log("Column Created");
  60. },
  61. function (sender, args) {
  62. console.log(args.get_message());
  63. }
  64. );
  65. }
  66. // Deletes a field
  67. public DeleteField(columnName: string) {
  68. let existingColumn = this.fields.getByTitle(columnName);
  69. existingColumn.deleteObject();
  70. this.context.executeQueryAsync(
  71. function () {
  72. console.log("Column Deleted");
  73. },
  74. function (sender, args) {
  75. console.log(args.get_message());
  76. }
  77. );
  78. }
  79. }
  80. // Variables required for list operations
  81. class SPListFieldOperations extends SPFieldOperations {
  82. // Variables or Objects
  83. public lists: SP.ListCollection;
  84. public list: SP.List;
  85. public listFields: SP.FieldCollection;
  86. // Set objects
  87. constructor(listName: string) {
  88. super();
  89. this.lists = this.web.get_lists();
  90. this.list = this.lists.getByTitle(listName);
  91. this.fields = this.list.get_fields();
  92. }
  93. }
Execution
The classes are instantiated with the help of the objects. Context of the site is set, using the constructors. The code snippet, mentioned below, triggers the members defined inside the classes. The comments are provided along with the function calls to explain every step in detail.
  1. // Loads necessary files
  2. $(document).ready(function () {
  3. SP.SOD.executeOrDelayUntilScriptLoaded(function () {
  4. SP.SOD.executeOrDelayUntilScriptLoaded(() => GetData(), 'SP.js');
  5. }, 'SP.RunTime.js');
  6. });
  7. // Instantiate object and call the methods
  8. function GetData() {
  9. let siteFieldsOps = new SPFieldOperations();
  10. // Uncomment necessary operations
  11. //siteFieldsOps.GetFields(); // Retrieves all fields from site
  12. //siteFieldsOps.GetField('TestSiteColumn'); // Retrieves required field from site
  13. //siteFieldsOps.CreateField('TestSiteColumn'); // Creates a field on a site
  14. //siteFieldsOps.DeleteField('TestSiteColumn'); // Deletes a field on a site
  15. let listFieldsOps = new SPListFieldOperations("TestList");
  16. // Uncomment necessary operations
  17. //listFieldsOps.GetFields(); // Retrieves all fields from list
  18. //listFieldsOps.GetField('TestListColumn'); // Retrieves required field from list
  19. //listFieldsOps.CreateField('TestListColumn'); // Creates a field on a list
  20. //listFieldsOps.DeleteField('TestListColumn'); // Deletes a field on a list
  21. }
Summary
Thus, you have learned about creating, retrieving, or deleting the fields or columns on the site and list, using TypeScript with an Object Oriented approach (with inheritance).