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 with this demo we will start with a list called “Products” and perform all operations on this list.

1

Operation: How to Add New Columns To List

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

2

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 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 function that we have explained in Step 1-5

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

4

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

5

Operation: How Get All Columns of List

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

6

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 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 function that we have explained in Step 1-6

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

7

Operation: How to Update column of List

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

8

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

9

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

10

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

11

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-

12

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

13

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

14

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 function that we have explained in Step 1-5

We can see the field collection to the list by browsing the list settings-

16

We can further look into column details by clicking it

17

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-

18

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 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 the a success message which is informing users about the status of operation

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

19

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

20

That is all for this demo.

Hope you find it helpful.

Leave a comment