> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-cms/extend-your-project/backoffice-extensions/extending-overview/extension-types/workspaces/workspace-context.md).

# Workspace Context

Workspace Contexts manages shared state and enables communication between extensions in a workspace.

Workspace Contexts serve as the central communication hub for workspace extensions, providing shared state management within the boundaries of the Workspace. They enable different Workspace components to interact through a common data layer.

## Purpose

Workspace Contexts provide:

* **Shared state** scoped to a specific workspace instance.
* **Communication layer** between extensions in the workspace.
* **Entity lifecycle management** for workspace data.
* **Context isolation** ensures workspace independence.

## Manifest

{% code title="manifest.ts" %}

```typescript
{
	type: 'workspaceContext',
	name: 'Example Counter Workspace Context',
	alias: 'example.workspaceContext.counter',
	api: () => import('./counter-workspace-context.js'),
	conditions: [
		{
			alias: UMB_WORKSPACE_CONDITION_ALIAS,
			match: 'Umb.Workspace.Document',
		},
	],
}
```

{% endcode %}

## API Implementation

Create a workspace context by extending `UmbContextBase` and providing a unique context token. Add this to your project to enable shared state management between workspace extensions:

{% code title="counter-workspace-context.ts" %}

```typescript
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbNumberState } from '@umbraco-cms/backoffice/observable-api';

export class WorkspaceContextCounter extends UmbContextBase {
	#counter = new UmbNumberState(0);
	readonly counter = this.#counter.asObservable();

	constructor(host: UmbControllerHost) {
		super(host, EXAMPLE_COUNTER_CONTEXT);
	}

	increment() {
		this.#counter.setValue(this.#counter.value + 1);
	}

	reset() {
		this.#counter.setValue(0);
	}
}

export const api = WorkspaceContextCounter;
```

{% endcode %}

## Context Token

A Context Token is used to consume or get a Context. Read more about [Context Consumption here](/umbraco-cms/extend-your-project/backoffice-extensions/foundation/context-api/consume-a-context.md).

{% code title="counter-workspace-context.ts" %}

```typescript
export const EXAMPLE_COUNTER_CONTEXT = new UmbContextToken<WorkspaceContextCounter>(
	'UmbWorkspaceContext', // Ensures workspace scoping
	'example.workspaceContext.counter',   // Must match manifest alias
);
```

{% endcode %}

When declaring a Workspace Context, always use `'UmbWorkspaceContext'` as the first parameter in your Context Token to ensure proper context isolation.

## Extension Communication

### Workspace Action

The following example shows how to call the increment method from a Workspace Action API.

{% code title="incrementor-workspace-action.ts" %}

```typescript
export class MyIncreaseCounterWorkspaceAction extends UmbWorkspaceActionBase {
	override async execute() {
		const context = await this.getContext(EXAMPLE_COUNTER_CONTEXT);
		context?.increment();
	}
}
```

{% endcode %}

### Workspace View

The following example shows how a Workspace View Element can consume a context.

{% code title="counter-workspace-view\.ts" %}

```typescript
export class MyWorkspaceView extends UmbElementMixin(LitElement) {

	@state()
	_count?: number;

	constructor() {
		super();
		this.consumeContext(EXAMPLE_COUNTER_CONTEXT, (context) => {
			this.observe(context?.counter, (count) => this._count = count);
		});
	}
	
	// A render using _count
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.umbraco.com/umbraco-cms/extend-your-project/backoffice-extensions/extending-overview/extension-types/workspaces/workspace-context.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
