目录

Workspace Management

Since API version 6.0.0(20), apps can enable the dual-workspace function and create, query, and delete workspaces.

Since API version 26.0.0, the dual-workspace status of a device can be queried, whether a workspace is an enterprise workspace can be determined, and workspaces can be switched.

Use Cases

Enterprise Space Kit provides apps with the capabilities of enabling the dual-workspace function and managing workspaces. Before creating a personal workspace, an app must enable the workspace function first. In addition, the functions of querying and removing workspaces are provided.

An enterprise workspace is a general office space managed by the enterprise, and a personal workspace is used for external communication, operations, open-source development, and other work-related activities.

API Description

For details about these APIs, please refer to the API Reference.

API Description
enableWorkspace(enable: boolean): Promise<void> Enables or disables the workspace function. After calling this API, an MDM app also needs to call the restrictions.setDisallowedPolicy API of MDM Kit to enable the INACTIVE_USER_FREEZE device feature, so as to enable the inactive user running capability.
createWorkspace(localName: string, workspaceType: WorkspaceType, params?: CreateWorkspaceParams): Promise<WorkspaceInfo> Creates a workspace and returns the workspace information.
queryWorkspace(queryFlag: QueryType): Promise<WorkspaceInfo[]> Queries workspace information and returns the result.
removeWorkspace(workspaceId: number): Promise<void> Removes a workspace.
isEnterpriseWorkspaceEnabled(): Promise<boolean> Queries whether dual workspaces are enabled on a device and returns the result.
isEnterpriseWorkspace(workspaceId?: number): Promise<boolean> Queries whether a workspace is an enterprise workspace and returns the result.
switchWorkspace(workspaceId?: number): Promise<void> Switches the workspace.

Development Procedure

  1. Import the dependent modules of the workspace management API.
import { spaceManager } from '@kit.EnterpriseSpaceKit';
import { ErrCode } from '../../common/ErrCode';
import { hilog } from '@kit.PerformanceAnalysisKit';
  1. Encapsulate the workspace management API.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

export class SpaceLifeCircleManagerApi {
  static async createWorkspace(localName: string, workspaceType: spaceManager.WorkspaceType,
    params: spaceManager.CreateWorkspaceParams): Promise<spaceManager.WorkspaceInfo | undefined> {
    try {
      const workspaceInfo: spaceManager.WorkspaceInfo =
        await spaceManager.createWorkspace(localName, workspaceType, params);
      hilog.info(DOMAIN, TAG,
        `Succeeded in creating workspace, workspaceInfo:` + JSON.stringify(workspaceInfo));
      return workspaceInfo;
    } catch (err) {
      hilog.error(DOMAIN, TAG,
        `Failed to create workspace. Code: ${err.code}, message: ${err.message}`);
      return undefined;
    }
  }

  static async enableWorkspace(enable: boolean): Promise<number> {
    try {
      await spaceManager.enableWorkspace(enable);
      hilog.info(DOMAIN, TAG, 'Succeeded in enabling workspace');
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to enable workspace. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }

  static async queryWorkspace(queryType: spaceManager.QueryType): Promise<spaceManager.WorkspaceInfo[]> {
    try {
      const spaces: spaceManager.WorkspaceInfo[] = await spaceManager.queryWorkspace(queryType);
      hilog.info(DOMAIN, TAG, `Succeeded in querying workspace`);
      return spaces;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to query workspace. Code: ${err.code}, message: ${err.message}`);
      return [];
    }
  }

  static async removeWorkspace(workspaceId: number): Promise<number> {
    try {
      await spaceManager.removeWorkspace(workspaceId);
      hilog.info(DOMAIN, TAG, 'Succeeded in removing workspace');
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to remove workspace. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }
  
  static async isEnterpriseWorkspaceEnabled(): Promise<boolean> {
    try {
      const status = await spaceManager.isEnterpriseWorkspaceEnabled();
      hilog.info(DOMAIN, TAG, `Succeeded in getting enterprise workspace enabled. enabled: ${status}`);
      return status;
    } catch (err) {
      hilog.error(DOMAIN, TAG,
        `Failed to get enterprise workspace enabled. Code: ${err.code}, message: ${err.message}`);
      return false;
    }
  }

  static async isEnterpriseWorkspace(): Promise<boolean> {
    try {
      const isEnterpriseWorkspace = await spaceManager.isEnterpriseWorkspace();
      hilog.info(DOMAIN, TAG, `Succeeded in getting enterprise workspace. isEnterprise: ${isEnterpriseWorkspace}`);
      return isEnterpriseWorkspace;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to get enterprise workspace. Code: ${err.code}, message: ${err.message}`);
      return false;
    }
  }

