Working with SharePoint Online & PowerShell

Scenario:

  1. Connect to SharePoint Online via PowerShell running on my local computer
  2. Authenticate myself
  3. Open the context of a specific SharePoint Site
  4. Retrieve a specific library
  5. Find a specific folder in this library
  6. Delete/recycle the items in this folder

Result:

# replace these details (also consider using Get-Credential to enter password securely as script runs).. 
$username = "YOURUSERNAME" 
$password = "YOURPASS" 
$url = "https://YOURTENANT.sharepoint.com"

$folderName = "somefolder"
$libraryName = "Documents"

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force 

# the path here may need to change if you used e.g. C:\Lib.. 
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 

# connect/authenticate to SharePoint Online and get ClientContext object.. 
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
$clientContext.Credentials = $credentials 
if (!$clientContext.ServerObjectIsNull.Value) 
{ 
	$checkUrl = $clientContext.Url
    Write-Host "Connected to SharePoint Online site: '$checkUrl'" -ForegroundColor Green 
} 

# check if library exists
$web = $clientContext.Web
$list = $clientContext.Web.Lists.GetByTitle($libraryName); 
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = '1'+$folderName+'';  
$collListItem = $list.GetItems($query)
$clientContext.Load($collListItem)
$clientContext.ExecuteQuery()

if($collListItem.Count -eq 1){
    Write-Host "Folder exists: '$folderName'" -ForegroundColor Green 
}else{
    Write-Host "Folder '$folderName' does not exist" -ForegroundColor Red
	Exit
}

# check if library exists
$web = $clientContext.Web
$list = $clientContext.Web.Lists.GetByTitle($libraryName); 
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = ''; 
$query.FolderServerRelativeUrl = "/documents/somefolder"
$collListItem = $list.GetItems($query)
$clientContext.Load($collListItem)
$clientContext.ExecuteQuery()

$itemsToDelete = @();

if($collListItem.Count -eq 0){
	Write-Host "No files found in this folder" -ForegroundColor Red
	Exit
}

Write-Host
Write-Host "I'm deleting the files now..." -ForegroundColor Green
Write-Host

#recycle items via id
foreach ($id in $itemsToDelete){
 	$itemToDelete = $list.getItemById($id);
    $recycled = $itemToDelete.deleteObject(); #Recycle()
	$clientContext.Load($collListItem)
	$clientContext.ExecuteQuery()
	Write-Host "	$itemToDelete['FileLeafRef'] is deleted" -ForegroundColor Yellow
}
VN:F [1.9.22_1171]
Rating: 9.7/10 (3 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
Working with SharePoint Online & PowerShell, 9.7 out of 10 based on 3 ratings