List/Library Field(Column) Manipulation Using PnP JS Core

PnP-JS-Core library is a reusable library, developed by Microsoft and community contributors. This library is designed mainly to make common SharePoint tasks easy and spontaneous. It contains many fluent API for working with the SharePoint REST API. Please refer to the following site to know more about the PnP-JS-Core library.

Here is a good C-Sharp corner article, which describes about the basic functionality of Pnp-JS-Core.

In this article, we will see the following concepts about the fields in a List/Library.
  • List of properties, which can be obtained for each field.
  • Add or Delete a field.
  • Modify the properties of a field.

To use PnP-JS-core in our JavaScript, we need to include few JS files, which can be downloaded from the following link.

If we want to make the script work in Internet Explorer, we have to additionally add two more JS files ,which can be downloaded from the following link.

Once, we have downloaded all the needed JS files, we can upload them to our SharePoint site Library like Site Assets and include them in our JavaScript.

  1. <script type=”text/javascript” src=”/siteassets/scripts/pnp.js”></script>  
  2. <script>  
  3.     //our code  
  4. </script>  
Details about Fields

We can retrieve all the details about the fields of a list by running the following command in the Browser console.
  1. $pnp.sp.web.lists.getByTitle("My List").fields.get();   
code

Properties of Field

With the command, shown above, we can get all the details about the properties of the fields also.

log

The list of properties, which are available for a field are as follows: 
  • AutoIndexed
  • CanBeDeleted
  • DefaultValue
  • Description
  • Direction
  • EnforceUniqueValues
  • EntityPropertyName
  • FieldTypeKind
  • Filterable
  • FromBaseType
  • Group
  • Hidden
  • Id
  • Indexed
  • InternalName
  • JSLink
  • MaxLength
  • ReadOnlyField
  • Required
  • SchemaXml
  • Scope
  • Sealed
  • Sortable
  • StaticName
  • Title
  • TypeAsString
  • TypeDisplayName
  • TypeShortDescription
  • ValidationFormula
  • ValidationMessage
  • odata.editLink
  • odata.id
  • odata.type

Methods: List of avalable methods to handle the fields are as follows:

  • add
  • addCalculated
  • addCurrency
  • addDateTime
  • addMultilineText
  • addNumber
  • addText
  • addUrl
  • constructor
  • createFieldAsXml
  • getById
  • getByTitle

Add a new Field to the List

We can add the fields to the list, using script. To add a field with a single line of text, we can use the following script:

  1. <script type=”text/javascript” src=”/siteassets/scripts/pnp.js”></script>  
  2. <script type="text/javascript">  
  3.     $pnp.sp.web.lists.getByTitle("My List").fields.addText('Testing Field').then(function(result) {  
  4.         alert('Field created successfully');  
  5.     });  
  6. </script>  
Access a Field in the List

To access a particular field, we can either use (a) getById or (b) getByTitle methods. 
  1. getById

    We can find the ID of a field by expanding the result set of the particular field object in the Browser console or alternatively, we can execute the script by passing the Title of the field and getting the ID.
    1. <script type=”text/javascript” src=”/siteassets/scripts/pnp.js”></script>  
    2. <script type="text/javascript">  
    3.     $pnp.sp.web.lists.getByTitle("My List").fields.getByTitle('Testing Field').get().then(function(result) {  
    4.         alert(result.Id)  
    5.     });  
    6. </script>  
    The following image shows the ID of the field testing field.

    Id

    Once we obtain the ID, we can get the details about the particular field with the following script.
    1. <script type=”text/javascript” src=”/siteassets/scripts/pnp.js”></script>  
    2. <script type="text/javascript">  
    3.     $pnp.sp.web.lists.getByTitle("My List").fields.getById('2de15c93-7250-4aae-ab64-494e3efea4aa').get().then(function(result) {  
    4.         alert(result.Id + "\n" + result.Title + "\n" + result.Description + "\n" + result.Required)  
    5.     });  
    6. </script>  
  2. getByTitle

    Alternatively, we can access a field with its Title and following is the command for it: 
    1. $pnp.sp.web.lists.getByTitle("My List").fields.getByTitle('Testing Field').get();   
    The script, shown above, can be converted to use the getByTitle function.

Delete a field from the List

We can delete a field from a List by using the script, shown below:

  1. $pnp.sp.web.lists.getByTitle("My List").fields.getById('0374823b-66d5-4b8b-919d-ee5c21e20a75').delete();   
(OR)
  1. $pnp.sp.web.lists.getByTitle("My List").fields.getByTitle('Testing Field').delete();   
The script would look like, as shown below:
  1. <script type=”text/javascript” src=”/siteassets/scripts/pnp.js”></script>  
  2. <script type="text/javascript">  
  3.     $pnp.sp.web.lists.getByTitle("My List").fields.getByTitle('Testing Field').delete().then(function(result) {  
  4.         alert('Field deleted successfully');  
  5.     });  
  6. </script>  
Update a property of a Field in List

We can update the properties of the field by setting the value for it with the following script.

To update the Testing Field as Mandatory field ( Its required that this column contains information),
  1. <script type=”text/javascript” src=”/siteassets/scripts/pnp.js”></script>  
  2. <script type="text/javascript">  
  3.     $pnp.sp.web.lists.getByTitle("My List").fields.getByTitle('Testing Field').update({  
  4.         Required: true  
  5.     }).then(function(result) {  
  6.         alert('Field Updated successfully');  
  7.     });  
  8. </script>  
To update the Description of the field.
  1. <script type=”text/javascript” src=”/siteassets/scripts/pnp.js”></script>  
  2. <script type="text/javascript">  
  3.     $pnp.sp.web.lists.getByTitle("My List").fields.getByTitle('Testing Field').update({  
  4.         Description: 'Test Description'  
  5.     }).then(function(result) {  
  6.         alert('Field Updated successfully');  
  7.     });  
  8. </script>  
Conclusion

So in this article we learned the list of properties and methods to handle fields available in a List/Library. The scripts can be tested by adding them to a content editor or script editor web part. This code can be alternatively used in an Add-in also. Using the pnp-core js we can achieve most of the field manipulation actions in a List/Library through client side scripting itself.

Up Next
    Ebook Download
    View all
    Learn
    View all