SharePoint Online - Working with Lists Using PowerShell

In this article, we will discuss SharePoint List operations using PowerShell for SharePoint Online.

To start with this demo, I have created a sample SharePoint Online Site that I will use to perform SharePoint operations in this and a few of the upcoming articles in this series.

SharePoint

In case you are not aware of how to start with PowerShell development for SharePoint Online, I would  recommend you to first go through one of my previous articles. This article will help you to set up the development environment and get you started.

Provide SharePoint Online Site credential when it is asked for.

SharePoint
Operation: How To Get Lists Collection
 
SharePoint

In Step 1, we will initialize the Web Object using SharePoint Client Context.

In Step 2, we initialize the SharePoint Lists Collection.

In Step 3, we call the “Load” function to retrieve SharePoint List Collection properties from the Server.

In Step 4, we call “ExecuteQuery” method to send the request to SharePoint Server.

In Step 5, we iterate the lists collection returned back from the server and display required list properties as output.

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

And here is the output for this operation.

SharePoint
Operation: How To Add New List
 
SharePoint

In Step 1, we will initialize “ListCreationInformation” object which allows specifying the metadata details for this new list.

In Step 2, we will specify a Title for the new list.

In Step 3, we will specify Description for the new list.

In Step 4, we will specify List Template for the new list. In this example, we will use Custom List Template.

In Step 5, we will call the “Add” method of “Lists” collection for SharePoint Web.

In Step 6, we will call the “Load” method, to retrieve the properties of the list object from Server.

In Step 7, we call “ExecuteQuery” method to send the request to SharePoint Server.

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

On successful execution of Step 1-7, we will get a list added to SharePoint and Internal ID of the list as Output.

And here is the output for this operation.

SharePoint

We can also look at the SharePoint Site to ensure that a new list has been added.

SharePoint
 
SharePoint
 
Operation: How To Update Existing List
 
SharePoint

In Step 1, we will initialize “List” object which allows modifying the metadata details for this existing list.

In Step 2, we will update the description of the existing list.

In Step 3, we will call “Update” function of list object and then call “Load” method, to retrieve the properties of list object from Server.

In Step 4, we call “ExecuteQuery” method to send the request to the SharePoint Server.

In Step 5, we will retrieve the list description to ensure that it is updated successfully.

In Step 6, we call a function that we have explained in Step 1-6.

And here is the output for this operation.

SharePoint

We can also look at the SharePoint Site to ensure that the list description has been updated.

SharePoint
 
Operation: How To Delete Existing List
 
SharePoint

In Step 1, we will initialize “List” object by calling a method “GetByTitle” on the Web Object.

In Step 2, we will call “DeleteObject” method on List Object that will delete the reference of the respective list from lists collection.

In Step 3, we call “ExecuteQuery” method to send the request to the SharePoint Server.

In Step 4, we will display the success message to inform the successful deletion of the list.

In Step 5, we call a function that we have explained in Step 1-4

And here is the output for this operation.

SharePoint

We can also look at the  SharePoint Site to ensure that the list has been deleted successfully from SharePoint.

SharePoint
 
Code Snippet

  1. function GetAllLists() {  
  2.     $web = $clientContext.Web;  
  3.     $listColl = $web.Lists;  
  4.     $clientContext.Load($listColl);  
  5.     $clientContext.ExecuteQuery();  
  6.     foreach($list in $listColl) {  
  7.    #Display the list name and ID  
  8.         write - host - ForegroundColor Green "List Name: "  
  9.         $list.Title " ID: "  
  10.         $list.Id  
  11.     }  
  12. }  
  13.   
  14. function CreateList() {  
  15.     $creationInfo = New - Object Microsoft.SharePoint.Client.ListCreationInformation;  
  16.     $creationInfo.Title = "Products";  
  17.     $creationInfo.Description = "Products list created using Powershell";  
  18.     $creationInfo.TemplateType = [int][Microsoft.SharePoint.Client.ListTemplateType]::GenericList  
  19.     $newList = $clientContext.Web.Lists.Add($creationInfo);  
  20.     $clientContext.Load($newList);  
  21.     $clientContext.ExecuteQuery();  
  22.     write - host - ForegroundColor Green "List Name: "  
  23.     $newList.Title " ID: "  
  24.     $newList.Id  
  25. }  
  26.   
  27. function DeleteList() {  
  28.     $list = $clientContext.Web.Lists.GetByTitle("Products");  
  29.     $list.DeleteObject();  
  30.     $clientContext.ExecuteQuery();  
  31.     write - host - ForegroundColor Green "List Deleted successfully"  
  32. }  
  33.   
  34. function UpdateList() {  
  35.     $list = $clientContext.Web.Lists.GetByTitle("Products");  
  36.     $list.Description = "Products description updated using Powershell";  
  37.     $list.Update();  
  38.     $clientContext.Load($list);  
  39.     $clientContext.ExecuteQuery();  
  40.     Write - Host - ForegroundColor Green "List Name: "  
  41.     $list.Title " Description: "  
  42.     $list.Description  
  43. }  
That is all for this demo.

Stay tuned for the upcoming articles in this series.

I hope you find it helpful.