Skip to content

Instantly share code, notes, and snippets.

@star-crossed
Created April 7, 2017 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save star-crossed/da16f8ff7d1eff51f8e725431915020f to your computer and use it in GitHub Desktop.
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.
[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