Rename Qlik Sense Sheets using API

45 Views Asked by At

I am trying to rename Qlik Sense Sheets using APIs. I got to know we should rename only using properties. I tried below code in Powershell but its not working. Please help me correct the code. I am getting error while retrieving sheets information

$hdrs = @{}
$hdrs.Add("X-Qlik-Xrfkey","examplexrfkey123")
$hdrs.Add("X-Qlik-User", "UserDirectory=INTERNAL; UserId=sa_api")

$cert = Get-ChildItem -Path "Cert:\CurrentUser\My" | Where {$_.Subject -like '*QlikClient*'}

$body = '{}'

$Data = Get-Content C:\ProgramData\Qlik\Sense\Host.cfg
$FQDN = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($($Data)))

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'

$guid = [guid]::NewGuid()

# Function to update sheet name
function Update-SheetName {
    param (
        [string]$sheetId,
        [string]$newName
    )

    $body = @{
        name = $newName
    } | ConvertTo-Json

    Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/sheet/$sheetId/properties" -Method Put -Headers $hdrs -ContentType 'application/json' -Body $body -Certificate $cert
}

$apps = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/app/full?xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert

Foreach ($app in $apps) {

    if ($app.published -and $app.stream.name -eq 'SCPM' -and $app.name -eq 'MAERSK Operational Test Rename') {

        # Retrieve list of sheets
        $sheets = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/sheet/full?xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert

        # Iterate through each sheet
        foreach ($sheet in $sheets) {
            # Construct new sheet name with owner name and sheet type
            $ownerName = $sheet.owner.name
            $sheetType = if ($sheet.community) { "Community" } else { "MySheet" }
            $newName = "$ownerName - $sheetType - $($sheet.name)"

            # Update sheet name
            Update-SheetName -sheetId $sheet.id -newName $newName
        }
     }
}

ERROR:

Invoke-RestMethod : The remote server returned an error: (404) Not Found.
At C:\Test\RenameSheetName-2.ps1:37 char:13
+ ...   $sheets = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/sheet/f ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
2

There are 2 best solutions below

1
SmoothBrane On

Looks like your Update-SheetName function is using a $headers variable while at the beginning of your script you created a $hdrs. You may need to use the -Certificate $cert part too, as well as the xrfkey=examplexrfkey123 query parameter.

0
Stefan Stoichev On

As far as I know it's not possible to update sheets using the Repository API. Sheets are part of the app and in order to interact (create, read, update and delete) with its components you'll have to open the app. And this is done via the Engine API. And the Engine API is websocket based.