目录

@ohos.data.dataShare (DataShare)

The DataShare module allows an app to manage its own data and share data with other apps on the same device.

  • The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs of this module can be used only in the stage model.

Modules to Import

import { dataShare } from '@kit.ArkData';

dataShare.createDataProxyHandle20+

createDataProxyHandle(): Promise<DataProxyHandle>

Creates a DataProxyHandle instance. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Return value

Type Description
Promise<DataProxyHandle> Promise used to return the result.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.

Example:

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    dataShare.createDataProxyHandle().then((dataProxyHandle) => {
      console.info("createDataProxyHandle succeed");
    }).catch((err: BusinessError) => {
      console.error(`Failed to create DataProxyHandle. Code: ${err.code}, message: ${err.message}`);
    });
  }
}

ChangeType20+

Enumerates the data change types.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Name Value Description
INSERT 0 Data is inserted.
DELETE 1 Data is deleted.
UPDATE 2 Data is updated.

ProxyData20+

Defines a struct for shared configurations.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Name Type Read-Only Optional Description
uri string No No Unique ID of a shared configuration, fixed in the format of "datashareproxy://{bundleName}/{path}", in which bundleName indicates the bundle name of the publisher application, and path can be set to any value but must be unique in the same application. The value is a string with a maximum of 256 bytes.
value ValueType No Yes

Value of the shared configuration. If not specified, an empty string is used.

Note:

1. Before API version 26.0.0, the string length does not exceed 4096 bytes. Since API version 26.0.0, the default maximum string length allowed is 4096 bytes, and you can configure maxValueLength in DataProxyConfig to extend the maximum length to 102400 bytes.

2. When the shared configuration is published for the first time, if this parameter is not specified, an empty string is used by default. When the shared configuration is updated, if this parameter is not specified, the value of the shared configuration will not be updated.

allowList string[] No Yes

List of apps that are allowed to subscribe to and read the shared configuration. If not specified, an empty string array is used. The maximum length of the array is 256, and elements beyond 256 do not take effect. When the shared configuration is published for the first time, if this parameter is not specified, an empty allowlist is used by default. When the shared configuration is updated, if this parameter is not specified, the allowlist of the shared configuration will not be updated. An empty allowlist indicates that only the publisher can access the shared configuration.

Before API version 26.0.0, each element in the array is the appIdentifier of an app. The maximum length of a single appIdentifier is 128 bytes, and an appIdentifier longer than 128 bytes does not take effect.

Since API version 26.0.0, the array supports the special string "all" (case-sensitive), which indicates that all apps are allowed to access.

isMultiValues boolean No Yes

Whether this is a multi-value shared configuration that uses values. true indicates that the data published this time is of the multi-value type, in which case the value parameter is ignored. false indicates a non-multi-value type. The default value is false.

Since: 26.0.0

values Record<number, ValueType> No Yes

Value of the multi-value type. The first parameter in Record is the key, which is specified by the user and must be unique. The second parameter is the value corresponding to the key. A single app can add up to 10 values under a single URI, and the maximum length of each value is 4096 bytes. In addition, the total length of all values is limited by the maxValueLength field in DataProxyConfig. This parameter takes effect only when isMultiValues is set to true, and it cannot be empty. When isMultiValues is false, the default value is undefined.

Since: 26.0.0

trustProviders string[] No Yes

List of apps that can assign values to the multi-value shared configuration. The array can contain up to 256 elements, and elements beyond 256 do not take effect. Each element in the array is the appIdentifier of an app. The maximum length of an appIdentifier is 128 bytes, and the part longer than 128 bytes does not take effect.

If this parameter is not set when the shared configuration is published for the first time, the assignment list is empty by default. An empty assignment list indicates that only the publisher can assign values to the multi-value shared configuration. The array supports the special string "all" (case-sensitive), which indicates that all apps are allowed to assign values to the multi-value shared configuration. This parameter takes effect only when isMultiValues is set to true.

Since: 26.0.0

DataProxyChangeInfo20+

Defines a struct for notifying subscribers of the shared configuration changes, including data change type, URI, and content.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Name Type Read-Only Optional Description
type ChangeType No No Data change type.
uri string No No URI for notifying the change. The fixed format is "datashareproxy://{bundleName}/{path}", where bundleName is the bundle name of the publisher application of the configuration, and path can be any value but must not be duplicated within the same app. The string length cannot exceed 256 bytes.
value ValueType No No Changed data.
values ValueType[] No Yes

Change data of the multi-value type. If the changed data type is not a multi-value type, the value of values is undefined.

Since: 26.0.0

DataProxyErrorCode20+

Enumerates the status code returned by the batch operations of shared configuration.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Name Value Description
SUCCESS 0 The operation is successful.
URI_NOT_EXIST 1 The URI does not exist or the URI is not subscribed to.
NO_PERMISSION 2 No permission to perform this operation on the URI.
OVER_LIMIT 3 Before API version 26.0.0, indicates that the configurations published by the current app exceed the upper limit of 32 configurations; from API version 26.0.0, indicates that the configurations published by the current app exceed the upper limit of 64 configurations, or the value of the obtained share configuration item exceeds the maximum length limit configured by the maxValueLength field in DataProxyConfig.

DataProxyResult20+

Defines a struct for the batch operation result of shared configuration.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Name Type Read-Only Optional Description
uri string No No URI to be operated, with a maximum of 256 bytes. The value is fixed in the format of "datashareproxy://{bundleName}/{path}", in which bundleName indicates the bundle name of the publisher application, and path can be set to any value but must be unique in the same application.
result DataProxyErrorCode No No Operation result code.

DataProxyGetResult20+

Defines a struct for obtaining the batch operation result of shared configuration.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Name Type Read-Only Optional Description
uri string No No URI to be operated, with a maximum of 256 bytes. The value is fixed in the format of "datashareproxy://{bundleName}/{path}", in which bundleName indicates the bundle name of the publisher application, and path can be set to any value but must be unique in the same application.
result DataProxyErrorCode No No Operation result code.
value ValueType | undefined No No If the operation is successful, the value is the one set in shared configuration; otherwise, the value is undefined.
allowList string[] | undefined No No If the operation is successful, the allowlist is the one set in shared configuration; otherwise, the allowlist is undefined. Only the publisher can obtain the allowlist. Other applications can obtain only the value.

DataProxyType20+

Enumerates the data proxy types.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Name Value Description
SHARED_CONFIG 0 Inter-application shared configuration.

DataProxyConfig20+

Defines a struct for the data proxy configuration.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Name Type Read-Only Optional Description
type DataProxyType No No Type of the data proxy.
maxValueLength DataProxyMaxValueLength No Yes

Maximum allowed length of the value of the shared configuration. If not filled, the default value is MAX_LENGTH_4K, that is, the maximum allowed length of the value of the shared configuration is 4096 bytes.

Since: 26.0.0

DataProxyMaxValueLength

Enumerates the maximum length allowed for the value of a share configuration.

Since: 26.0.0

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Name Value Description
MAX_LENGTH_4K 4096 Indicates that the maximum length allowed for the value of a share configuration is 4096 bytes.
MAX_LENGTH_100K 102400 Indicates that the maximum length allowed for the value of a share configuration is 102400 bytes.

DataProxyHandle20+

Defines the data proxy handle, which can be used to access or manage shared configuration information. Before calling an API provided by DataProxyHandle, you must create a DataProxyHandle instance using createDataProxyHandle.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

[h2]on('dataChange')20+

on(event: 'dataChange', uris: string[], config: DataProxyConfig, callback: AsyncCallback<DataProxyChangeInfo[]>): DataProxyResult[]

Subscribes to the change event of the shared configuration corresponding to a specified URI. If the change event is subscribed, the subscriber will receive a callback notification that carries the data change type, changed URI, and changed content when the publisher modifies the configuration. This API uses an asynchronous callback to return the result. This function does not support cross-user notification subscription or subscription to unpublished configurations. If the permission is revoked after the subscription is successful, the subscriber will not be notified consequently.

Paired call

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Parameters

Name Type Mandatory Description
event string Yes Event or callback type. The value is dataChange, which indicates the data change. This event is triggered when the publisher modifies the configuration.
uris string[] Yes

Array of URIs corresponding to the share configurations to subscribe to.

NOTE

1. Before API version 26.0.0, the maximum length of the array is 32; from API version 26.0.0, the maximum length of the array is 64.

