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 ListWe can add a new column to the list by making use of the following code.
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.
We can see the details of the new column (Datatype and others) by browsing the List Settings.
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.
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.
Operation: How to update column of List
Let’s consider that we have to add little description the field title, which is blank currently
We can update an existing column to the list by making use of the following code.
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
We can see description of Title field is updated to the list by browsing the field properties under list settings
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-
And we can also verify the list settings to ensure that “ProductOwner” Column is not added to the list earlier
Now we will look into code to add existing Site Column to the list as explained below.
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.
We can further look into column details by clicking on it
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.
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.
We can see this value shown as default value whenever a new Item has been created (programmatically or using browser) as shown below.
Code Snippets
- function AddFieldToList() {
- web = $clientContext.Web;
- $list = $web.Lists.GetByTitle("Products");
- $schemaXML = "<Field DisplayName='CustomField' Type='Text' />";
- $list.Fields.AddFieldAsXml($schemaXML, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::DefaultValue);
- $clientContext.ExecuteQuery();
- }
-
- function GetListFields() {
- $web = $clientContext.Web;
- $list = $web.Lists.GetByTitle("ProductsDocuments");
- $fieldColl = $list.Fields
- $clientContext.Load($fieldColl);
- $clientContext.ExecuteQuery();
- foreach($field in $fieldColl) {
- Write - Host - ForegroundColor Green "Field Name: "
- $field.Title " ID: "
- $field.ID
- }
- }
-
- function UpdateListField() {
- $web = $clientContext.Web;
- $list = $web.Lists.GetByTitle("Products");
- $field = $list.Fields.GetByTitle("Title");
- $field.Description = "New Title updated by PCSOM";
- $field.Update();
- $clientContext.Load($field);
- $clientContext.ExecuteQuery();
- Write - Host - ForegroundColor Green "Field Name: "
- $field.Title " Description: "
- $field.Description
- }
-
- function AddExistingFieldToList() {
- $web = $clientContext.Web;
- $list = $web.Lists.GetByTitle("Products");
- $field = $web.Fields.GetByTitle("Categories");
- $list.Fields.Add($field);
- $clientContext.ExecuteQuery();
- }
-
- function SetDefaultValueForField() {
- $web = $clientContext.Web;
- $list = $web.Lists.GetByTitle("Products");
- $field = $list.Fields.GetByTitle("CustomField");
- $field.DefaultValue = "Default";
- $field.Update();
- $clientContext.ExecuteQuery();
- }
That is all for this demo.
Hope you find it helpful.