Share via


Undeclare SharePoint in-place records using PowerShell

Question

Thursday, January 24, 2013 8:01 PM

Hello all,

I have been trying to find the best solution to undeclare all the records in one of my document libraries.  I have tried tried several scripts but they all seem to have various issues. I finally got the one below to debug without issues. However when I run the script I get no response. I setup PowerShell to log and it has nothing in it too. Any help would be greatly appreciated!!

function UndeclareRecords($siteUrl, $libraryName) {
  function IsRecord($item) {
    [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::IsRecord($item)
  }

  function EnsureCheckedIn($item) {
    if ($item.File.CheckOutType -ne [Microsoft.SharePoint.SPFile+SPCheckOutType]::None `
        -and (-not [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::IsLocked)) {
      write-host "Checking in file: $($item.Url)"
      $file.CheckIn("Checked in by script prior to undeclaring as record", ($
                    [Microsoft.SharePoint.SPCheckinType]::MinorCheckIn)
    }
  }

  function UndeclareRecord($item) {
    write-host "Undeclaring record: $($item.Url)"
    [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::UndeclareItemAsRecord($item) 
  }

  $site = Get-SPWeb $siteUrl
  $list = $site.Lists[$libraryName]
  $list.Items | foreach { 
    if (IsRecord $_) {
      EnsureCheckedIn $_
      UndeclareRecord $_
    }
  }

  $site.Dispose()
}

All replies (5)

Friday, January 25, 2013 10:36 AM âś…Answered | 3 votes

Hi,

Here are the powershell code to un-declare the records inside a library:

$SPAssignment = Start-SPAssignment

$web = Get-SPWeb "siteurlhere" -AssignmentCollection $spAssignment

$list = $web.lists["libraryname"].items

foreach ($item in $list)

{

                $IsRecord = [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::IsRecord($Item)

                if ($IsRecord -eq $true){

                                Write-Host "Undeclared $($item.Title)"

                                [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::UndeclareItemAsRecord($Item)

                }

}

Stop-SPAssignment $SPAssignment

For more information,please refer to this site:

Undeclare / Declare all / some records in a list: http://www.mysharepointadventures.com/2012/06/undeclare-declare-all-some-records-in-a-list/

Thanks,

Entan Ming

Entan Ming
TechNet Community Support


Friday, January 25, 2013 4:56 PM

Hi Entan,

Thank you for your response.  I tried your script and got this result: 

Exception calling "IsRecord" with "1" argument(s): "Value cannot be null.
Parameter name: item"
At C:\Users\Administrator\AppData\Local\Temp\2\07a9b491-c920-4c01-9fa7-a1e8d6381576.ps1:11 char:101

  •                 $IsRecord = [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::IsRecord <<<< ($Item)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

I am using PowerGUI to debug the script and it doesn't like this: IsRecord($Item). I am fairly new to writing PowerShell scripts but from what I can tell that shouldn't be an issue. Any thoughts?

Thanks!


Monday, January 28, 2013 7:23 AM

Hi,

You need to make sure that you have used the right siteURL and list name in the above script. You don't need to change blank space to %20 in this line:$list = $web.lists["libraryname"].items. Let's take 'Document library' as an example, you can change the above line to $list = $web.lists["Document library"].items. Provide the correct site URL here :$web = Get-SPWeb http://servername/sitename.

Thanks,

Entan Ming

Entan Ming
TechNet Community Support


Monday, January 28, 2013 5:52 PM

I got it working an a test library now. Thanks for all your help!


Wednesday, October 9, 2013 1:52 PM

How did you get it to work? I am getting the same error that you got and my names of the site and library are correct.

Thank you