2. The URI is in the fixed format of "datashareproxy://{bundleName}/{path}", where bundleName is the bundleName of the publisher app of the configuration, path can be any value but must be unique within the same app, and the string length cannot exceed 256 bytes.

config DataProxyConfig Yes Configuration of the data proxy operation. Since API version 26.0.0, when the length of the changed share configuration content exceeds the maximum length limit configured by the maxValueLength field in DataProxyConfig, the share configuration content is truncated.
callback AsyncCallback<DataProxyChangeInfo[]> Yes Callback invoked to return the result. If the subscription is successful, err is undefined and data is the obtained array of DataProxyChangeInfo objects, which contains the change type, URI, and changed share configuration content. Otherwise, err is an error object.

Return value

Type Description
DataProxyResult[] Batch operation result array.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.
15700014 The parameter format is incorrect or the value range is invalid.

Example:

const urisToWatch: string[] =
  ['datashareproxy://com.example.app1/config1', 'datashareproxy://com.example.app1/config2',];
const config: dataShare.DataProxyConfig = {
  type: dataShare.DataProxyType.SHARED_CONFIG,
};
const callback = (err: BusinessError<void>, changes: dataShare.DataProxyChangeInfo[]): void => {
  if (err) {
    console.error(`Failed to receive data change notification. Code: ${err.code}, message: ${err.message}`);
  } else {
    changes.forEach((change) => {
      console.info(`Change Type: ${change.type}, URI: ${change.uri}, Value: ${change.value}`);
    });
  }
};
const results: dataShare.DataProxyResult[] = dataProxyHandle.on('dataChange', urisToWatch, config, callback);
results.forEach((result) => {
  console.info(`URI: ${result.uri}, Result: ${result.result}`);
});

[h2]off('dataChange')20+

off(event: 'dataChange', uris: string[], config: DataProxyConfig, callback?: AsyncCallback<DataProxyChangeInfo[]>): DataProxyResult[]

Unsubscribes from the change event of the proxy data corresponding to a specified URI.

Paired call

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Parameters

Name Type Mandatory Description
event string Yes Event or callback type. The value is dataChange, which indicates the data change.
uris string[] Yes

URI array of the shared configurations to unsubscribe from.

NOTE

1. Before API version 26.0.0, the maximum length of the array is 32. Since API version 26.0.0, the maximum length of the array is 64.

2. The URI is in the fixed format "datashareproxy://{bundleName}/{path}", where bundleName is the bundle name of the publisher application of the configuration, and path can be any value but must be unique within the same app. The string length cannot exceed 256 bytes.

config DataProxyConfig Yes Data proxy configuration.
callback AsyncCallback<DataProxyChangeInfo[]> No Callback function. When the unsubscription succeeds, err is undefined and data is the obtained DataProxyChangeInfo array, which contains the change type, URI, and changed shared configuration content; otherwise, it is an error object. If this parameter is not specified, all registered callback functions are unsubscribed.

Return value

Type Description
DataProxyResult[] Batch operation result array.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.
15700014 The parameter format is incorrect or the value range is invalid.

Example:

const urisToUnWatch: string[] =
  ['datashareproxy://com.example.app1/config1', 'datashareproxy://com.example.app1/config2',];
const config: dataShare.DataProxyConfig = {
  type: dataShare.DataProxyType.SHARED_CONFIG,
};
const callback = (err: BusinessError<void>, changes: dataShare.DataProxyChangeInfo[]): void => {
  if (err) {
    console.error(`Failed to receive data change notification. Code: ${err.code}, message: ${err.message}`);
  } else {
    changes.forEach((change) => {
      console.info(`Change Type: ${change.type}, URI: ${change.uri}, Value: ${change.value}`);
    });
  }
};
const results: dataShare.DataProxyResult[] = dataProxyHandle.off('dataChange', urisToUnWatch, config, callback);
results.forEach((result) => {
  console.info(`URI: ${result.uri}, Result: ${result.result}`);
});

[h2]publish20+

publish(data: ProxyData[], config: DataProxyConfig): Promise<DataProxyResult[]>

Publishes share configuration items. This API uses a promise to return the result.

After publication, the publisher and the apps specified in the allowlist can access the share configuration items.

If the URI to be published already exists, the corresponding share configuration item is updated. If the length of any URI in the configuration items to be published exceeds the upper limit or the format verification fails, the current publish operation fails.