  static async switchWorkspace(workspaceId: number): Promise<number> {
    try {
      await spaceManager.switchWorkspace(workspaceId);
      hilog.info(DOMAIN, TAG, `Succeeded in switching enterprise workspace. workspaceId: ${workspaceId}`);
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to switch enterprise workspace. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }
}
  1. Import the dependencies related to the implementation of workspace management service.
import { router } from '@kit.ArkUI';
import { spaceManager } from '@kit.EnterpriseSpaceKit';
import { SpaceLifeCircleManagerApi } from '../api/SpaceLifeCircleManagerApi';
import { ErrCode } from '../../common/ErrCode';
import { hilog } from '@kit.PerformanceAnalysisKit';
  1. Implement the workspace management service.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

@Entry
@Component
struct SpaceLifeCircleManagerPage {
  async enableWorkspace() {
    // Preset parameters.
    const enable: boolean = true;

    if (await SpaceLifeCircleManagerApi.enableWorkspace(enable) !== ErrCode.OK) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to enable workspace!');
      return;
    }
    // Process the post-logic.
  }

  async createWorkspace() {
    // Preset parameters.
    const localName: string = 'localName'; // Workspace name, which is passed by the user.
    const workspaceType: spaceManager.WorkspaceType = spaceManager.WorkspaceType.ADMIN;
    const params: spaceManager.CreateWorkspaceParams = {
      shortName: 'test'
    };

    const workspaceInfo: spaceManager.WorkspaceInfo | undefined = await
    SpaceLifeCircleManagerApi.createWorkspace(localName, workspaceType, params);
    if (workspaceInfo === undefined) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to create workspace!');
      return;
    }
    // Process the post-logic.
  }

  async queryWorkspace() {
    const queryType: spaceManager.QueryType = spaceManager.QueryType.ALL;
    let spaceInfo = await SpaceLifeCircleManagerApi.queryWorkspace(queryType)
    // Process the post-logic.
  }

  async removeWorkspace() {
    const workspaceId: number = 101; // ID of the workspace to be deleted, which is passed by the user.
    if (await SpaceLifeCircleManagerApi.removeWorkspace(workspaceId) !== ErrCode.OK) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to remove workspace!');
      return;
    }
    // Process the post-logic.
  }

  async isEnterpriseWorkspaceEnabled() {
    const status = await SpaceLifeCircleManagerApi.isEnterpriseWorkspaceEnabled();
    // Process the post-logic.
  }

  async isEnterpriseWorkspace() {
    const isEnterpriseWorkspace = await SpaceLifeCircleManagerApi.isEnterpriseWorkspace();
    // Process the post-logic.
  }

  async switchWorkspace() {
    const workspaceId: number = 101; // ID of a created workspace.
    if (await SpaceLifeCircleManagerApi.switchWorkspace(workspaceId) !== ErrCode.OK) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to switch workspace!');
      return;
    }
    // Process the post-logic.
  }

  build() {
    Column() {
      Row() {
        Button($r('app.string.enableWorkspace'))
          .buttonCommonStyle()
          .onClick(() => {
            this.enableWorkspace();
          })
      }

      Row() {
        Button($r('app.string.createWorkspace'))
          .buttonCommonStyle()
          .onClick(() => {
            this.createWorkspace();
          })
      }

      Row() {
        Button($r('app.string.queryWorkspace'))
          .buttonCommonStyle()
          .onClick(() => {
            this.queryWorkspace();
          })
      }

      Row() {
        Button($r('app.string.removeWorkspace'))
          .buttonCommonStyle()
          .onClick(() => {
            this.removeWorkspace();
          })
      }

      Row() {
        Button($r('app.string.isEnterpriseWorkspaceEnabled'))
          .buttonCommonStyle()
          .onClick(() => {
            this.isEnterpriseWorkspaceEnabled();
          })
      }

      Row() {
        Button($r('app.string.isEnterpriseWorkspace'))
          .buttonCommonStyle()
          .onClick(() => {
            this.isEnterpriseWorkspace();
          })
      }

      Row() {
        Button($r('app.string.switchWorkspace'))
          .buttonCommonStyle()
          .onClick(() => {
            this.switchWorkspace();
          })
      }

      Row() {
        Button($r('app.string.back'))
          .buttonCommonStyle()
          .onClick(() => {
            router.back();
          })
      }
    }
  }
}

@Extend(Button)
function buttonCommonStyle() {
  .width(200)
  .height(50)
  .backgroundColor('#6366F1')
  .fontColor('#FFFFFF')
  .fontSize(14)
  .margin({ left: 20, bottom: 5 })
}