# Managing Assets in a Group (ArkTS) Before managing assets in a group, ensure that you are familiar with the following operations: - [Adding an Asset (ArkTS)](asset-js-add.md) - [Removing Assets (ArkTS)](asset-js-remove.md) - [Updating an Asset (ArkTS)](asset-js-update.md) - [Querying Assets (ArkTS)](asset-js-query.md) ## Prerequisites Set the group ID **demo_group_id** in the **app.json5** file. ```json { "app": { // Other configuration items are omitted here. "assetAccessGroups": [ "demo_group_id" ] } } ``` ## Adding an Asset to a Group Add an asset to the group, with the password **demo_pwd**, alias **demo_alias**, and additional attribute **demo_label**. The asset can be accessed after the user unlocks the device for the first time. ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); return textEncoder.encodeInto(str); } let attr: asset.AssetMap = new Map(); attr.set(asset.Tag.SECRET, stringToArray('demo_pwd')); attr.set(asset.Tag.ALIAS, stringToArray('demo_alias')); attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED); attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); attr.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); try { asset.add(attr).then(() => { console.info(`Asset added to the group successfully.`); }).catch((err: BusinessError) => { console.error(`Failed to add Asset to the group. Code is ${err.code}, message is ${err.message}`); }) } catch (error) { let err = error as BusinessError; console.error(`Failed to add Asset to the group. Code is ${err.code}, message is ${err.message}`); } ``` ## Removing an Asset from a Group Remove asset **demo_alias** from group **demo_group_id**. ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); return textEncoder.encodeInto(str); } let query: asset.AssetMap = new Map(); query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the asset alias to remove a single asset. To remove all assets, leave the alias unspecified. query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); try { asset.remove(query).then(() => { console.info(`Asset removed from the group successfully.`); }).catch((err: BusinessError) => { console.error(`Failed to remove Asset from the group. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { let err = error as BusinessError; console.error(`Failed to remove Asset from the group. Code is ${err.code}, message is ${err.message}`); } ``` ## Updating an asset in a Group Update asset **demo_alias** in group **demo_group_id** as follows: change the asset plaintext to **demo_pwd_new** and the additional attribute to **demo_label_new**. ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); return textEncoder.encodeInto(str); } let query: asset.AssetMap = new Map(); query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); let attrsToUpdate: asset.AssetMap = new Map(); attrsToUpdate.set(asset.Tag.SECRET, stringToArray('demo_pwd_new')); attrsToUpdate.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label_new')); try { asset.update(query, attrsToUpdate).then(() => { console.info(`Asset in the group updated successfully.`); }).catch((err: BusinessError) => { console.error(`Failed to update Asset in the group. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { let err = error as BusinessError; console.error(`Failed to update Asset in the group. Code is ${err.code}, message is ${err.message}`); } ``` ## Querying the Plaintext of an Asset in a Group Query the plaintext of asset **demo_alias** in group **demo_group_id**. ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); return textEncoder.encodeInto(str); } function arrayToString(arr: Uint8Array): string { let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true }); let str = textDecoder.decodeToString(arr, { stream: false }) return str; } let query: asset.AssetMap = new Map(); query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the alias of the asset to query. query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // Return all asset information, including attributes and asset plaintext. query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); try { asset.query(query).then((res: Array) => { for (let i = 0; i < res.length; i++) { // parse the secret. let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array; // parse uint8array to string let secretStr: string = arrayToString(secret); } }).catch ((err: BusinessError) => { console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { let err = error as BusinessError; console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); } ``` ## Querying the Attributes of an Asset in a Group Query the attributes of asset **demo_alias** in group **demo_group_id**. ```typescript import { asset } from '@kit.AssetStoreKit'; import { util } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; function stringToArray(str: string): Uint8Array { let textEncoder = new util.TextEncoder(); return textEncoder.encodeInto(str); } let query: asset.AssetMap = new Map(); query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the alias of the asset to query. query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); // Return only the attributes of the asset, that is, the result does not include the asset plaintext. query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); try { asset.query(query).then((res: Array) => { for (let i = 0; i < res.length; i++) { // Parse the attribute. let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number; } }).catch ((err: BusinessError) => { console.error(`Failed to query Asset from the group. Code is ${err.code}, message is ${err.message}`); }); } catch (error) { let err = error as BusinessError; console.error(`Failed to query Asset from the group. Code is ${err.code}, message is ${err.message}`); } ```