Only the publisher is allowed to update share configuration items.

Before API version 26.0.0, each app supports a maximum of 32 share configurations. Since API version 26.0.0, each app supports a maximum of 64 share configurations.

Since API version 26.0.0, publishing multi-value type configurations is supported, and a URI can correspond to only one value type. After a configuration is published, the publish API cannot be used to update a published multi-value type URI. For the APIs for operating multi-value types, see putValue, removeValue, and getValues.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Parameters

Name Type Mandatory Description
data ProxyData[] Yes Array of share configuration items to be created or updated. Before API version 26.0.0, the maximum length of the array is 32; since API version 26.0.0, the maximum length of the array is 64.
config DataProxyConfig Yes Configuration of the data proxy operation. Since API version 26.0.0, if the length of any value in the published configuration items exceeds the maximum length limit configured by the maxValueLength field in DataProxyConfig, the current publish operation fails.

Return value

Type Description
Promise<DataProxyResult[]> Promise used to return the result array of the batch operations.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.
15700014 The parameter format is incorrect or the value range is invalid.

Example:

const newConfigData: dataShare.ProxyData[] = [{
  uri: 'datashareproxy://com.example.app1/config1',
  value: 'Value1',
  allowList: ['appIdentifier2', 'appIdentifier3'], // The string here is only an example. Replace it with the actual appIdentifier of the app when using it.
}, {
  uri: 'datashareproxy://com.example.app1/config2',
  value: 'Value2',
  allowList: ['appIdentifier3', 'appIdentifier4'], // The string here is only an example. Replace it with the actual appIdentifier of the app when using it.
}];
const config: dataShare.DataProxyConfig = {
  type: dataShare.DataProxyType.SHARED_CONFIG,
};
dataProxyHandle.publish(newConfigData, config).then((results: dataShare.DataProxyResult[]) => {
  results.forEach((result) => {
    console.info(`URI: ${result.uri}, Result: ${result.result}`);
  });
}).catch((error: BusinessError) => {
  console.error(`Failed to publish config. code: ${error.code}, message: ${error.message}`);
});

[h2]delete20+

delete(uris: string[], config: DataProxyConfig): Promise<DataProxyResult[]>

Deletes the specified shared configuration items based on URIs. This API uses a promise to return the result. Only the publisher is allowed to delete shared configuration items.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Parameters

Name Type Mandatory Description
uris string[] Yes

Array of URIs corresponding to the share configurations to be deleted.

NOTE

1. Before API version 26.0.0, the maximum length of the array is 32; starting from API version 26.0.0, the maximum length of the array is 64.

2. The URI is in the fixed format of "datashareproxy://{bundleName}/{path}", where bundleName is the bundle name of the publisher app of the configuration, and path can be set to any value but must be unique within the same app. The string length cannot exceed 256 bytes.

config DataProxyConfig Yes Data proxy configuration.

Return value

Type Description
Promise<DataProxyResult[]> Promise used to return the result array of the batch operations.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.
15700014 The parameter format is incorrect or the value range is invalid.

Example:

const urisToDelete: string[] =
  ['datashareproxy://com.example.app1/config1', 'datashareproxy://com.example.app1/config2',];
const config: dataShare.DataProxyConfig = {
  type: dataShare.DataProxyType.SHARED_CONFIG,
};
dataProxyHandle.delete(urisToDelete, config).then((results: dataShare.DataProxyResult[]) => {
  results.forEach((result) => {
    console.info(`URI: ${result.uri}, Result: ${result.result}`);
  });
}).catch((error: BusinessError) => {
  console.error(`Failed to delete config. code: ${error.code}, message: ${error.message}`);
});

[h2]deleteMyPublishedData

deleteMyPublishedData(config: DataProxyConfig): Promise<DataProxyResult[]>

Deletes all share configuration items published by the current publisher. This API uses a promise to return the result. Only the configuration publisher can delete share configuration items.

Since: 26.0.0

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Parameters

Name Type Mandatory Description
config DataProxyConfig Yes Configuration of the data proxy operation.

Return value

Type Description
Promise<DataProxyResult[]> Promise used to return the result. Returns an array of batch operation results.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.
15700014 The parameter format is incorrect or the value range is invalid.

Example

