Recently had a conversation regarding assigning a label to CM items that are part of a Fusion workbook. This is an interesting idea since it will let you then go into CM and find all items that were part of that Fusion workbook based on the user label and perform additional tasks using the CM API.
Here's the example Powershell to do so:
##############################################################################################################################
# 04-08-2024 - Example Fusion Custom Activity (PJ)
# Adds a CM label to records in the workbook
# This is an example custom activity and needs to be adjusted and tested per requirements.
#
# 1) Connects to CM
# 2) Creates a new CM label
# 3) Connects to Fusion
# 4) Retrieves items in the workbook with a custom activity name of "labelcm"
# 5) Checks if the item belongs to a repository of type Content Manager
# 6) Assigns the new record label to the item in CM
#
# Replace the following parameters with your specific tenant information:
# -YOUR_TENANT_ID
# -YOUR_USER_ID
# -YOUR_TENANT_URL (i.e. https://www.demo.microfocusfileanalysis.com:9310)
# -YOUR_CUSTOM_ACTIVITY
# Replace the following parameters with your specific CM dataset:
# -YOUR_CM_DBID
# -YOUR_CM_WGS
##############################################################################################################################
############### ADJUST FUSION VARIABLES ##################
$tenantID = "YOUR_TENANT_ID" #Tenant ID
$tenantUser = "YOUR_USER_ID" #Tenant user - User must have API access and permissions for the API name and Workspace
$tenantUrl = "YOUR_TENANT_URL" #Tenant URL - Do not include trailing slash in URL
$customActivity = "YOUR_CUSTOM_ACTIVITY" #Custom Activity API Name
$batchSize = "5000" #Adjust batch size for result paging to meet performance/memory characteristics while retrieving items from workbook
##########################################################
################# ADJUST CM VARIABLES ####################
$cmDbid = "YOUR_CM_DBID" #Content Manager Dataset ID
$cmWgs = "YOUR_CM_WGS" #Content Manager Workgroup Server
##########################################################
#Load CM SDK and Connect to DB
Add-Type -Path "C:\Program Files (x86)\Micro Focus\Content Manager\TRIM.SDK.dll"
try {
$database = new-object TRIM.SDK.Database;
$database.Id = $cmDbid
$database.WorkgroupServerName = $cmWgs
$database.Connect()
}
catch {
Write-Host -f red "Exception Error: "$_.Exception.Message
}
#Create Record Label that duplicate records will be assigned to
try {
$uLabel = New-Object TRIM.SDK.UserLabel($database)
$labelTime = Get-Date -Format "dd/MM/yyyy_HH.mm"
$uLabel.Name = "Duplicate Records _ $labelTime"
$uLabel.Save()
}
catch {
Write-Host -f red "Exception Error: "$_.Exception.Message
}
##########################################################
#Reading password from terminal
$tenantPassword = Read-Host "Enter a Password"
#Authentication details
$auth = @"
{"tenantId":"$tenantID",
"user":"$tenantUser",
"password":"$tenantPassword"
}
"@
$headerA = @{
"Content-Type" = "application/json"
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Get authentication token via post
$loginResponse = Invoke-RestMethod -Uri "$tenantUrl/v1/auth/login" -Method 'Post' -Body $auth -Headers $headerA -UseBasicParsing
$token = $loginResponse.accessToken.ToString()
Write-Output "Access token is $token"
#Execute REST call to retrieve workbooks with pending state for custom activity
$headerB = @{
"Content-Type"="application/json"
"accept"="application/json"
"Authorization"="Bearer $token"
}
$customActivityUrl = $tenantUrl + '/ca/v1/workspace/custom-activity-workbooks/' + $customActivity + '?processingStatus=Pending'
$custActResponse = Invoke-RestMethod -Uri $customActivityUrl -Method 'Get' -Headers $headerB
Write-Output "Found workbooks for external processing" $custActResponse.Count.ToString()
foreach ( $item in $custActResponse) {
$workbookID = $item.id.ToString()
$workspaceID = $item.workspaceId.ToString()
Write-Output "Processing Workbook $workbookID"
#Call to set status of workbook to processing
Write-Output "Call to set status to processing start"
$status = @{
"status" = "Processing"
"successCount" = 0
"errorCount" = 0
} | ConvertTo-Json
Invoke-RestMethod -Uri "$tenantUrl/ca/v1/workspace/$workspaceID/workbook/$workbookID/custom-activity/$customActivity" -Method 'Put' -body $status -Headers $headerB -UseBasicParsing
$wbpageToken = ""
$processedDocs = 0
#Now get the list of files until no more page tokens are left in the pending workbook
Do {
#Call to retrieve batch of files in workbook up to value defined in batchSize
$searchResponse = Invoke-RestMethod -Uri "$tenantUrl/ca/v1/workspace/$workspaceID/workbook/$workbookID/documents?queryFields=repository_id,repo_uri,repository_type&limit=$batchSize&pageToken=$wbpageToken" -Method 'Get' -Headers $headerB -UseBasicParsing
Write-Output "Processing file - page size is " $searchResponse.documentMetadataList.Count
Write-Output "Workspace ID " $workspaceID
Write-Output "Workbook ID " $workbookID
Write-Output "Page Token = " $searchResponse.pageToken
#Set page token for next page of results in the Do/While loop
$wbpageToken = $searchResponse.pageToken
#Iterate through each item in the current result page and add user label
foreach ( $item in $searchResponse.documentMetadataList) {
Write-Host "Processing Document with Fusion ID: " $item.documentId
#Checking if item belongs to repository of type CONTENTMANAGER
if ($item.metadata.repository_type -eq "CONTENTMANAGER")
{
$recUri = $item.metadata.repo_uri
Write-Host "Record uri: " $recUri
#Search for Record
$recordSearch = New-Object TRIM.SDK.TrimMainObjectSearch($database, [TRIM.SDK.BaseObjectTypes]::Record)
$recordSearch.SetsearchString("uri:$recUri")
If ($recordSearch.Count -eq "0")
{
Write-Host -f red "**** Record missing with URI: " $recUri
}
else
{
foreach ($recObj in $recordSearch)
{
Try{
Write-Host "Applying user label - record number: " $recObj.Number
$recObj.ApplyUserLabel($uLabel)
}
catch{
Write-Host -f red "Exception Error: "$_.Exception.Message
}
}
}
}
Else
{
Write-Host "Item is not contained in repository of type: CONTENTMANAGER"
}
Write-Host "-----------"
}
#Update processing progress status of workbook to indicate number of docs currently processed
Write-Output "Call to set status to processing update"
$processedDocs = $processedDocs + $searchResponse.documentMetadataList.Count
$status = @{
"status" = "Processing"
"successCount" = $processedDocs
"errorCount" = 0
} | ConvertTo-Json
#Call to set status to processing
Invoke-RestMethod -Uri "$tenantUrl/ca/v1/workspace/$workspaceID/workbook/$workbookID/custom-activity/$customActivity" -Method 'Put' -body $status -Headers $headerB -UseBasicParsing
#Start-Sleep -Second 1
} while ($searchResponse.pageToken -ne $null)
#Update status of the current workbook to completed
Write-Output "Call to set status to completed"
$status = @{
"status" = "Completed"
"successCount" = $searchResponse.documentMetadataList.Count
"errorCount" = 0
} | ConvertTo-Json
Invoke-RestMethod -Uri "$tenantUrl/ca/v1/workspace/$workspaceID/workbook/$workbookID/custom-activity/$customActivity" -Method 'Put' -body $status -Headers $headerB -UseBasicParsing
}
Write-Output "Disposing of CM database connection"
$database.Dispose()
#Log out of Fusion tenant API
#Write-Output "--Logout"
#$logoutAnswer = Invoke-RestMethod -Uri $uri"v1/auth/logout" -Method 'Post' -Body $auth -Headers $headerA -UseBasicParsing