SharePoint Online - Working With List Fields Using PowerShell

In this article, we will discuss the operations on List Fields (Columns), which involves getting All Columns, adding New Columns, updating Existing Columns, and so on.

To start this demo, we will start with a list called “Products” and perform all operations on this list.

Operation - How to Add New Columns To List

We can add a new column to the list by making use of the following code.

Working With List Fields Using PowerShell

In Step 1, we will get the object reference to the current Web using Client Context properties

In Step 2, we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3, we will define the XML of the List Column schema. You can get this XML by prototyping the list using SharePoint UI and then by using SharePoint Client Browser to look for Schema XML for Lists & Fields.

In Step 4, we will call “AddFieldAsXml” method to add the field as XML Schema to the list.

In Step 5, we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method.

In Step 6, we will display the success message to the users.

In Step 7, we will call a function that we have explained in Step 1-5.

We can see this field added to the list by browsing the list.

Working With List Fields Using PowerShell

We can see the details of the new column (Datatype and others) by browsing the List Settings.

Working With List Fields Using PowerShell
Operation - How to get All Columns of List

We can get all the columns used in a list by making use of the following code.

Working With List Fields Using PowerShell

In Step 1, we will get the object reference to the current Web using Client Context properties.

In Step 2, we will get the object reference to the respective list by calling “GetByTitle” method.

In Step 3, we will get the object reference to Fields collection of the list.

In Step 4, we call the “Load” function to retrieve Fields collection properties from the server.

In Step 5, we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method.

In Step 6, we will loop through the collection and display Field details to the users.

In Step 7, we will call the function that we have explained in Step 1-6.

We can see the fields collection to the list by browsing the list.

Working With List Fields Using PowerShell
Operation: How to update column of List

Let’s consider that we have to add little description the field title, which is blank currently

Working With List Fields Using PowerShell

We can update an existing column to the list by making use of the following code.

Working With List Fields Using PowerShell

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to the respective field by calling “GetByTitle” method

In Step 4 we will set the Description property of List object with the required value

In Step 5 we will call “Update” method to save these changes back to SharePoint List

In Step 6 we will call “Load” method to retrieve updated properties (Description) of the field from Server

In Step 7 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 8 we will display Field details to the users to the users

In Step 7 we will call function that we have explained in Step 1-8

Working With List Fields Using PowerShell

We can see description of Title field is updated to the list by browsing the field properties under list settings

Working With List Fields Using PowerShell
Operation - How To Add Existing Site Columns To List

Since we are planning to add an existing Site Column to the list, it is necessary to ensure the existence of Site Column. We can verify this by navigating “Site Settings > Site Columns”.

For this demo I already have added a Site Column “ProductOwner” that we can see under “Custom Columns” group as shown below-

Working With List Fields Using PowerShell

And we can also verify the list settings to ensure that “ProductOwner” Column is not added to the list earlier

Working With List Fields Using PowerShell

Now we will look into code to add existing Site Column to the list as explained below.

Working With List Fields Using PowerShell

In Step 1, we will get the object reference to the current Web using Client Context properties

In Step 2, we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3, we will get the object reference to Fields collection of the Web. It is important to note that Site Columns are the part of Web Fields Collection not List Fields Collection. So we have to make use of Web object reference to look for existing Site Columns.

In Step 4, we call the “Add” function on “Fields” Collection of the list to add the reference of the Site Column from Step 3

In Step 5, we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6, we will call the function that we have explained in Step 1-5

We can see the field collection in the list by browsing the list settings.

Working With List Fields Using PowerShell

We can further look into column details by clicking on it

Working With List Fields Using PowerShell
Operation - How Set Default Value For Field

We can set default values to SharePoint List Fields programmatically by using the following code as explained below.

Working With List Fields Using PowerShell

In Step 1, we will get the object reference to the current Web using Client Context properties.

In Step 2, we will get the object reference to the respective list by calling “GetByTitle” method.

In Step 3, we will get the object reference to Fields collection of the list.

In Step 4, we will call “DefaultValue” property of Field Object and assign it a value of our choice.

In Step 5, we will call “Update” method of Field Object, which will update the “DefaultValue” property back into the database.

In Step 6, we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 7, we will display a success message which is informing users about the status of the operation.

In Step 8, we will call the function that we have explained in Step 1-7.

Working With List Fields Using PowerShell

We can see this value shown as default value whenever a new Item has been created (programmatically or using browser) as shown below.

Working With List Fields Using PowerShell
Code Snippets
  1. function AddFieldToList() {  
  2.     web = $clientContext.Web;  
  3.     $list = $web.Lists.GetByTitle("Products");  
  4.     $schemaXML = "<Field DisplayName='CustomField' Type='Text' />";  
  5.     $list.Fields.AddFieldAsXml($schemaXML, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::DefaultValue);  
  6.     $clientContext.ExecuteQuery();  
  7. }  
  8.   
  9. function GetListFields() {  
  10.     $web = $clientContext.Web;  
  11.     $list = $web.Lists.GetByTitle("ProductsDocuments");  
  12.     $fieldColl = $list.Fields  
  13.     $clientContext.Load($fieldColl);  
  14.     $clientContext.ExecuteQuery();  
  15.     foreach($field in $fieldColl) {  
  16.         Write - Host - ForegroundColor Green "Field Name: "  
  17.         $field.Title " ID: "  
  18.         $field.ID  
  19.     }  
  20. }  
  21.   
  22. function UpdateListField() {  
  23.     $web = $clientContext.Web;  
  24.     $list = $web.Lists.GetByTitle("Products");  
  25.     $field = $list.Fields.GetByTitle("Title");  
  26.     $field.Description = "New Title updated by PCSOM";  
  27.     $field.Update();  
  28.     $clientContext.Load($field);  
  29.     $clientContext.ExecuteQuery();  
  30.     Write - Host - ForegroundColor Green "Field Name: "  
  31.     $field.Title " Description: "  
  32.     $field.Description  
  33. }  
  34.   
  35. function AddExistingFieldToList() {  
  36.     $web = $clientContext.Web;  
  37.     $list = $web.Lists.GetByTitle("Products");  
  38.     $field = $web.Fields.GetByTitle("Categories");  
  39.     $list.Fields.Add($field);  
  40.     $clientContext.ExecuteQuery();  
  41. }  
  42.   
  43. function SetDefaultValueForField() {  
  44.     $web = $clientContext.Web;  
  45.     $list = $web.Lists.GetByTitle("Products");  
  46.     $field = $list.Fields.GetByTitle("CustomField");  
  47.     $field.DefaultValue = "Default";  
  48.     $field.Update();  
  49.     $clientContext.ExecuteQuery();  

That is all for this demo.

Hope you find it helpful.