const config: dataShare.DataProxyConfig = {
  type: dataShare.DataProxyType.SHARED_CONFIG,
};
dataProxyHandle.deleteMyPublishedData(config).then((results: dataShare.DataProxyResult[]) => {
  results.forEach((result) => {
    console.info(`URI: ${result.uri}, Result: ${result.result}`);
  });
}).catch((error: BusinessError) => {
  console.error(`Failed to delete all configs. Code: ${error.code}, message: ${error.message}`);
});

[h2]get20+

get(uris: string[], config: DataProxyConfig): Promise<DataProxyGetResult[]>

Obtains a specified shared configuration item based on the URI. This API uses a promise to return the result. Only the publisher and applications in the allowed list can access the shared configuration item.

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Parameters

Name Type Mandatory Description
uris string[] Yes

Array of URIs of the share configurations to obtain.

NOTE

1. Before API version 26.0.0, the maximum length of the array is 32; since API version 26.0.0, the maximum length of the array is 64.

2. The URI is in the fixed format "datashareproxy://{bundleName}/{path}", where bundleName is the bundle name of the publisher app of the configuration, and path can be any value but must be unique within the same app. The string length cannot exceed 256 bytes.

config DataProxyConfig Yes Configuration of the data proxy operation. Since API version 26.0.0, the length of the value of the obtained share configuration item cannot exceed the maximum length limit configured by the maxValueLength field in DataProxyConfig. If the limit is exceeded, the status code of the return value of the corresponding operation result DataProxyErrorCode is OVER_LIMIT.

Return value

Type Description
Promise<DataProxyGetResult[]> Promise used to return the result array of the batch operations.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.
15700014 The parameter format is incorrect or the value range is invalid.

Example:

const urisToGet: string[] =
  ['datashareproxy://com.example.app1/config1', 'datashareproxy://com.example.app1/config2',];
const config: dataShare.DataProxyConfig = {
  type: dataShare.DataProxyType.SHARED_CONFIG,
};
dataProxyHandle.get(urisToGet, config).then((results: dataShare.DataProxyGetResult[]) => {
  results.forEach((result) => {
    console.info(`URI: ${result.uri}, Result: ${result.result}, AllowList: ${result.allowList}`);
  });
}).catch((error: BusinessError) => {
  console.error(`Failed to get config. code: ${error.code}, message: ${error.message}`);
});

[h2]putValue

putValue(uri: string, key: number, value: ValueType, config: DataProxyConfig): Promise<void>

Writes a value to the published data. This operation can be performed only on multi-value type data. This API uses a promise to return the result.

If the key passed in does not exist, a new value is added. If the key already exists, the value corresponding to the key is updated.

By default, a single data item (that is, a URI) can have a maximum of 10 values added in a single app, and the maximum length of each value is 4096 bytes. In addition, the total length of all values of a single data item (that is, a URI) in a single app is limited by the maxValueLength parameter value specified when the data is published using publish.

Since: 26.0.0

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Parameters

Name Type Mandatory Description
uri string Yes URI corresponding to the data to be operated. The fixed format is "datashareproxy://{bundleName}/{path}", where bundleName is the bundle name of the publisher application, and path can be set arbitrarily but must not be duplicated within the same app. The string length cannot exceed 256 bytes.
key number Yes

Key corresponding to the value to be added, which is unique for the same app.

The value range is all integers.

value ValueType Yes Value to be added.
config DataProxyConfig Yes Configuration of the data proxy operation. The maxValueLength parameter in the configuration does not take effect.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.
15700011 The URI does not exist.
15700014 The parameter format is incorrect or the value range is invalid.
15700015 No permission to access the data specified by the URI.

Example

const config: dataShare.DataProxyConfig = {
  type: dataShare.DataProxyType.SHARED_CONFIG,
};
let testUri: string = 'datashareproxy://com.test.dataproxyhandle/test/pv/001';
let newConfigData: dataShare.ProxyData[] = [{
  uri: testUri,
  values: { 0: 'init' },
  isMultiValues: true,
  allowList: [],
  trustProviders: []
}];

await dataProxyHandle?.publish(newConfigData, config).then((results: dataShare.DataProxyResult[]) => {
  results.forEach((result) => {
    console.info(`URI: ${result.uri}, Result: ${result.result}`);
  });
}).catch((error: BusinessError) => {
  console.error(`Failed to publish config. code: ${error.code}, message: ${error.message}`);
});

