Declare and Undeclare Records in SPO RRS feed

  • Question

  • Premise: I need a script which will go through an SPO site collection and undeclare all the items that have been declared records.

    To develop the script I have set up the following

    • I created a library in a test site and enabled it for in-place records.
    • I’ve put two docs in the library (file1.txt and file2.txt)

    I've got the following script:

    $url = ''
    $user = ''
    $pwd  = ConvertTo-SecureString 'secret' -AsPlainText -Force
    $crd = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user,$pwd)
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
    $ctx.Credentials = $crd
    $web = $ctx.Web
    Write-Verbose $web.Title
    $list = $ctx.Web.GetList('')
    Write-Verbose $list.Title
    $caml = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $items = $list.GetItems($caml)
    foreach($item in $items)
        Write-Verbose '---------' -Verbose
        Write-Verbose 'Next Item' -Verbose
        $itemId = $item.Id
        Write-Verbose $item.File.Name -Verbose
        $isRecord = [Microsoft.SharePoint.Client.RecordsRepository.Records]::IsRecord($ctx, $item)
        Write-Verbose "Before: $($isRecord.Value)" -Verbose
            Write-Verbose 'Undeclare' -Verbose
            [Microsoft.SharePoint.Client.RecordsRepository.Records]::UndeclareItemAsRecord($ctx, $item)
            Write-Verbose 'Declare' -Verbose
        Write-Verbose 'Updated Record Status' -Verbose
        $updatedItem = $list.GetItemById($itemId)
        $isRecord = [Microsoft.SharePoint.Client.RecordsRepository.Records]::IsRecord($ctx, $updatedItem)
        Write-Verbose "After: $($isRecord.Value)" -Verbose

    When I run this the first time, the two files are declared records (yay!).

    When I run it a second time the script errors by trying to declare the item a record again. ie the value of 'isRecord.Value' is False even though I know the items are records. 

    I can move the UndeclareItemAsRecord outside the IF block & comment out the IF, and it works. 

    Help? What is wrong with my IsRecord Call? 

    Why would it always be false? 

    Tuesday, June 12, 2018 10:59 PM