1e41f4b71Sopenharmony_ci# Querying Assets (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Available APIs 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciYou can use [OH_Asset_Query](../../reference/apis-asset-store-kit/_asset_api.md#oh_asset_query) to query asset 6e41f4b71Sopenharmony_ciinformation. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciThe following table describes the attributes for querying an asset. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci>**NOTE** 11e41f4b71Sopenharmony_ci> 12e41f4b71Sopenharmony_ci>In the following table, the attributes starting with **ASSET_TAG_DATA_LABEL** are custom asset attributes reserved. These attributes are not encrypted. Therefore, do not put personal data in these attributes. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci| Attribute Name (Asset_Tag) | Attribute Content (Asset_Value) | Mandatory| Description | 15e41f4b71Sopenharmony_ci| ------------------------------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ | 16e41f4b71Sopenharmony_ci| ASSET_TAG_ALIAS | Type: uint8[]<br>Length: 1-256 bytes | No | Asset alias, which uniquely identifies an asset. | 17e41f4b71Sopenharmony_ci| ASSET_TAG_ACCESSIBILITY | Type: uint32_t<br>Value range: see [Asset_Accessibility](../../reference/apis-asset-store-kit/_asset_type.md#asset_accessibility)| No | Access control based on the lock screen status. | 18e41f4b71Sopenharmony_ci| ASSET_TAG_REQUIRE_PASSWORD_SET | Type: bool | No | Whether the asset is accessible only when a lock screen password is set. | 19e41f4b71Sopenharmony_ci| ASSET_TAG_AUTH_TYPE | Type: uint32_t<br>Value range: see [Asset_AuthType](../../reference/apis-asset-store-kit/_asset_type.md#asset_authtype)| No | Type of user authentication required for accessing the asset. | 20e41f4b71Sopenharmony_ci| ASSET_TAG_SYNC_TYPE | Type: uint32_t<br>Value range: see [Asset_SyncType](../../reference/apis-asset-store-kit/_asset_type.md#asset_synctype)| No | Type of sync supported by the asset. | 21e41f4b71Sopenharmony_ci| ASSET_TAG_IS_PERSISTENT | Type: bool | No | Whether to retain the asset when the application is uninstalled. | 22e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_CRITICAL_1 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 23e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_CRITICAL_2 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 24e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_CRITICAL_3 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 25e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_CRITICAL_4 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 26e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_NORMAL_1 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 27e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_NORMAL_2 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 28e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_NORMAL_3 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 29e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_NORMAL_4 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 30e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_1<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.| 31e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_2<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.| 32e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_3<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.| 33e41f4b71Sopenharmony_ci| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_4<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.| 34e41f4b71Sopenharmony_ci| ASSET_TAG_RETURN_TYPE | Type: uint32_t<br>Value range: see [Asset_ReturnType](../../reference/apis-asset-store-kit/_asset_type.md#asset_returntype)| No | Type of the asset query result to return. | 35e41f4b71Sopenharmony_ci| ASSET_TAG_RETURN_LIMIT | Type: uint32_t | No | Maximum number of asset records to return. | 36e41f4b71Sopenharmony_ci| ASSET_TAG_RETURN_OFFSET | Type: uint32_t<br>Value range: 1-65536 | No | Offset of the asset query result.<br>**NOTE**: This parameter specifies the starting asset record to return in batch asset query. | 37e41f4b71Sopenharmony_ci| ASSET_TAG_RETURN_ORDERED_BY | Type: uint32_t<br>Value: ASSET_TAG_DATA_LABEL_xxx | No | How the query results are sorted. Currently, the results can be sorted only by **ASSET_TAG_DATA_LABEL**.<br>**NOTE**: By default, assets are returned in the order in which they are added.| 38e41f4b71Sopenharmony_ci| ASSET_TAG_REQUIRE_ATTR_ENCRYPTED<sup>13+</sup> | Type: bool| No| Whether to query the customized asset attribute information that is encrypted. By default, the unencrypted, customized asset attribute information is queried.| 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci## Constraints 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ciThe assets queried are transmitted to the service through an IPC channel. Due to the limitation of the IPC buffer size, the maximum number of assets to be queried at a time cannot exceed 40. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci## Example 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci### Querying the Plaintext of an Asset 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ciQuery the plaintext of asset **demo_alias**. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci1. Add the dynamic library in the CMake script. 51e41f4b71Sopenharmony_ci ```txt 52e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libasset_ndk.z.so) 53e41f4b71Sopenharmony_ci ``` 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci2. Add an asset. 56e41f4b71Sopenharmony_ci ```c 57e41f4b71Sopenharmony_ci #include <string.h> 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci #include "asset/asset_api.h" 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci void QueryAsset() { 62e41f4b71Sopenharmony_ci static const char *ALIAS = "demo_alias"; 63e41f4b71Sopenharmony_ci Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 64e41f4b71Sopenharmony_ci Asset_Attr attr[] = { 65e41f4b71Sopenharmony_ci { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // Specify the alias of the asset to query. 66e41f4b71Sopenharmony_ci { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }, // Return all asset information, including the attributes and asset plaintext. 67e41f4b71Sopenharmony_ci }; 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci Asset_ResultSet resultSet = {0}; 70e41f4b71Sopenharmony_ci int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); 71e41f4b71Sopenharmony_ci if (ret == ASSET_SUCCESS) { 72e41f4b71Sopenharmony_ci // Parse the resultSet. 73e41f4b71Sopenharmony_ci for (uint32_t i = 0; i < resultSet.count; i++) { 74e41f4b71Sopenharmony_ci // Parse the secret: the data is secret->blob.data, the size is secret->blob.size. 75e41f4b71Sopenharmony_ci Asset_Attr *secret = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_SECRET); 76e41f4b71Sopenharmony_ci } 77e41f4b71Sopenharmony_ci } 78e41f4b71Sopenharmony_ci OH_Asset_FreeResultSet(&resultSet); 79e41f4b71Sopenharmony_ci } 80e41f4b71Sopenharmony_ci ``` 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci### Querying Attributes of an Asset 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ciQuery attributes of asset **demo_alias**. 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci1. Add the dynamic library in the CMake script. 87e41f4b71Sopenharmony_ci ```txt 88e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libasset_ndk.z.so) 89e41f4b71Sopenharmony_ci ``` 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci2. Add an asset. 92e41f4b71Sopenharmony_ci ```c 93e41f4b71Sopenharmony_ci #include <string.h> 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci #include "asset/asset_api.h" 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci void QueryAttributes() { 98e41f4b71Sopenharmony_ci static const char *ALIAS = "demo_alias"; 99e41f4b71Sopenharmony_ci Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 100e41f4b71Sopenharmony_ci Asset_Attr attr[] = { 101e41f4b71Sopenharmony_ci { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // Specify the alias of the asset to query. 102e41f4b71Sopenharmony_ci { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, // Return only the asset attributes, that is, the result does not include the asset plaintext. 103e41f4b71Sopenharmony_ci }; 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci Asset_ResultSet resultSet = {0}; 106e41f4b71Sopenharmony_ci int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); 107e41f4b71Sopenharmony_ci if (ret == ASSET_SUCCESS) { 108e41f4b71Sopenharmony_ci // Parse the result. 109e41f4b71Sopenharmony_ci for (uint32_t i = 0; i < resultSet.count; i++) { 110e41f4b71Sopenharmony_ci // Parse the data label: the data is label->blob.data, the size is label->blob.size. 111e41f4b71Sopenharmony_ci Asset_Attr *label = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_DATA_LABEL_NORMAL_1); 112e41f4b71Sopenharmony_ci } 113e41f4b71Sopenharmony_ci } 114e41f4b71Sopenharmony_ci OH_Asset_FreeResultSet(&resultSet); 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci ``` 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci### Querying Attributes of Assets 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ciQuery attributes of assets with the tag of **demo_label** and return a total of 10 records sorted by **DATA_LABEL_NORMAL_1** starting from the fifth record that matches the search criteria. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci1. Add the dynamic library in the CMake script. 123e41f4b71Sopenharmony_ci ```txt 124e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libasset_ndk.z.so) 125e41f4b71Sopenharmony_ci ``` 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci2. Add an asset. 128e41f4b71Sopenharmony_ci ```c 129e41f4b71Sopenharmony_ci #include <string.h> 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci #include "asset/asset_api.h" 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci void BatchQuery() { 134e41f4b71Sopenharmony_ci static const char *LABEL = "demo_label"; 135e41f4b71Sopenharmony_ci Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci Asset_Attr attr[] = { 138e41f4b71Sopenharmony_ci { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, 139e41f4b71Sopenharmony_ci { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label }, 140e41f4b71Sopenharmony_ci { .tag = ASSET_TAG_RETURN_OFFSET, .value.u32 = 5 }, 141e41f4b71Sopenharmony_ci { .tag = ASSET_TAG_RETURN_LIMIT, .value.u32 = 10 }, 142e41f4b71Sopenharmony_ci { .tag = ASSET_TAG_RETURN_ORDERED_BY, .value.u32 = ASSET_TAG_DATA_LABEL_NORMAL_1 }, 143e41f4b71Sopenharmony_ci }; 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci Asset_ResultSet resultSet = { 0 }; 146e41f4b71Sopenharmony_ci int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); 147e41f4b71Sopenharmony_ci if (ret == ASSET_SUCCESS) { 148e41f4b71Sopenharmony_ci // Parse the result. 149e41f4b71Sopenharmony_ci for (uint32_t i = 0; i < resultSet.count; i++) { 150e41f4b71Sopenharmony_ci // Parse the data alias: the data is alias->blob.data, the size is alias->blob.size. 151e41f4b71Sopenharmony_ci Asset_Attr *alias = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_ALIAS); 152e41f4b71Sopenharmony_ci } 153e41f4b71Sopenharmony_ci } 154e41f4b71Sopenharmony_ci OH_Asset_FreeResultSet(&resultSet); 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci ``` 157