netnerds.net

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.

Posted by: Chrissy   Filed under: PowerShell, SharePoint Leave a comment
Comments (5) Trackbacks (0)
  1. 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?

    • 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)
      }

      • 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]

  2. 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


Leave a comment


No trackbacks yet.