Detect and Repair Content DB orphans / Security Corruption (Missing ScopeID) in a SharePoint 2010 / 2013 Farm using Windows PowerShell (Updated!)
We have come across issues like unable to access / unable to delete / unable to change permissions / unable to access version history for a document or library / list due to Content DB orphans OR security corruption (Missing ScopeID). You may see error similar to below in ULS logs.
<Date Time> w3wp.exe (PID) <TID> SharePoint Foundation General 8kh7 High The URL 'DocumentLibrary/DocumentName.docx' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. <Correlation ID>
<Date Time> w3wp.exe (PID) <TID> SharePoint Foundation General 8nca Verbose Application error when access /_layouts/Versions.aspx, Error=The URL 'DocumentLibrary/DocumentName.docx' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. at Microsoft.SharePoint.Library.SPRequestInternalClass.GetFileVersions(String bstrWebUrl, String bstrFileUrl, Byte level, Object& pvarVersions, UInt32& pdwcVersions) at Microsoft.SharePoint.Library.SPRequest.GetFileVersions(String bstrWebUrl, String bstrFileUrl, Byte level, Object& pvarVersions, UInt32& pdwcVersions) <Correlation ID>
<Date Time> w3wp.exe (PID) <TID> SharePoint Foundation Runtime tkau Unexpected System.Runtime.InteropServices.COMException: The URL 'DocumentLibrary/DocumentName.docx' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. at Microsoft.SharePoint.Library.SPRequestInternalClass.GetFileVersions(String bstrWebUrl, String bstrFileUrl, Byte level, Object& pvarVersions, UInt32& pdwcVersions) at Microsoft.SharePoint.Library.SPRequest.GetFileVersions(String bstrWebUrl, String bstrFileUrl, Byte level, Object& pvarVersions, UInt32& pdwcVersions) <Correlation ID>
Good thing is, Content DB orphans / Security Corruption (Missing ScopeID) could be detected using stsadm -o databaserepair or using a PowerShell script such as below for SharePoint 2010 /2013 Farm.
Note:
- Please take farm / Content DB backup before you attempt to use this script. I or Microsoft are not responsible for any damages due to wrong usage of the script!
- Script runs at Farm level, enumerates thru each Content DB so script could take some time to complete if there are many Content DBs / large farm.
- Script is Read-Only (just detects orphans – if any).
- IMPORTANT: Script can be changed to remove orphans by setting bool DeleteCorruption to $true. I strongly suggest to use this script initially only for detection and enable DeleteCorruption flag only after ensuring working backup of affected Content DBs / entire farm (if required).
- More Info on SPContentDatabase.Repair method https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spcontentdatabase.repair.aspx
- More Info on stsadm -o databaserepair command https://technet.microsoft.com/en-us/library/cc263282.aspx
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{Add-PSSnapin Microsoft.SharePoint.Powershell}
$CDBs = Get-SPContentDatabase
ForEach ($CDB in $CDBs)
{
Write-Host "Detecting Orphans for " $CDB.Name
$CDB.Repair($false)
}
Comments
- Anonymous
January 01, 2003
@HJ: I am not sure where did you see direct modification attempt. In the above post, I have used SharePoint APIs which is perfectly supported and recommended.
You may want to go thru http://support.microsoft.com/kb/841057 to make yourself familiar with supportability guidelines. - Anonymous
September 25, 2014
Correct me if I am wrong, but directly altering the SharePoint content database like this is against Best Practices. Not a lot of business would allow this unless things were really dire. - Anonymous
November 22, 2014
Thanks for the blog it is just what I was looking for - Anonymous
November 01, 2015
Thanks for the post. I have tried this script which displays the COUNT of the orphaned objects and the TYPE. Is there any way to find out exactly WHAT is orphaned? Thank you. - Anonymous
November 02, 2015
@Conax: It displays Orphan object details followed by Count.