In this video I demonstrate how we can use the CM API (using PowerShell for the example) to apply holds to records based on search criteria that can be defined in the hold properties. This is just a dabble into what could be used to build something that gets scheduled to automatically apply holds on scheduled interval using something like Windows Scheduler. The PowerShell example is included below.
#Example PowerShell script to apply holds to records using the search string
#set on the hold properties. The sample script filters which holds to process
#based on a UDF of "AutoHold" that is set to true on the hold definition
#Date: 03/18/2022
#Author: Patrick Johnson
#Load CM SDK and Connect to DB
Add-Type -Path "C:\Program Files\Micro Focus\Content Manager\HP.HPTRIM.SDK.dll"
$database = new-object HP.HPTRIM.SDK.Database;
$database.Id = "XX"
$database.WorkgroupServerName = "localhost"
$database.Connect()
$dbName = $database.Name
Write-Host -ForegroundColor Cyan " Connected to:" $dbName
#Search for Holds
$holdSearch = New-Object HP.HPTRIM.SDK.TrimMainObjectSearch($database, [HP.HPTRIM.SDK.BaseObjectTypes]::Hold)
$holdSearch.SetsearchString("AutoHold") #Filters search string to Holds with Additional Field of "AutoHold" set to true
foreach ($hold in $holdSearch)
{
Write-Host -ForegroundColor Cyan "------------------"
Write-Host -ForegroundColor Cyan " Processing hold:" $hold.Name
Write-Host -ForegroundColor Cyan " Hold uri:" $hold.Uri
$holdRecSearchString = $hold.SearchCriteria.ToString()
Write-Host -ForegroundColor Cyan " Search string:" $holdRecSearchString
$itemCount = 0
$itemCountOnHold = 0
$itemCountNoHold = 0
#Search for records
$recordSearch = New-Object HP.HPTRIM.SDK.TrimMainObjectSearch($database, [HP.HPTRIM.SDK.BaseObjectTypes]::Record)
$recordSearch.SetsearchString($holdRecSearchString)
foreach ($record in $recordSearch)
{
#Write-Host -ForegroundColor Red "Processing Record:" $record.Number
$itemCount = $itemCount + 1
$recHolds = $record.AllHolds
#Write-Host -ForegroundColor Cyan " Holds on Record:" $recHolds
if ($recHolds.Contains($hold.Name))
{
#Write-Host -ForegroundColor Cyan " Record Has Hold:" $hold.Name
$itemCountOnHold = $itemCountOnHold +1
}
else
{
#Write-Host -ForegroundColor Cyan "Doesn't have Hold:" $hold.Name
$itemCountNoHold = $itemCountNoHold +1
}
}
Write-Host -ForegroundColor Red "*Metrics pre hold"
Write-Host -ForegroundColor Red " Total records:" $itemCount
Write-Host -ForegroundColor Red " Already on hold:" $itemCountOnHold
Write-Host -ForegroundColor Red " Not on hold:" $itemCountNoHold
#Applies hold to records of search string defined on hold
$hold.AddRecords($holdRecSearchString)
#Validating Hold Metrics after applying hold to items of search
$itemCount = 0
$itemCountOnHold = 0
$itemCountNoHold = 0
foreach ($record in $recordSearch)
{
#Write-Host -ForegroundColor Red "Processing Record:" $record.Number
$itemCount = $itemCount + 1
$recHolds = $record.AllHolds
#Write-Host -ForegroundColor Cyan " Holds on Record:" $recHolds
if ($recHolds.Contains($hold.Name))
{
#Write-Host -ForegroundColor Cyan " Record Has Hold:" $hold.Name
$itemCountOnHold = $itemCountOnHold +1
}
else
{
#Write-Host -ForegroundColor Cyan "Doesn't have Hold:" $hold.Name
$itemCountNoHold = $itemCountNoHold +1
}
}
Write-Host -ForegroundColor Green "*Metrics post hold"
Write-Host -ForegroundColor Green " Total records:" $itemCount
Write-Host -ForegroundColor Green " Already on hold:" $itemCountOnHold
Write-Host -ForegroundColor Green " Not on hold:" $itemCountNoHold
Write-Host -ForegroundColor Cyan "------------------"
}
$database.Dispose()
Write-Host -ForegroundColor Cyan "Disconnected from:" $dbName