8Sep/115
SharePoint + Powershell: Remove Hold, Record Declaration on All Documents in a Library
In order to delete a Records Library or Records Center, all holds and records in a library must be removed and the Holds and Processing timer job must be run. If this criteria is not met, the "Delete this Document Library" will not be an option in the library settings.
Here is how you can do this programmatically. Note that I did not include the deletion of documents or the document library itself.
$siteURL = $args[0]
$libraryName= $args[1]
$site = Get-SPSite $siteURL
$web = $site.RootWeb
$library = $web.Lists[$libraryName]
$records = $library.Items[0]
# First, remove all holds from all items in the Library
$holdsList = $web.Lists["Holds"]
$holds = $holdsList.Items[0]
[Microsoft.Office.RecordsManagement.Holds.Hold]::RemoveHold($records,$holds,"Holds have been removed.")
# Next, undeclare all items as records. Ps. BulkUndeclareItemsAsRecords is useless.
foreach ($record in $records) {
[Microsoft.Office.RecordsManagement.RecordsRepository.Records]::UndeclareItemAsRecord($item)
}
Start-SPTimerJob HoldProcessing
$web.Dispose()
$site.Dispose()
Thanks to anavijai for the sample Hold removing code.



January 20th, 2012 - 15:54
Why is BulkUndeclareItemsAsRecords usless? Removing the holds doesn't do me much good if I can't also undeclare them. Is there another way to undeclare these files?
February 21st, 2012 - 03:37
Rebecca, you're right: an undeclare is required. BulkUndeclareItemsAsRecords requires comma-delimited string of item IDs as input, which is buiky. Instead, I just undeclared the records as items one by one in the next three lines after the 'useless' comment.
foreach ($record in $records) {
[Microsoft.Office.RecordsManagement.RecordsRepository.Records]::UndeclareItemAsRecord($item)
}
May 4th, 2012 - 18:55
This is exactly what I need but I am not understanding how to input the arguments. can you help with this?
$siteURL = $args[0]
$libraryName= $args[1]
$site = Get-SPSite $siteURL
$web = $site.RootWeb
$library = $web.Lists[$libraryName]
$records = $library.Items[0]
May 4th, 2012 - 19:43
Hey Karen,
In this case, you'd save the script as a file, say "release-holds.ps1"
Then you'd pass it arguments
.\release-holds.ps1 "http://sharepoint/" "My Library"
Otherwise, you can just remove the args part and do $siteURL = "http://sharepoint/" and so on.
May 7th, 2012 - 15:47
Hi Chrissy,
When I run this, I come across the following error. I believe the issue is on this line: $records = $library.Items[0]
Any clues on how to get this working/can you provide an alternate script without arugements?
Cannot index into a null array.
At C:powershellUndeclare Records.ps1:7 char:27
+ $records = $library.Items[ <<<< 0]
+ CategoryInfo : InvalidOperation: (0:In
+ FullyQualifiedErrorId : NullArray
Exception calling "UndeclareItemAsRecord" with "1" ar
e null.
Parameter name: item"
At C:powershellUndeclare Records.ps1:10 char:86
+ [Microsoft.Office.RecordsManagement.RecordsReposito
AsRecord <<<< ($record)
+ CategoryInfo : NotSpecified: (:) [], M
+ FullyQualifiedErrorId : DotNetMethodException