none
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 = 'https://me.sharepoint.com/sites/ipr'
    $user = 'admin@me.onmicrosoft.com'
    $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
    $ctx.Load($web)
    $ctx.ExecuteQuery()
    Write-Verbose $web.Title
    
    $list = $ctx.Web.GetList('https://me.sharepoint.com/sites/ipr/records/')
    $ctx.Load($list)
    $ctx.ExecuteQuery()
    Write-Verbose $list.Title
    
    $caml = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $items = $list.GetItems($caml)
    $ctx.Load($items)
    $ctx.ExecuteQuery()
    
    foreach($item in $items)
    {
        Write-Verbose '---------' -Verbose
        Write-Verbose 'Next Item' -Verbose
    
        $itemId = $item.Id
        $ctx.Load($item)
        $ctx.Load($item.File)
        $ctx.ExecuteQuery()
        Write-Verbose $item.File.Name -Verbose
    
        $isRecord = [Microsoft.SharePoint.Client.RecordsRepository.Records]::IsRecord($ctx, $item)
        Write-Verbose "Before: $($isRecord.Value)" -Verbose
    
        if($isRecord.Value)
        {
            Write-Verbose 'Undeclare' -Verbose
            [Microsoft.SharePoint.Client.RecordsRepository.Records]::UndeclareItemAsRecord($ctx, $item)
        }
        else
        {
            Write-Verbose 'Declare' -Verbose
            [Microsoft.SharePoint.Client.RecordsRepository.Records]::DeclareItemAsRecord($ctx,$item)
        }
        $ctx.ExecuteQuery()
        Write-Verbose 'Updated Record Status' -Verbose
    
        $updatedItem = $list.GetItemById($itemId)
        $ctx.Load($updatedItem)
        $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