Skip to content

Find Where a Released Document Landed

A county records office releases recorded documents to its repository through a workflow. Its case system needs the repository location of each document, so a clerk can open it from the case. This recipe reads where Upload To Destination put each document, and shows how to push the location out instead of polling.

At a glance

  • Extension point REST API from your own code, or a Call External API step after release
  • You need A token with Manage Work Items and Access Repository; a workflow with an Upload To Destination step; the work item id
  • Teaches The keys the release step writes (General.DocURL, General.KL_IsReleased, General.DocTypeId) and how to map a content type id to its name

How the release step records the location

The Upload To Destination step (Toolbox, External Steps) releases each document. For each document it uploads, it writes two keys to the document's General bucket:

  • General.DocURL: the location that the destination connector reported for the uploaded file. Its format depends on the destination.
  • General.KL_IsReleased: a marker that the document was released, so a rerun of the step does not upload it again.

The step chooses the destination from General.DocTypeId, the internal id of the document's content type. Each content type belongs to one destination. A document that the step did not upload has no DocURL.

Set it up

  1. Give the service account Manage Work Items and Access Repository and create a token. See Authentication.
  2. Put the token in the environment variable KnowledgeLakeApiToken.
  3. For C#, create a console app (dotnet new console) and generate KL.APIClient from your tenant's spec and reference it. See Generate the client.
  4. Get the work item id. If your system started the work item, store the id that NewWorkItem returned. If a workflow calls your endpoint after the release step, the id is in the WorkItemId header.

The script

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using KL.APIClient.Api;
using KL.APIClient.Client;
using KL.APIClient.Model;

var workItemId = Guid.Parse(Environment.GetCommandLineArgs()[1]);   // the work item guid

var config = new Configuration { BasePath = "https://yourcompany.knowledgelake.io/api" };
config.AddApiKey("TempIdentityToken", Environment.GetEnvironmentVariable("KnowledgeLakeApiToken"));
var workItems = new V1KWorkItemAPIApi(config);
var repository = new V1RepositoryAPIApi(config);

var wi = await workItems.V1KWorkItemAPIControllerGetWorkItemAsync(new PaasWorkItemIdDTO { WorkItemId = workItemId });
if (!wi.Success) throw new Exception("GetWorkItem failed: " + wi.MessageKey);

var types = await repository.V1RepositoryAPIControllerGetContentTypesAsync();
if (!types.Success) throw new Exception("GetContentTypes failed: " + types.MessageKey);

Console.WriteLine($"{"DocumentId",-36}  {"ContentType",-30}  {"Released",-8}  Location");
foreach (var doc in wi.Data.Documents)
{
    var general = doc.Metadata != null && doc.Metadata.TryGetValue("General", out var bucket) && bucket != null
        ? bucket
        : new Dictionary<string, string>();
    general.TryGetValue("DocURL", out var docUrl);
    general.TryGetValue("DocTypeId", out var docTypeId);
    var type = types.Data.FirstOrDefault(t => Guid.TryParse(docTypeId, out var typeId) && t.InternalId == typeId);
    bool released = !string.IsNullOrEmpty(docUrl);
    Console.WriteLine($"{doc.Id,-36}  {type?.ContentTypeDisplayName,-30}  {released,-8}  {docUrl}");
}
Get-ReleasedLocations.ps1
$base    = "https://yourcompany.knowledgelake.io/api"
$headers = @{ TempIdentityToken = $env:KnowledgeLakeApiToken }

$wi = Invoke-RestMethod -Method Post -Uri "$base/V1_KWorkItemAPI/GetWorkItem" -Headers $headers `
        -ContentType "application/json" -Body (@{ WorkItemId = "<work item guid>" } | ConvertTo-Json)
if (-not $wi.Success) { throw "GetWorkItem failed: $($wi.MessageKey)" }

$types = Invoke-RestMethod -Method Post -Uri "$base/V1_RepositoryAPI/GetContentTypes" -Headers $headers `
           -ContentType "application/json" -Body "{}"
if (-not $types.Success) { throw "GetContentTypes failed: $($types.MessageKey)" }

foreach ($doc in $wi.Data.Documents) {
    $general = $doc.Metadata.General
    $type    = $types.Data | Where-Object { $_.InternalId -eq $general.DocTypeId } | Select-Object -First 1
    [pscustomobject]@{
        DocumentId  = $doc.Id
        ContentType = $type.ContentTypeDisplayName
        Released    = [bool]$general.DocURL
        Location    = $general.DocURL
    }
}

How it works

  • V1_KWorkItemAPI/GetWorkItem returns every document with its Metadata.General bucket, where the release step wrote DocURL and DocTypeId.
  • V1_RepositoryAPI/GetContentTypes (empty body; in C#, V1RepositoryAPIControllerGetContentTypesAsync() takes no parameter) lists the content types the user can see. The script matches InternalId to DocTypeId and reads ContentTypeDisplayName.
  • Released is true when DocURL has a value.

Push the location out from the workflow

Instead of polling, send the location when the release is done:

  1. Put a Call External API step right after Upload To Destination.
  2. Send the locations in the body with Liquid, for example {% for d in WorkItem.Documents %}{{ d.Metadata.General.DocURL }}{% endfor %}, or let your endpoint call GetWorkItem with the WorkItemId header.

See Call External API.

Test it

  1. Release a test work item with one document.
  2. Run the script with that work item id. For C#, run dotnet run -- <work item id> in the folder of the console app. For PowerShell, put the id in place of <work item guid>.
  3. Open the location in the destination.

You should see

one row with the content type name, Released = True and a Location that opens the released document.

What can go wrong

What you see Cause and fix
DocURL is empty Upload To Destination has not run yet, failed, or skipped the document. Check the work item in Monitor. GetWorkflowHistory shows which steps ran.
GetWorkItem fails for an old work item The work item no longer exists after its workflow retention. Store the location when the release happens (the push pattern above).
DocTypeId matches no content type The token's user cannot see that content type.
The location is not a link DocURL holds what the destination connector returns. Test with your destination before you show it as a link.