# Workspace Context

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

```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',
		},
	],
}
```

## 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:

```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;
```

## Context Token

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

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

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.

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

### Workspace View

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

```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
}
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.umbraco.com/umbraco-cms/customizing/extending-overview/extension-types/workspaces/workspace-context.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
