In this post, you will learn how we can retrieve, create, delete or
update lists using CSOM with PowerShell.
This is mainly focused on using PowerShell scripts for SharePoint online sites.
This is mainly focused on using PowerShell scripts for SharePoint online sites.
Get All Lists:
First we will see how we can get the existing lists available on the SharePoint site. The steps followed here are very similar to the steps following CSOM or JSOM programming.
- Initialize
context object with the site URL parameter. Then initialize the SP
Online Credentials with the above parameters and set it to the context.
- Then
access the list using the context, load the objects and execute the query.
- Loop through the result object and get the necessary properties for each list
1. $siteURL = ""
2. $userId = ""
3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString
4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
5. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
6. $ctx.credentials = $creds
7. try{
8. $lists = $ctx.web.Lists
9. $ctx.load($lists)
10. $ctx.executeQuery()
11. foreach($list in $lists){
12. Write-Host $list.Title
13. }
14. }
15. catch{
16. write-host "$($_.Exception.Message)" -foregroundcolor red
17. }
This will get all the list names available for the list on the site.
Next, we will see how we can get one particular list on the site.
Get List:
The operations are similar as the above section. But we will get the
particular list using GetByTitle Method. The following section depicts you the
flow.
1. $siteURL = ""
2. $userId = ""
3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString
4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
5. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
6. $ctx.credentials = $creds
7. try{
8. $list = $ctx.web.Lists.GetByTitle("TestList")
9. $ctx.load($list)
10. $ctx.executeQuery()
11. Write-Host "$list.Title"
12. }
13. catch{
14. write-host "$($_.Exception.Message)" -foregroundcolor red
15. }
Create List:
Here we will see how we can create a list. The following steps depict the flow.
- Initialize
context object with the site URL parameter. Then initialize the SP
Online Credentials with the above parameters and set it to the context.
- Initialize
the ListCreationInformation object.
- Set
the required parameters for new list. The necessary parameters are list
name and template type.
- Add
new list creation object to the list collection. Then Load and execute the
query.
1. $siteURL = ""
2. $userId = ""
3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString
4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
5. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
6. $ctx.credentials = $creds
7. try{
8. $listInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
9. $listInfo.Title = "TestList2"
10. $listInfo.TemplateType= "100"
11. $list = $ctx.web.Lists.Add($listInfo)
12. $ctx.load($list)
13. $ctx.executeQuery()
14. }
15. catch{
16. write-host "$($_.Exception.Message)" -foregroundcolor red
17. }
The list will be added to the site.
Delete List:
Here we will see how we can delete the list. The following steps depict you the flow.
- Initialize
context object with the site URL parameter. Then initialize the SP
Online Credentials with the above parameters and set it to the context.
- Get
the list using GetByTitle method.
- Then, remove the list using the delete object method.
- Using the context, execute the query.
1. $siteURL = ""
2. $userId = ""
3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString
4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
5. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
6. $ctx.credentials = $creds
7. try{
8. $list = $ctx.web.Lists.GetByTitle("TestList2")
9. $list.DeleteObject()
10. $ctx.executeQuery()
11. }
12. catch{
13. write-host "$($_.Exception.Message)" -foregroundcolor red
14. }
The list will be deleted from the list.
Update List:
Here we will see how we can update the list. The following steps depict you the flow.
- Initialize
context object with the site URL parameter. Then initialize the SP
Online Credentials with the above parameters and set it to the context.
- Get
the list using GetByTitle method.
- In
my case, I am trying to enable content type for the list (Update
Operation). You can do your own custom operation with your custom logic
here.
- Then,
update the list using the update method.
- Using
the context, execute the query.
1. $siteURL = ""
2. $userId = ""
3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString
4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
5. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
6. $ctx.credentials = $creds
7. try{
8. $list = $ctx.web.Lists.GetByTitle("TestList")
9. $list.ContentTypesEnabled = $true
10. $list.Update()
11. $ctx.executeQuery()
12. Write-Host "Content Type Enabled for the list"
13. }
14. catch{
15. write-host "$($_.Exception.Message)" -foregroundcolor red
16. }
The list will be updated. I have tried enabling the content type
(one scenario). You can check the content types by going to the list settings.
Likewise you can do other update operations (like add or removal of fields,
etc.).