The chat responses are generated using Generative AI technology for intuitive search and may not be entirely accurate. They are not intended as professional advice. For full details, including our use rights, privacy practices and potential export control restrictions, please refer to our Generative AI Service Privacy Information. As this is a test version, please let us know if something irritating comes up. Like you get recommended a chocolate fudge ice cream instead of an energy managing application. If that occurs, please use the feedback button in our contact form!
Skip to content

Application context

2612

When Polarion embeds a view, the view starts with no knowledge of its surroundings. The context is the answer to "where am I?": which application and view this is, which project, which work item or document, what is selected, and how to call back into Polarion.

Obtaining the context

import { fetchContext } from 'polarion-app-sdk';

const context = await fetchContext();

fetchContext() sends a request to the Polarion host page and resolves with a PolarionContext object. It rejects with an error if no answer arrives within five seconds, which is what happens when the page is not embedded by Polarion.

Call it once when the view starts. It is safe to call it again later; the answer reflects the state at that moment.

The object

interface PolarionContext {
  applicationId: string;        // "com.example.inspector"
  viewId: string;               // "card"

  projectId?: string;           // "test"
  spaceId?: string;             // "_default"      (Document views)
  documentId?: string;          // "Requirements"  (Document views)
  workItemId?: string;          // "TEST-514"      (form extensions)
  selectedWorkItemIds: string[];// ["test/TEST-514"] in a form (all ticked items in a multi-item edit form); empty in sidebars

  appEndpoint: string;          // "https://host/polarion/apps/com.example.inspector/rest"
  restBaseUrl: string;          // "https://host/polarion/rest/v1"
  restToken?: string;           // token for the X-Polarion-REST-Token header
}

A real example, captured from a form extension (names shortened):

{
  "applicationId": "com.example.context",
  "viewId": "form",
  "projectId": "test",
  "workItemId": "TEST-514",
  "selectedWorkItemIds": ["test/TEST-514"],
  "appEndpoint": "https://polarion.example.com/polarion/apps/com.example.context/rest",
  "restBaseUrl": "https://polarion.example.com/polarion/rest/v1",
  "restToken": "UE9M…"
}

The field-by-field description is in the PolarionContext reference.

What is set where

FieldForm extensionSidebar (table)Sidebar (document)Navigation topic
projectId✔✔✔✔
workItemId✔–––
spaceId, documentIdonly inside a Document–✔–
selectedWorkItemIdsthe open item; all ticked items in the multi-item edit formemptyemptyempty
restBaseUrl, appEndpoint✔✔✔✔
restToken✔ when token auth is enabled✔✔✔

In the multi-item edit form below the Work Items table, opened by ticking several rows, a form extension gets all ticked items in selectedWorkItemIds (for example ["drivepilot/DP-477", "drivepilot/DP-461", "drivepilot/DP-393"]), while workItemId is the current item (DP-477). Observed on Polarion 2612.

The selection in a sidebar does not come from the context: on Polarion 2612 selectedWorkItemIds is empty in a sidebar even when rows are selected, also when the context is read again. The selection arrives through selection-changed events; see React to selection changes. The Application context example shows the empty selectedWorkItemIds in a sidebar; the Selection sidebar example follows the selection with events.

Write code that tolerates missing optional fields. A form extension that is also declared as a navigation topic, for instance, must handle workItemId being undefined.

Work item identifiers

workItemId is a project-local identifier such as TEST-514. The entries of selectedWorkItemIds and of the selection-changed event, in contrast, carry the project: test/TEST-514. Both forms were observed on Polarion 2612, in a form extension, the Work Items table and a Document. Code that accepts both is safest: add the project when an id has no /, strip it where the REST API expects the local id. The REST API addresses work items as projectId/workItemId in resource ids and as separate path segments in URLs; server script examples in this guide use the combined form "test/TEST-514" in an objectId parameter. Build those strings from projectId and the local id:

const fullId = `${context.projectId}/${context.workItemId}`;

The context is a snapshot

The context is static. The host answers fetchContext() with the state at the time of the call, and nothing in the object changes while the view is open; reading it again returns the same kind of snapshot, not a live view. For anything that changes while the view is open, use events:

  • The selection in a table changes constantly. Sidebars subscribe to selection-changed and keep their own copy of the selected ids. See React to selection changes.
  • The work item of a form extension does not change while the panel is mounted; opening another work item loads the form again and the view starts over.
  • The REST token is tied to the logged-in session. If a REST call fails with HTTP 401 after a long idle period, call fetchContext() again to obtain a fresh token before retrying.

How the token flows

Polarion creates the REST token when it loads a view: the host bridge mints it for the logged-in user and hands it over in the context. That is why a server without token authentication shows the warning X-Rest-Token generation is not allowed on the server at the moment a view opens, and why restToken is empty there. A fresh token is issued on every view load. Calls to the application's own endpoints require this token in the X-Polarion-REST-Token header. The Polarion session alone does not authenticate those calls, so they cannot be tested by opening the URL in the browser's address bar.

The token is a credential

restToken grants the current user's rights to anyone who holds it. Keep it in memory, send it only to restBaseUrl and appEndpoint on the same origin, and never log it, store it or forward it to third parties. See Security model.