try {
  await dataProxyHandle?.putValue(testUri, 1, 'hello', config);
  console.info(`putValue success`);
} catch (error) {
  console.error(`putValue failed: code: ${error.code}, message: ${error.message}`);
}

[h2]removeValue

removeValue(uri: string, key: number, config: DataProxyConfig): Promise<void>

Removes the value corresponding to the key. This operation can be performed only on multi-value type data. Only values added by the current app can be removed. This API uses a promise to return the result.

Since: 26.0.0

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Parameters

Name Type Mandatory Description
uri string Yes URI corresponding to the data to be operated. The fixed format is "datashareproxy://{bundleName}/{path}", where bundleName is the bundle name of the publisher application, and path can be set arbitrarily but must not be duplicated within the same app. The string length cannot exceed 256 bytes.
key number Yes

Key corresponding to the value to be removed.

The value range is all integers.

config DataProxyConfig Yes Configuration of the data proxy operation.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.
15700011 The URI does not exist.
15700014 The parameter format is incorrect or the value range is invalid.
15700015 No permission to access the data specified by the URI.

Example

const config: dataShare.DataProxyConfig = {
  type: dataShare.DataProxyType.SHARED_CONFIG,
};
let testUri: string = 'datashareproxy://com.test.dataproxyhandle/test/pv/001';
let newConfigData: dataShare.ProxyData[] = [{
  uri: testUri,
  values: { 0: 'init' },
  isMultiValues: true,
  allowList: [],
  trustProviders: []
}];

await dataProxyHandle?.publish(newConfigData, config).then((results: dataShare.DataProxyResult[]) => {
  results.forEach((result) => {
    console.info(`URI: ${result.uri}, Result: ${result.result}`);
  });
}).catch((error: BusinessError) => {
  console.error(`Failed to publish config. code: ${error.code}, message: ${error.message}`);
});

try {
  await dataProxyHandle?.removeValue(testUri, 0, config);
  console.info(`removeValue success`);
} catch (error) {
  console.error(`removeValue failed: code: ${error.code}, message: ${error.message}`);
}

[h2]getValues

getValues(uri: string, config: DataProxyConfig): Promise<ValueType[]>

Obtains all multi-value type data under the specified URI. Only the publisher and the apps in the allowList can obtain this data. This API uses a promise to return the result.

Since: 26.0.0

System capability: SystemCapability.DistributedDataManager.DataShare.Consumer

Parameters

Name Type Mandatory Description
uri string Yes URI corresponding to the data to be operated. The fixed format is "datashareproxy://{bundleName}/{path}", where bundleName is the bundle name of the publisher application, and path can be set arbitrarily but must not be duplicated within the same app. The string length cannot exceed 256 bytes.
config DataProxyConfig Yes Configuration of the data proxy operation.

Return value

Type Description
Promise<ValueType[]> Promise used to return the result. Returns an array of all values under the URI.

Error codes

For details about the error codes, see DataShare Error Codes.

ID Error Message
15700000 Inner error. Possible causes: The service is not ready or is being restarted abnormally.
15700011 The URI does not exist.
15700014 The parameter format is incorrect or the value range is invalid.
15700015 No permission to access the data specified by the URI.

Example

const config: dataShare.DataProxyConfig = {
  type: dataShare.DataProxyType.SHARED_CONFIG,
};
let testUri: string = 'datashareproxy://com.test.dataproxyhandle/test/pv/001';
let newConfigData: dataShare.ProxyData[] = [{
  uri: testUri,
  values: { 0: 'init' },
  isMultiValues: true,
  allowList: [],
  trustProviders: []
}];

await dataProxyHandle!.publish(newConfigData, config).then((results: dataShare.DataProxyResult[]) => {
  results.forEach((result) => {
    console.info(`URI: ${result.uri}, Result: ${result.result}`);
  });
}).catch((error: BusinessError) => {
  console.error(`Failed to publish config. code: ${error.code}, message: ${error.message}`);
});

try {
  let result: ValueType[] = await dataProxyHandle?.getValues(testUri, config);
  console.info(`getValues success. Values: ` + JSON.stringify(result));
} catch (error) {
  console.error(`getValues failed: code: ${error.code}, message: ${error.message}`);
}