Created
April 7, 2017 15:52
-
-
Save star-crossed/da16f8ff7d1eff51f8e725431915020f to your computer and use it in GitHub Desktop.
PowerShell script for SharePoint Online to reset all subwebs, lists, and list items in a web.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true, HelpMessage="This is the URL to the SharePoint Online site where you want to reset inheritance.")] | |
[string]$Url, | |
[Parameter(Mandatory=$false, HelpMessage="If true, all subwebs will be reset as well.")] | |
[Boolean]$Subwebs, | |
[Parameter(Mandatory=$false, HelpMessage="This is the path to the DLLs for CSOM.")] | |
[string]$CSOMPath | |
) | |
Set-Strictmode -Version 1 | |
If ($Subwebs -eq $null) { $Subwebs = $false } | |
If ($CSOMPath -eq $null -or $CSOMPath -eq "") { $CSOMPath = "." } | |
Add-Type -Path "$CSOMPath\Microsoft.SharePoint.Client.dll" | |
Add-Type -Path "$CSOMPath\Microsoft.SharePoint.Client.Runtime.dll" | |
function ResetItem ([Microsoft.SharePoint.Client.ListItem]$currentItem) { | |
$currentItem.ResetRoleInheritance() | |
$currentItem.Update() | |
} | |
function ResetList ([Microsoft.SharePoint.Client.List]$currentList) { | |
$clientContext.Load($currentList) | |
$clientContext.ExecuteQuery() | |
Write-Host $currentWeb.Title: $currentList.Title | |
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery() | |
$items = $currentList.GetItems($query) | |
$clientContext.Load($items) | |
$clientContext.ExecuteQuery() | |
$items | ForEach-Object { | |
ResetItem($_) | |
} | |
$currentList.ResetRoleInheritance() | |
$currentList.Update() | |
$clientContext.ExecuteQuery() | |
} | |
function ResetWeb ([Microsoft.SharePoint.Client.Web]$currentWeb) { | |
$context.Load($currentWeb) | |
$context.Load($currentWeb.Webs) | |
$context.Load($currentWeb.Lists) | |
$context.ExecuteQuery() | |
Write-Host $currentWeb.Title: $currentWeb.Url | |
$currentWeb.Lists | ForEach-Object { | |
ResetList($_) | |
} | |
If ($Subwebs) { | |
$currentWeb.Webs | ForEach-Object { | |
ResetWeb($_) | |
} | |
} | |
$currentWeb.ResetRoleInheritance() | |
$currentWeb.Update() | |
$context.ExecuteQuery() | |
} | |
$psCredentials = Get-Credential | |
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($psCredentials.UserName, $psCredentials.Password) | |
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($Url) | |
$clientContext.Credentials = $spoCredentials | |
If ($clientContext.ServerObjectIsNull.Value) { | |
Write-Error "Could not connect to SharePoint Online site collection: $Url" | |
} Else { | |
Write-Host "Connected to SharePoint Online site collection: " $Url -ForegroundColor Green | |
$web = $clientContext.Web | |
ResetWeb($web) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment