Pick an Assignee from a Live Directory¶
A triage user decides who should review each work item, and the list of reviewers lives in the company directory, not in KnowledgeLake. This gizmo loads the people of a department from your directory service, lets the user filter and pick one, matches the person to a KnowledgeLake user, and publishes the user token. The workflow then assigns the next task to that person.
At a glance
- Extension point Custom Gizmo calling your directory and
gizmo.API, plus the layout's built-inassign-toinput - You need A directory service that allows
Origin: null; Index fieldsAssignee EmailandAssignee Token; the featureopenapi_api_ft_catalogs_viewand Use Custom Gizmos for triage users - Teaches Combining an outside service with
V1_AppsAPI.GetUsers, a filterable list, and routing the next task to a person
| Key | Kind and meaning |
|---|---|
department |
Input, string. The department whose people to list. |
assignee-name |
Result, string. Display name of the chosen person. |
assignee-email |
Result, string. Email of the chosen person. |
assignee-token |
Result, string. The person's KnowledgeLake user token, or empty if no user has that email. |
How the pick reaches the next task¶
graph TD
A["Directory service"] --> B["Assignee Picker gizmo"]
C["V1_AppsAPI.GetUsers"] --> B
B --> D["Result assignee-token"]
D --> E["Output Mapping to Index, Assignee Token"]
E --> F["Next Start A Task maps it to assign-to"]
F --> G["Review task assigned to that user"]
Your directory service¶
The gizmo calls GET https://directory.example.com/api/people?department={name} and expects:
[
{ "name": "Avery Brooks", "email": "avery.brooks@example.com", "title": "Senior Reviewer" },
{ "name": "Jordan Lee", "email": "jordan.lee@example.com", "title": "Reviewer" }
]
The service must send Access-Control-Allow-Origin: * (the request carries Origin: null). It receives no user identity, so return only names, emails and titles that any task user may see.
Set it up¶
- Go to Configuration › Services › Custom Gizmos › New Custom Gizmo, title "Assignee Picker". Paste the files below. Replace
https://directory.example.cominwhitelist.txtandmain.js. Save. - In the Tasks app, go to Layouts, edit the triage layout › Add Gizmo › Custom Gizmo › "Assignee Picker".
- In Configure Gizmo, set "Department", or tick Use Value from Input (Input Name
department) to pass it from the work item. - In the triage Start A Task step, Output Mappings:
assignee-email› work item bucketIndex, keyAssignee Emailassignee-token› work item bucketIndex, keyAssignee Token- In the review Start A Task step (the next task), Input Mappings: map the layout input
assign-to› Level Batch, BucketIndex, fieldAssignee Token. Every layout has theassign-toinput; you do not add it. - Give triage users
openapi_api_ft_catalogs_viewand Use Custom Gizmos.
The files¶
<script src="{project}/main.js"></script>
<style>
body { font-family: sans-serif; padding: 12px; }
#filter { width: 100%; padding: 6px; margin-bottom: 8px; box-sizing: border-box; }
table { width: 100%; border-collapse: collapse; }
td { padding: 6px; border-bottom: 1px solid #ddd; }
tr.person { cursor: pointer; }
tr.person:hover { background: #f2f6fb; }
tr.selected { background: #dbe9f8; }
#status { margin-top: 8px; color: #555; }
</style>
<input type="text" id="filter" placeholder="Filter by name or title">
<table><tbody id="people"></tbody></table>
<div id="status">Loading people...</div>
// Assignee Picker gizmo.
// Inputs: department (string).
// Results: assignee-name, assignee-email, assignee-token (KnowledgeLake user token or empty).
// API permissions: CatalogsView. Outside origins: https://directory.example.com
var DIRECTORY = "https://directory.example.com/api/people?department=";
var people = [];
var chosenEmail = "";
var usersPromise = null;
gizmo.SetInputSchema(function (schema) {
schema.AddString("department")
.SetDisplayName("Department")
.SetDefaultValue("Accounts Payable")
.ConfigureUI(function (ui) { ui.AsTextbox(); });
});
gizmo.DeclarePermissions(function (builder) {
builder.AddPermission(APIPermissions.CatalogsView);
});
gizmo.OnReceiveConfig(function (config) {
});
gizmo.OnShow(function () {
document.getElementById("filter").addEventListener("input", draw);
var department = gizmo.Config.GetString("department", "");
fetch(DIRECTORY + encodeURIComponent(department))
.then(function (response) {
if (!response.ok) { throw new Error("HTTP " + response.status); }
return response.json();
})
.then(function (list) {
people = Array.isArray(list) ? list : [];
setStatus(people.length + " people in " + department);
draw();
})
.catch(function (err) {
console.error("Directory call failed", err);
setStatus("Could not load the directory.");
});
});
gizmo.CheckIfCanProceed(function () {
try {
if (gizmo.IsViewOnlyMode()) { return true; }
if (!chosenEmail) { setStatus("Pick a person before you continue."); }
return chosenEmail !== "";
} catch (e) {
return false;
}
});
function draw() {
var text = document.getElementById("filter").value.toLowerCase();
var body = document.getElementById("people");
body.textContent = "";
people
.filter(function (p) {
return ((p.name || "") + " " + (p.title || "")).toLowerCase().indexOf(text) !== -1;
})
.forEach(function (p) {
var row = document.createElement("tr");
row.className = "person" + (p.email === chosenEmail ? " selected" : "");
[p.name, p.title, p.email].forEach(function (value) {
var cell = document.createElement("td");
cell.textContent = value || "";
row.appendChild(cell);
});
if (!gizmo.IsViewOnlyMode()) {
row.addEventListener("click", function () { choose(p); });
}
body.appendChild(row);
});
}
function choose(person) {
chosenEmail = person.email || "";
draw();
setStatus("Matching " + person.name + " to a KnowledgeLake user...");
findUserToken(chosenEmail).then(function (token) {
gizmo.Result
.SetString("assignee-name", person.name || "")
.SetString("assignee-email", chosenEmail)
.SetString("assignee-token", token);
return gizmo.PublishResult().then(function () {
setStatus(token
? "Selected " + person.name + "."
: "Selected " + person.name + ". No KnowledgeLake user has this email.");
});
});
}
// Load every user of every catalog once, then match by email.
function loadUsers() {
if (usersPromise) { return usersPromise; }
usersPromise = gizmo.API.V1_AppsAPI.GetCatalogs()
.then(function (catalogs) {
var list = (catalogs && catalogs.Success) ? catalogs.Data : [];
return Promise.all(list.map(function (catalog) {
return gizmo.API.V1_AppsAPI.GetUsers({ CatalogId: catalog.CatalogId })
.then(function (users) { return (users && users.Success) ? users.Data : []; })
.catch(function () { return []; });
}));
})
.then(function (userLists) {
return [].concat.apply([], userLists);
});
return usersPromise;
}
function findUserToken(email) {
var wanted = email.toLowerCase();
return loadUsers()
.then(function (users) {
for (var i = 0; i < users.length; i++) {
if ((users[i].Email || "").toLowerCase() === wanted) {
return users[i].Token;
}
}
return "";
})
.catch(function (err) {
console.error("User lookup failed", err);
usersPromise = null;
return "";
});
}
function setStatus(text) {
document.getElementById("status").textContent = text;
}
How it works¶
- The directory list comes from your service with
fetch;whitelist.txtlets it through the CSP. V1_AppsAPI.GetCatalogsandV1_AppsAPI.GetUsersrun as the signed-in user. Each user has aToken, aDisplayNameand anEmail. The gizmo loads them once and reuses the list for every pick.- The person is matched by email, case-insensitive, and the gizmo publishes their
Token. CheckIfCanProceedstops the triage task until a person is picked.- In the review step, the layout's built-in
assign-toinput takes that token, and the task service assigns the new task to that user.
Test it¶
- Open the directory URL with a department in a browser and check the JSON.
- Create a test task from the triage layout and open it. Filter the list and pick a person.
- Run a work item through both steps and open the review task in the Tasks app.
You should see
"Selected Avery Brooks." (or the person you picked) in the gizmo, the token in the work item's Assignee Token field, and the review task assigned to that person.
What can go wrong¶
Watch out
- "Could not load the directory." Check the console for a CSP error (origin missing from
whitelist.txt) or a CORS error (the service does not allowOrigin: null). - The task page does not load. The user lacks
openapi_api_ft_catalogs_view. Grant it to triage users. - "No KnowledgeLake user has this email." The directory email and the KnowledgeLake user email differ (for example an alias). Match on the address KnowledgeLake stores.
- The review task is created unassigned. The person is not in the task's assignment pool, or has no Run permission on the workflow. The task service then leaves the task unassigned and logs a warning. Add the person to the task's assignment pool, or pick someone who is in it.
- The token is empty. An empty
assign-toleaves the task to the normal assignment rules: an initial assignee if one is set, otherwise the pool.
Related¶
- API Reference:
V1_AppsAPIandCatalogsView - Look Up a Record in Your Own System: the CORS and whitelist details
- Debugging Custom Gizmos