Uncommitted changes on SPWeb object
-
10-12-2019 - |
Question
In my simple Powershell script, I keep getting an error "Exception calling "ResetRoleInheritance" : There are uncommitted changes on the SPWEb Object, call SPWeb.Update() to commit the cahnges before calling this method."
I am attempting to reset all subsites with inherited permissions in preparation of cleaning up the permissions for our TeamSites. Where should I be placing SPWeb.Update() in the following code?
$spWeb = Get-SPWeb "http://<sitecollection>/<subsite>/"
Echo "Resetting Permission Inheritance for Site: $spWeb"
$spWeb.ResetRoleInheritance()
$spWeb.Update()
foreach($subSite in $spWeb.Webs)
{
Echo "Resetting Permission Inheritance for SubSite: $subSite"
$subSite.ResetRoleInheritance()
$subSite.Update()
$subSite.Dispose()
}
$spWeb.Dispose()
Solution
This works... for some reason. I have no idea why, really. It most definately could be cleaned up i'm sure as well:
$spWeb = Get-SPWeb "http://<site collection>/<subsite>/"
Echo "Resetting Permission Inheritance for Site: $spWeb"
foreach($subSite in $spWeb.Webs)
{
$subSite.Update()
Echo "Resetting Permission Inheritance for SubSite: $subSite"
$subSite.ResetRoleInheritance()
$subSite.Update()
$subSite.Dispose()
}
$spWeb.ResetRoleInheritance()
$spWeb.Update()
$spWeb.Dispose()
OTHER TIPS
I was tired of dealing with this issue so I got around it by fetching each SPWeb individually, using a list of urls as a string
array. Something like this:
$urls = (Get-SPWeb "http://<site collection>/<subsite>/").webs | select -exp url
(copy and paste urls to/from text file to new powershell session)
# this is now a brand new SPWeb object each time. to be absolutely sure im not causing the afore-mentioned errors
$urls | foreach { $web = get-spweb $_; $web.resetroleinheritance(); $web.dispose() }
Probably not a great solution for automated scripts but it got me over the line.