harmony 鸿蒙@ohos.contact (Contacts)

2022-08-09 浏览 (1191)

@ohos.contact (Contacts)

The contact module provides contact management functions, such as adding, deleting, and updating contacts.

NOTE

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

Modules to Import

import contact from '@ohos.contact';

contact.addContact10+

addContact(context: Context, contact: Contact, callback: AsyncCallback<number>): void

Adds a contact. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
contactContactYesContact information.
callbackAsyncCallback<number>YesCallback used to return the result, which is a contact ID.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  contact.addContact(
    globalThis.context as Context,
    {name: {fullName: 'xxx'},
      phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
    }, (err, data) => {
      if (err) {
        console.log(`addContact callback: err->${JSON.stringify(err)}`);
        return;
      }
      console.log(`addContact callback: success data->${JSON.stringify(data)}`);
  });

contact.addContact(deprecated)7+

addContact(contact:Contact, callback:AsyncCallback<number>): void

Adds a contact. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use addContact.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contactContactYesContact information.
callbackAsyncCallback<number>YesCallback used to return the result, which is a contact ID.

Example

// The sample code applies only to JS source files.
contact.addContact({
    name: {fullName: 'xxx'},
    phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
}, (err, data) => {
    if (err) {
        console.log(`addContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`addContact callback: success data->${JSON.stringify(data)}`);
});

contact.addContact10+

addContact(context: Context, contact: Contact): Promise<number>

Adds a contact. This API uses a promise to return the result.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
contactContactYesContact information.

Return Value

TypeDescription
Promise<number>Promise used to return the contact ID.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  let promise = contact.addContact(
    globalThis.context as Context,
    {name: {fullName: 'xxx'},
      phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
  });
  promise.then((data) => {
    console.log(`addContact success: data->${JSON.stringify(data)}`);
  }).catch((err) => {
    console.error(`addContact fail: err->${JSON.stringify(err)}`);
  });

contact.addContact(deprecated)7+

addContact(contact: Contact): Promise<number>

Adds a contact. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use addContact.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contactContactYesContact information.

Return Value

TypeDescription
Promise<number>Promise used to return the contact ID.

Example

// The sample code applies only to JS source files.
let promise = contact.addContact({
    name: {fullName: 'xxx'},
    phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
});
promise.then((data) => {
    console.log(`addContact success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`addContact fail: err->${JSON.stringify(err)}`);
});

contact.deleteContact10+

deleteContact(context: Context, key: string, callback: AsyncCallback<void>): void

Deletes a contact based on the specified contact key. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
keystringYesContact key. Each contact corresponds to one key.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }  
  contact.deleteContact(globalThis.context as Context, 'xxx', (err) => {
      if (err) {
          console.log(`deleteContact callback: err->${JSON.stringify(err)}`);
          return;
      }
      console.log('deleteContact success');
  });

contact.deleteContact(deprecated)7+

deleteContact(key: string, callback: AsyncCallback<void>): void

Deletes a contact based on the specified contact key. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use deleteContact.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
keystringYesContact key. Each contact corresponds to one key.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.deleteContact('xxx', (err) => {
    if (err) {
        console.log(`deleteContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log('deleteContact success');
});

contact.deleteContact10+

deleteContact(context: Context, key: string): Promise<void>

Deletes a contact based on the specified contact key. This API uses a promise to return the result.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
keystringYesContact key. Each contact corresponds to one key.

Return Value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
let promise = contact.deleteContact(globalThis.context as Context, 'xxx');
promise.then(() => {
    console.log(`deleteContact success`);
}).catch((err) => {
    console.error(`deleteContact fail: err->${JSON.stringify(err)}`);
});

contact.deleteContact(deprecated)7+

deleteContact(key: string): Promise<void>

Deletes a contact based on the specified contact key. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use deleteContact.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
keystringYesContact key. Each contact corresponds to one key.

Return Value

TypeDescription
Promise<void>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.deleteContact('xxx');
promise.then(() => {
    console.log(`deleteContact success`);
}).catch((err) => {
    console.error(`deleteContact fail: err->${JSON.stringify(err)}`);
});

contact.updateContact10+

updateContact(context: Context, contact: Contact, callback: AsyncCallback<void>): void

Updates a contact based on the specified contact information. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
contactContactYesContact information.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.updateContact(globalThis.context as Context, {
    id: 1,
    name: {fullName: 'xxx'},
    phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
}, (err) => {
    if (err) {
        console.log(`updateContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log('updateContact success');
});

contact.updateContact(deprecated)7+

updateContact(contact: Contact, callback: AsyncCallback<void>): void

Updates a contact based on the specified contact information. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use updateContact.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contactContactYesContact information.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.updateContact({
    id: 1,
    name: {fullName: 'xxx'},
    phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
}, (err) => {
    if (err) {
        console.log(`updateContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log('updateContact success');
});

contact.updateContact10+

updateContact(context: Context, contact: Contact, attrs: ContactAttributes, callback: AsyncCallback<void>): void

Updates a contact based on the specified contact information. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
contactContactYesContact information.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.updateContact(globalThis.context as Context, {
    id: 1,
    name: {fullName: 'xxx'},
    phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err) => {
    if (err) {
        console.log(`updateContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log('updateContact success');
});

contact.updateContact(deprecated)7+

updateContact(contact: Contact, attrs: ContactAttributes, callback: AsyncCallback<void>): void

Updates a contact based on the specified contact information. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use updateContact.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contactContactYesContact information.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.updateContact({
    id: 1,
    name: {fullName: 'xxx'},
    phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err) => {
    if (err) {
        console.log(`updateContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log('updateContact success');
});

contact.updateContact10+

updateContact(context: Context, contact: Contact, attrs?: ContactAttributes): Promise<void>

Updates a contact based on the specified contact information and attributes. This API uses a promise to return the result.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
contactContactYesContact information.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  let promise = contact.updateContact(globalThis.context as Context, {
      id: 1,
      name: {fullName: 'xxx'},
      phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
  }, {
      attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
  });
  promise.then(() => {
      console.log('updateContact success');
  }).catch((err) => {
      console.error(`updateContact fail: err->${JSON.stringify(err)}`);
  });

contact.updateContact(deprecated)7+

updateContact(contact: Contact, attrs?: ContactAttributes): Promise<void>

Updates a contact based on the specified contact information and attributes. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use updateContact.

Permission required: ohos.permission.WRITE_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contactContactYesContact information.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<void>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.updateContact({
    id: 1,
    name: {fullName: 'xxx'},
    phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
});
promise.then(() => {
    console.log('updateContact success');
}).catch((err) => {
    console.error(`updateContact fail: err->${JSON.stringify(err)}`);
});

contact.isLocalContact10+

isLocalContact(context: Context, id: number, callback: AsyncCallback<boolean>): void

Checks whether the ID of this contact is in the local address book. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
idnumberYesContact ID. Each contact corresponds to one ID.
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true indicates that the contact ID is in the local address book, and the value false indicates the opposite.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.isLocalContact(globalThis.context as Context, /*id*/1, (err, data) => {
    if (err) {
        console.log(`isLocalContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`isLocalContact callback: success data->${JSON.stringify(data)}`);
});

contact.isLocalContact(deprecated)7+

isLocalContact(id: number, callback: AsyncCallback<boolean>): void

Checks whether the ID of this contact is in the local address book. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use isLocalContact.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
idnumberYesContact ID. Each contact corresponds to one ID.
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true indicates that the contact ID is in the local address book, and the value false indicates the opposite.

Example

// The sample code applies only to JS source files.
contact.isLocalContact(/*id*/1, (err, data) => {
    if (err) {
        console.log(`isLocalContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`isLocalContact callback: success data->${JSON.stringify(data)}`);
});

contact.isLocalContact10+

isLocalContact(context: Context, id: number): Promise<boolean>

Checks whether the ID of this contact is in the local address book. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
idnumberYesContact ID. Each contact corresponds to one ID.

Return Value

TypeDescription
Promise<boolean>Promise used to return the result. The value true indicates that the contact ID is in the local address book, and the value false indicates the opposite.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  let promise = contact.isLocalContact(globalThis.context as Context, /*id*/1);
  promise.then((data) => {
      console.log(`isLocalContact success: data->${JSON.stringify(data)}`);
  }).catch((err) => {
      console.error(`isLocalContact fail: err->${JSON.stringify(err)}`);
  });

contact.isLocalContact(deprecated)7+

isLocalContact(id: number): Promise<boolean>

Checks whether the ID of this contact is in the local address book. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use isLocalContact.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
idnumberYesContact ID. Each contact corresponds to one ID.

Return Value

TypeDescription
Promise<boolean>Promise used to return the result. The value true indicates that the contact ID is in the local address book, and the value false indicates the opposite.

Example

// The sample code applies only to JS source files.
let promise = contact.isLocalContact(/*id*/1);
promise.then((data) => {
    console.log(`isLocalContact success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`isLocalContact fail: err->${JSON.stringify(err)}`);
});

contact.isMyCard10+

isMyCard(context: Context, id: number, callback: AsyncCallback<boolean>): void

Checks whether a contact is included in my card. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
idnumberYesContact ID.
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true indicates that the contact is included in my card, and the value false indicates the opposite.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  contact.isMyCard(globalThis.context as Context, /*id*/1, (err, data) => {
      if (err) {
          console.log(`isMyCard callback: err->${JSON.stringify(err)}`);
          return;
      }
      console.log(`isMyCard callback: success data->${JSON.stringify(data)}`);
  });

contact.isMyCard(deprecated)7+

isMyCard(id: number, callback: AsyncCallback<boolean>): void

Checks whether a contact is included in my card. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use isMyCard.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
idnumberYesContact ID.
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true indicates that the contact is included in my card, and the value false indicates the opposite.

Example

// The sample code applies only to JS source files.
contact.isMyCard(/*id*/1, (err, data) => {
    if (err) {
        console.log(`isMyCard callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`isMyCard callback: success data->${JSON.stringify(data)}`);
});

contact.isMyCard10+

isMyCard(context: Context, id: number): Promise<boolean>

Checks whether a contact is included in my card. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
idnumberYesContact ID.

Return Value

TypeDescription
Promise<boolean>Promise used to return the result. The value true indicates that the contact is included in my card, and the value false indicates the opposite.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  let promise = contact.isMyCard(globalThis.context as Context, /*id*/1);
  promise.then((data) => {
      console.log(`isMyCard success: data->${JSON.stringify(data)}`);
  }).catch((err) => {
      console.error(`isMyCard fail: err->${JSON.stringify(err)}`);
  });

contact.isMyCard(deprecated)7+

isMyCard(id: number): Promise<boolean>

Checks whether a contact is included in my card. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use isMyCard.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
idnumberYesContact ID.

Return Value

TypeDescription
Promise<boolean>Promise used to return the result. The value true indicates that the contact is included in my card, and the value false indicates the opposite.

Example

// The sample code applies only to JS source files.
let promise = contact.isMyCard(/*id*/1);
promise.then((data) => {
    console.log(`isMyCard success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`isMyCard fail: err->${JSON.stringify(err)}`);
});

contact.queryMyCard10+

queryMyCard(context: Context, callback: AsyncCallback<Contact>): void

Queries my card. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  contact.queryMyCard(globalThis.context as Context, (err, data) => {
      if (err) {
          console.log(`queryMyCard callback: err->${JSON.stringify(err)}`);
          return;
      }
      console.log(`queryMyCard callback: success data->${JSON.stringify(data)}`);
  });

contact.queryMyCard(deprecated)7+

queryMyCard(callback: AsyncCallback<Contact>): void

Queries my card. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryMyCard.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Contact>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryMyCard((err, data) => {
    if (err) {
        console.log(`queryMyCard callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryMyCard callback: success data->${JSON.stringify(data)}`);
});

contact.queryMyCard10+

queryMyCard(context: Context, attrs: ContactAttributes, callback: AsyncCallback<Contact>): void

Queries my card. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  contact.queryMyCard(globalThis.context as Context, {
      attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
  }, (err, data) => {
      if (err) {
          console.log(`queryMyCard callback: err->${JSON.stringify(err)}`);
          return;
      }
      console.log(`queryMyCard callback: success data->${JSON.stringify(data)}`);
  });

contact.queryMyCard(deprecated)7+

queryMyCard(attrs: ContactAttributes, callback: AsyncCallback<Contact>): void

Queries my card. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryMyCard.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryMyCard({
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryMyCard callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryMyCard callback: success data->${JSON.stringify(data)}`);
});

contact.queryMyCard10+

queryMyCard(context: Context, attrs?: ContactAttributes): Promise<Contact>

Queries my card based on the specified contact attributes. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Contact>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  let promise = contact.queryMyCard(globalThis.context as Context, {
      attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
  });
  promise.then((data) => {
      console.log(`queryMyCard success: data->${JSON.stringify(data)}`);
  }).catch((err) => {
      console.error(`queryMyCard fail: err->${JSON.stringify(err)}`);
  });

contact.queryMyCard(deprecated)7+

queryMyCard(attrs?: ContactAttributes): Promise<Contact>

Queries my card based on the specified contact attributes. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryMyCard.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Contact>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.queryMyCard({
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
});
promise.then((data) => {
    console.log(`queryMyCard success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryMyCard fail: err->${JSON.stringify(err)}`);
});

contact.selectContact(deprecated)7+

selectContact(callback: AsyncCallback<Array<Contact>>): void

Selects a contact. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use selectContacts.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.Contacts

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.selectContact((err, data) => {
    if (err) {
        console.log(`selectContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`selectContact callback: success data->${JSON.stringify(data)}`);
});

contact.selectContact(deprecated)7+

selectContact(): Promise<Array<Contact>>

Selects a contact. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use selectContacts.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.Contacts

Return Value

TypeDescription
Promise<Array<Contact>>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.selectContact();
promise.then((data) => {
    console.log(`selectContact success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`selectContact fail: err->${JSON.stringify(err)}`);
});

contact.selectContacts10+

selectContacts(callback: AsyncCallback<Array<Contact>>): void

Selects a contact. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Applications.Contacts

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
401Parameter error.

Example

// The sample code applies only to JS source files.
contact.selectContacts((err, data) => {
    if (err) {
        console.log(`selectContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`selectContact callback: success data->${JSON.stringify(data)}`);
});

contact.selectContacts10+

selectContacts(): Promise<Array<Contact>>

Selects a contact. This API uses a promise to return the result.

System capability: SystemCapability.Applications.Contacts

Return Value

TypeDescription
Promise<Array<Contact>>Promise used to return the result.

Error codes

IDError Message
401Parameter error.

Example

// The sample code applies only to JS source files.
let promise = contact.selectContacts();
promise.then((data) => {
    console.log(`selectContact success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`selectContact fail: err->${JSON.stringify(err)}`);
});

contact.selectContacts10+

selectContacts(options: ContactSelectionOptions, callback: AsyncCallback<Array<Contact>>): void

Selects a contact. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Applications.Contacts

Parameters

NameTypeMandatoryDescription
optionsContactSelectionOptionsYesContact selection options.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
401Parameter error.

Example

// The sample code applies only to JS source files.
contact.selectContacts({
  isMultiSelect:false
}, (err, data) => {
    if (err) {
        console.log(`selectContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`selectContact callback: success data->${JSON.stringify(data)}`);
});

contact.selectContacts10+

selectContacts(options: ContactSelectionOptions): Promise<Array<Contact>>

Selects a contact. This API uses a promise to return the result.

System capability: SystemCapability.Applications.Contacts

Return Value

TypeDescription
optionsContactSelectionOptions
Promise<Array<Contact>>Promise used to return the result.

Error codes

IDError Message
401Parameter error.

Example

// The sample code applies only to JS source files.
let promise = contact.selectContacts({isMultiSelect:false});
promise.then((data) => {
    console.log(`selectContact success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`selectContact fail: err->${JSON.stringify(err)}`);
});

contact.queryContact10+

queryContact(context: Context, key: string, callback: AsyncCallback<Contact>): void

Queries a contact based on the specified key. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
keystringYesContact key. Each contact corresponds to one key.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContact(globalThis.context as Context, 'xxx', (err, data) => {
    if (err) {
        console.log(`queryContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContact callback: success data->${JSON.stringify(data)}`);
});

contact.queryContact(deprecated)7+

queryContact(key: string, callback: AsyncCallback<Contact>): void

Queries a contact based on the specified key. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContact.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
keystringYesContact key. Each contact corresponds to one key.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContact('xxx', (err, data) => {
    if (err) {
        console.log(`queryContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContact callback: success data->${JSON.stringify(data)}`);
});

contact.queryContact10+

queryContact(context: Context, key: string, holder: Holder, callback: AsyncCallback<Contact>): void

Queries a contact based on the specified key. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
keystringYesContact key. Each contact corresponds to one key.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContact(globalThis.context as Context, 'xxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContact callback: success data->${JSON.stringify(data)}`);
});

contact.queryContact(deprecated)7+

queryContact(key: string, holder: Holder, callback: AsyncCallback<Contact>): void

Queries a contact based on the specified key. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContact.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
keystringYesContact key. Each contact corresponds to one key.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContact('xxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContact callback: success data->${JSON.stringify(data)}`);
});

contact.queryContact10+

queryContact(context: Context, key: string, attrs: ContactAttributes, callback: AsyncCallback<Contact>): void

Queries a contact based on the specified key. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
keystringYesContact key. Each contact corresponds to one key.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContact(globalThis.context as Context, 'xxx', {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContact callback: success data->${JSON.stringify(data)}`);
});

contact.queryContact(deprecated)7+

queryContact(key: string, attrs: ContactAttributes, callback: AsyncCallback<Contact>): void

Queries a contact based on the specified key. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContact.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
keystringYesContact key. Each contact corresponds to one key.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContact('xxx', {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContact callback: success data->${JSON.stringify(data)}`);
});

contact.queryContact10+

queryContact(context: Context, key: string, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Contact>): void

Queries a contact based on the specified key. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
keystringYesContact key. Each contact corresponds to one key.
holderHolderYesApplication that creates the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

  // The sample code applies only to JS source files.
  // Obtain the context.
  import UIAbility from '@ohos.app.ability.UIAbility';
  class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
      globalThis.context = this.context;
    }
  }
  contact.queryContact(globalThis.context as Context, 'xxx', {
      holderId: 0,
      bundleName: "",
      displayName: ""
  }, {
      attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
  }, (err, data) => {
      if (err) {
          console.log(`queryContact callback: err->${JSON.stringify(err)}`);
          return;
      }
      console.log(`queryContact callback: success data->${JSON.stringify(data)}`);
  });

contact.queryContact(deprecated)7+

queryContact(key: string, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Contact>): void

Queries a contact based on the specified key. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContact.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
keystringYesContact key. Each contact corresponds to one key.
holderHolderYesApplication that creates the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Contact>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContact('xxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContact callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContact callback: success data->${JSON.stringify(data)}`);
});

contact.queryContact10+

queryContact(context: Context, key: string, holder?: Holder, attrs?: ContactAttributes): Promise<Contact>

Queries contacts based on the specified key, application, and attributes. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
keystringYesContact key. Each contact corresponds to one key.
holderHolderNoApplication that creates the contacts.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Contact>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
let promise = contact.queryContact(globalThis.context as Context, 'xxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
});
promise.then((data) => {
    console.log(`queryContact success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryContact fail: err->${JSON.stringify(err)}`);
});

contact.queryContact(deprecated)7+

queryContact(key: string, holder?: Holder, attrs?: ContactAttributes): Promise<Contact>

Queries contacts based on the specified key, application, and attributes. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContact.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
keystringYesContact key. Each contact corresponds to one key.
holderHolderNoApplication that creates the contacts.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Contact>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.queryContact('xxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
});
promise.then((data) => {
    console.log(`queryContact success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryContact fail: err->${JSON.stringify(err)}`);
});

contact.queryContacts10+

queryContacts(context: Context, callback: AsyncCallback<Array<Contact>>): void

Queries all contacts. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContacts(globalThis.context as Context, (err, data) => {
    if (err) {
        console.log(`queryContacts callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContacts callback: success data->${JSON.stringify(data)}`);
});

contact.queryContacts(deprecated)7+

queryContacts(callback: AsyncCallback<Array<Contact>>): void

Queries all contacts. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContacts.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContacts((err, data) => {
    if (err) {
        console.log(`queryContacts callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContacts callback: success data->${JSON.stringify(data)}`);
});

contact.queryContacts10+

queryContacts(context: Context, holder: Holder, callback: AsyncCallback<Array<Contact>>): void

Queries all contacts. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContacts(globalThis.context as Context, {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryContacts callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContacts callback: success data->${JSON.stringify(data)}`);
});

contact.queryContacts(deprecated)7+

queryContacts(holder: Holder, callback: AsyncCallback<Array<Contact>>): void

Queries all contacts. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContacts.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContacts({
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryContacts callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContacts callback: success data->${JSON.stringify(data)}`);
});

contact.queryContacts10+

queryContacts(context: Context, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries all contacts. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContacts(globalThis.context as Context, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContacts callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContacts callback: success data->${JSON.stringify(data)}`);
});

contact.queryContacts(deprecated)7+

queryContacts(attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries all contacts. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContacts.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContacts({
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContacts callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContacts callback: success data->${JSON.stringify(data)}`);
});

contact.queryContacts10+

queryContacts(context: Context, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries all contacts. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
holderHolderYesApplication that creates the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContacts(globalThis.context as Context, {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContacts callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContacts callback: success data->${JSON.stringify(data)}`);
});

contact.queryContacts(deprecated)7+

queryContacts(holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries all contacts. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContacts.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
holderHolderYesApplication that creates the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContacts({
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContacts callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContacts callback: success data->${JSON.stringify(data)}`);
});

contact.queryContacts10+

queryContacts(context: Context, holder?: Holder, attrs?: ContactAttributes): Promise<Array<Contact>>

Queries all contacts based on the specified application and attributes. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
holderHolderNoApplication that creates the contacts.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Array<Contact>>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
let promise = contact.queryContacts(globalThis.context as Context, {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
});
promise.then((data) => {
    console.log(`queryContacts success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryContacts fail: err->${JSON.stringify(err)}`);
});

contact.queryContacts(deprecated)7+

queryContacts(holder?: Holder, attrs?: ContactAttributes): Promise<Array<Contact>>

Queries all contacts based on the specified application and attributes. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContacts.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
holderHolderNoApplication that creates the contacts.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Array<Contact>>Promise used to return the result.

Example

  // The sample code applies only to JS source files.
  let promise = contact.queryContacts({
      holderId: 0,
      bundleName: "",
      displayName: ""
  }, {
      attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
  });
  promise.then((data) => {
      console.log(`queryContacts success: data->${JSON.stringify(data)}`);
  }).catch((err) => {
      console.error(`queryContacts fail: err->${JSON.stringify(err)}`);
  });

contact.queryContactsByPhoneNumber10+

queryContactsByPhoneNumber(context: Context, phoneNumber: string, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified phone number. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
phoneNumberstringYesPhone number of the contacts.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContactsByPhoneNumber(globalThis.context as Context, '138xxxxxxxx', (err, data) => {
    if (err) {
        console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByPhoneNumber(deprecated)7+

queryContactsByPhoneNumber(phoneNumber: string, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified phone number. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByPhoneNumber.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
phoneNumberstringYesPhone number of the contacts.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContactsByPhoneNumber('138xxxxxxxx', (err, data) => {
    if (err) {
        console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByPhoneNumber10+

queryContactsByPhoneNumber(context: Context, phoneNumber: string, holder: Holder, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified phone number. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
phoneNumberstringYesPhone number of the contacts.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContactsByPhoneNumber(globalThis.context as Context, '138xxxxxxxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByPhoneNumber(deprecated)7+

queryContactsByPhoneNumber(phoneNumber: string, holder: Holder, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified phone number. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByPhoneNumber.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
phoneNumberstringYesPhone number of the contacts.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContactsByPhoneNumber('138xxxxxxxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByPhoneNumber10+

queryContactsByPhoneNumber(context: Context, phoneNumber: string, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified phone number. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
phoneNumberstringYesPhone number of the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContactsByPhoneNumber(globalThis.context as Context, '138xxxxxxxx', {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByPhoneNumber(deprecated)7+

queryContactsByPhoneNumber(phoneNumber: string, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified phone number. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByPhoneNumber.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
phoneNumberstringYesPhone number of the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContactsByPhoneNumber('138xxxxxxxx', {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByPhoneNumber10+

queryContactsByPhoneNumber(context: Context, phoneNumber: string, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified phone number. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
phoneNumberstringYesPhone number of the contacts.
holderHolderYesApplication that creates the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContactsByPhoneNumber(globalThis.context as Context, '138xxxxxxxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByPhoneNumber(deprecated)7+

queryContactsByPhoneNumber(phoneNumber: string, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified phone number. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByPhoneNumber.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
phoneNumberstringYesPhone number of the contacts.
holderHolderYesApplication that creates the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContactsByPhoneNumber('138xxxxxxxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByPhoneNumber10+

queryContactsByPhoneNumber(context: Context, phoneNumber: string, holder?: Holder, attrs?: ContactAttributes): Promise<Array<Contact>>

Queries contacts based on the specified phone number, application, and attributes. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
phoneNumberstringYesPhone number of the contacts.
holderHolderNoApplication that creates the contacts.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Array<Contact>>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
let promise = contact.queryContactsByPhoneNumber(globalThis.context as Context, '138xxxxxxxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
});
promise.then((data) => {
    console.log(`queryContactsByPhoneNumber success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryContactsByPhoneNumber fail: err->${JSON.stringify(err)}`);
});

contact.queryContactsByPhoneNumber(deprecated)7+

queryContactsByPhoneNumber(phoneNumber: string, holder?: Holder, attrs?: ContactAttributes): Promise<Array<Contact>>

Queries contacts based on the specified phone number, application, and attributes. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByPhoneNumber.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
phoneNumberstringYesPhone number of the contacts.
holderHolderNoApplication that creates the contacts.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Array<Contact>>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.queryContactsByPhoneNumber('138xxxxxxxx', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
});
promise.then((data) => {
    console.log(`queryContactsByPhoneNumber success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryContactsByPhoneNumber fail: err->${JSON.stringify(err)}`);
});

contact.queryContactsByEmail10+

queryContactsByEmail(context: Context, email: string, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified email address. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
emailstringYesEmail address of the contact.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContactsByEmail(globalThis.context as Context, 'xxx@email.com', (err, data) => {
    if (err) {
        console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByEmail(deprecated)7+

queryContactsByEmail(email: string, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified email address. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByEmail.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
emailstringYesEmail address of the contact.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContactsByEmail('xxx@email.com', (err, data) => {
    if (err) {
        console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByEmail10+

queryContactsByEmail(context: Context, email: string, holder: Holder, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified email address. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
emailstringYesEmail address of the contact.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContactsByEmail(globalThis.context as Context, 'xxx@email.com', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByEmail(deprecated)7+

queryContactsByEmail(email: string, holder: Holder, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified email address. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByEmail.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
emailstringYesEmail address of the contact.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContactsByEmail('xxx@email.com', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByEmail10+

queryContactsByEmail(context: Context, email: string, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified email address. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
emailstringYesEmail address of the contact.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContactsByEmail(globalThis.context as Context, 'xxx@email.com', {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByEmail(deprecated)7+

queryContactsByEmail(email: string, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified email address. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByEmail.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
emailstringYesEmail address of the contact.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContactsByEmail('xxx@email.com', {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByEmail10+

queryContactsByEmail(context: Context, email: string, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified email address. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
emailstringYesEmail address of the contact.
holderHolderYesApplication that creates the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryContactsByEmail(globalThis.context as Context, 'xxx@email.com', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByEmail(deprecated)7+

queryContactsByEmail(email: string, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void

Queries contacts based on the specified email address. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByEmail.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
emailstringYesEmail address of the contact.
holderHolderYesApplication that creates the contacts.
attrsContactAttributesYesList of contact attributes.
callbackAsyncCallback<Array<Contact>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryContactsByEmail('xxx@email.com', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
}, (err, data) => {
    if (err) {
        console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`);
});

contact.queryContactsByEmail10+

queryContactsByEmail(context: Context, email: string, holder?: Holder, attrs?: ContactAttributes): Promise<Array<Contact>>

Queries contacts based on the specified email address, application, and attributes. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
emailstringYesEmail address of the contact.
holderHolderNoApplication that creates the contacts.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Array<Contact>>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
let promise = contact.queryContactsByEmail(globalThis.context as Context, 'xxx@email.com', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
});
promise.then((data) => {
    console.log(`queryContactsByEmail success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryContactsByEmail fail: err->${JSON.stringify(err)}`);
});

contact.queryContactsByEmail(deprecated)7+

queryContactsByEmail(email: string, holder?: Holder, attrs?: ContactAttributes): Promise<Array<Contact>>

Queries contacts based on the specified email address, application, and attributes. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryContactsByEmail.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
emailstringYesEmail address of the contact.
holderHolderNoApplication that creates the contacts.
attrsContactAttributesNoList of contact attributes.

Return Value

TypeDescription
Promise<Array<Contact>>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.queryContactsByEmail('xxx@email.com', {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, {
    attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME]
});
promise.then((data) => {
    console.log(`queryContactsByEmail success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryContactsByEmail fail: err->${JSON.stringify(err)}`);
});

contact.queryGroups10+

queryGroups(context: Context, callback: AsyncCallback<Array<Group>>): void

Queries all groups of this contact. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
callbackAsyncCallback<Array<Group>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryGroups(globalThis.context as Context, (err, data) => {
    if (err) {
        console.log(`queryGroups callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryGroups callback: success data->${JSON.stringify(data)}`);
});

contact.queryGroups(deprecated)7+

queryGroups(callback: AsyncCallback<Array<Group>>): void

Queries all groups of this contact. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryGroups.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<Group>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryGroups((err, data) => {
    if (err) {
        console.log(`queryGroups callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryGroups callback: success data->${JSON.stringify(data)}`);
});

contact.queryGroups10+

queryGroups(context: Context, holder: Holder, callback: AsyncCallback<Array<Group>>): void

Queries all groups of this contact. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Array<Group>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryGroups(globalThis.context as Context, {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryGroups callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryGroups callback: success data->${JSON.stringify(data)}`);
});

contact.queryGroups(deprecated)7+

queryGroups(holder: Holder, callback: AsyncCallback<Array<Group>>): void

Queries all groups of this contact. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryGroups.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<Array<Group>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryGroups({
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryGroups callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryGroups callback: success data->${JSON.stringify(data)}`);
});

contact.queryGroups10+

queryGroups(context: Context, holder?: Holder): Promise<Array<Group>>

Queries all groups of this contact based on the specified application. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
holderHolderNoApplication that creates the contacts.

Return Value

TypeDescription
Promise<Array<Group>>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
let promise = contact.queryGroups(globalThis.context as Context, {
    holderId: 0,
    bundleName: "",
    displayName: ""
});
promise.then((data) => {
    console.log(`queryGroups success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryGroups fail: err->${JSON.stringify(err)}`);
});

contact.queryGroups(deprecated)7+

queryGroups(holder?: Holder): Promise<Array<Group>>

Queries all groups of this contact based on the specified application. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryGroups.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
holderHolderNoApplication that creates the contacts.

Return Value

TypeDescription
Promise<Array<Group>>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.queryGroups({
    holderId: 0,
    bundleName: "",
    displayName: ""
});
promise.then((data) => {
    console.log(`queryGroups success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryGroups fail: err->${JSON.stringify(err)}`);
});

contact.queryHolders10+

queryHolders(context: Context, callback: AsyncCallback<Array<Holder>>): void

Queries all applications that have created contacts. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
callbackAsyncCallback<Array<Holder>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryHolders(globalThis.context as Context, (err, data) => {
    if (err) {
        console.log(`queryHolders callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryHolders callback: success data->${JSON.stringify(data)}`);
});

contact.queryHolders(deprecated)7+

queryHolders(callback: AsyncCallback<Array<Holder>>): void

Queries all applications that have created contacts. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryHolders.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<Holder>>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryHolders((err, data) => {
    if (err) {
        console.log(`queryHolders callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryHolders callback: success data->${JSON.stringify(data)}`);
});

contact.queryHolders10+

queryHolders(context: Context): Promise<Array<Holder>>

Queries all applications that have created contacts. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.

Return Value

TypeDescription
Promise<Array<Holder>>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
let promise = contact.queryHolders(globalThis.context as Context);
promise.then((data) => {
    console.log(`queryHolders success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryHolders fail: err->${JSON.stringify(err)}`);
});

contact.queryHolders(deprecated)7+

queryHolders(): Promise<Array<Holder>>

Queries all applications that have created contacts. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryHolders.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Return Value

TypeDescription
Promise<Array<Holder>>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.queryHolders();
promise.then((data) => {
    console.log(`queryHolders success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryHolders fail: err->${JSON.stringify(err)}`);
});

contact.queryKey10+

queryKey(context: Context, id: number, callback: AsyncCallback<string>): void

Queries the key of a contact based on the specified contact ID. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
idnumberYesContact ID.
callbackAsyncCallback<string>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryKey(globalThis.context as Context, /*id*/1, (err, data) => {
    if (err) {
        console.log(`queryKey callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryKey callback: success data->${JSON.stringify(data)}`);
});

contact.queryKey(deprecated)7+

queryKey(id: number, callback: AsyncCallback<string>): void

Queries the key of a contact based on the specified contact ID. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryKey.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
idnumberYesContact ID.
callbackAsyncCallback<string>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryKey(/*id*/1, (err, data) => {
    if (err) {
        console.log(`queryKey callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryKey callback: success data->${JSON.stringify(data)}`);
});

contact.queryKey10+

queryKey(context: Context, id: number, holder: Holder, callback: AsyncCallback<string>): void

Queries the key of a contact based on the specified contact ID. This API uses an asynchronous callback to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
idnumberYesContact ID.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<string>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
contact.queryKey(globalThis.context as Context, /*id*/1, {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryKey callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryKey callback: success data->${JSON.stringify(data)}`);
});

contact.queryKey(deprecated)7+

queryKey(id: number, holder: Holder, callback: AsyncCallback<string>): void

Queries the key of a contact based on the specified contact ID. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryKey.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
idnumberYesContact ID.
holderHolderYesApplication that creates the contacts.
callbackAsyncCallback<string>YesCallback used to return the result.

Example

// The sample code applies only to JS source files.
contact.queryKey(/*id*/1, {
    holderId: 0,
    bundleName: "",
    displayName: ""
}, (err, data) => {
    if (err) {
        console.log(`queryKey callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`queryKey callback: success data->${JSON.stringify(data)}`);
});

contact.queryKey10+

queryKey(context: Context, id: number, holder?: Holder): Promise<string>

Queries the key of a contact based on the specified contact ID and application. This API uses a promise to return the result.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context. For details about the application context of the stage model, see Context.
idnumberYesContact ID.
holderHolderNoApplication that creates the contacts.

Return Value

TypeDescription
Promise<string>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
401Parameter error.

Example

// The sample code applies only to JS source files.
// Obtain the context.
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage){
    globalThis.context = this.context;
  }
}
let promise = contact.queryKey(globalThis.context as Context, /*id*/1, {
    holderId: 0,
    bundleName: "",
    displayName: ""
});
promise.then((data) => {
    console.log(`queryKey success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryKey fail: err->${JSON.stringify(err)}`);
});

contact.queryKey(deprecated)7+

queryKey(id: number, holder?: Holder): Promise<string>

Queries the key of a contact based on the specified contact ID and application. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 10. You are advised to use queryKey.

Permission required: ohos.permission.READ_CONTACTS

System capability: SystemCapability.Applications.ContactsData

Parameters

NameTypeMandatoryDescription
idnumberYesContact ID.
holderHolderNoApplication that creates the contacts.

Return Value

TypeDescription
Promise<string>Promise used to return the result.

Example

// The sample code applies only to JS source files.
let promise = contact.queryKey(/*id*/1, {
    holderId: 0,
    bundleName: "",
    displayName: ""
});
promise.then((data) => {
    console.log(`queryKey success: data->${JSON.stringify(data)}`);
}).catch((err) => {
    console.error(`queryKey fail: err->${JSON.stringify(err)}`);
});

ContactSelectionOptions10+

Defines the contact selection options.

System capability: SystemCapability.Applications.Contacts

NameTypeMandatoryDescription
isMultiSelect 10+booleanNoWhether multiple contacts can be selected.

Contact

Defines a contact.

System capability: SystemCapability.Applications.ContactsData

Constant

NameValueDescription
INVALID_CONTACT_ID-1Default contact ID.

Attributes

NameTypeReadableWritableDescription
idnumberYesNoContact ID.
keystringYesNoContact key.
contactAttributesContactAttributesYesYesList of contact attributes.
emailsEmail[]YesYesList of email addresses of the contact.
eventsEvent[]YesYesList of important dates such as birthdays and anniversaries of the contact.
groupsGroup[]YesYesList of groups of the contact.
imAddressesImAddress[]YesYesList of instant message addresses of the contact.
phoneNumbersPhoneNumber[]YesYesList of phone numbers of the contact.
portraitPortraitYesYesContact portrait.
postalAddressesPostalAddress[]YesYesList of postal addresses of the contact.
relationsRelation[]YesYesList of relationships with the contact.
sipAddressesSipAddress[]YesYesList of Session Initiation Protocol (SIP) addresses of the contact.
websitesWebsite[]YesYesList of websites of the contact.
nameNameYesYesContact name.
nickNameNickNameYesYesContact nickname.
noteNoteYesYesContact notes.
organizationOrganizationYesYesOrganization of the contact.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let myContact = {
    phoneNumbers: [{
        phoneNumber: "138xxxxxxxx"
    }],
    name: {
        fullName: "fullName",
        namePrefix: "namePrefix"
    },
    nickName: {
        nickName: "nickName"
    }
};

Or, create data by configuring a new Contact object.

// The sample code applies only to JS source files.
let myContact = new contact.Contact();
let name = new contact.Name();
name.fullName = "fullName";
let phoneNumber = new contact.PhoneNumber();
phoneNumber.phoneNumber = "138xxxxxxxx";
myContact.name = name;
myContact.phoneNumbers = [phoneNumber];

ContactAttributes

Provides a list of contact attributes, which are generally used as arguments. If null is passed, all attributes are queried by default.

System capability: SystemCapability.Applications.ContactsData

NameTypeReadableWritableDescription
attributesAttribute[]YesYesList of contact attributes.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let contactAttributes = {
    attributes: [
        contact.Attribute.ATTR_EMAIL,
        contact.Attribute.ATTR_NAME,
        contact.Attribute.ATTR_PHONE
    ]
};

Or, create data by configuring a ContactAttributes object.

// The sample code applies only to JS source files.
let contactAttributes = new contact.ContactAttributes();
contactAttributes.attributes = [contact.Attribute.ATTR_EMAIL];

Attribute

Enumerates contact attributes.

System capability: SystemCapability.Applications.ContactsData

NameDescription
ATTR_CONTACT_EVENTImportant dates such as birthday and anniversaries of the contact.
ATTR_EMAILEmail address of the contact.
ATTR_GROUP_MEMBERSHIPGroups of the contact.
ATTR_IMIM addresses of the contact.
ATTR_NAMEContact name.
ATTR_NICKNAMEContact nickname.
ATTR_NOTEContact notes.
ATTR_ORGANIZATIONOrganization of the contact.
ATTR_PHONEPhone number of the contacts.
ATTR_PORTRAITContact portrait.
ATTR_POSTAL_ADDRESSPostal address of the contact.
ATTR_RELATIONRelationship with the contact.
ATTR_SIP_ADDRESSSIP addresses of the contact.
ATTR_WEBSITEWebsite that stores the contact information.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let attributes = [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME, contact.Attribute.ATTR_PHONE];

Email

Defines a contact's email.

System capability: SystemCapability.Applications.ContactsData

Constant

NameValueDescription
CUSTOM_LABEL0Custom mailbox type.
EMAIL_HOME1Home mailbox.
EMAIL_WORK2Work mailbox.
EMAIL_OTHER3Other mailbox.
INVALID_LABEL_ID-1Invalid mailbox.

Attributes

NameTypeReadableWritableDescription
emailstringYesYesEmail addresses
labelNamestringYesYesName of the mailbox type.
displayNamestringYesYesDisplayed name of the mailbox.
labelIdnumberYesYesMailbox type.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let email = {
    email: "xxx@email.com",
    displayName: "displayName"
}

Or, create data by configuring an Email object.

// The sample code applies only to JS source files.
let email = new contact.Email();
email.email = "xxx@email.com";

Holder

Defines an application that creates the contact.

System capability: SystemCapability.Applications.ContactsData

NameTypeReadableWritableDescription
bundleNamestringYesNoBundle name.
displayNamestringYesNoApplication name.
holderIdnumberYesYesApplication ID.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let holder = {
  holderId: 0
};

Or, create data by configuring a Holder object.

// The sample code applies only to JS source files.
let holder = new contact.Holder();
holder.holderId = 0;

Event

Defines a contact's event.

System capability: SystemCapability.Applications.ContactsData

Constant

NameValueDescription
CUSTOM_LABEL0Custom event.
EVENT_ANNIVERSARY1Anniversary event.
EVENT_OTHER2Other event.
EVENT_BIRTHDAY3Birthday event.
INVALID_LABEL_ID-1Invalid event.

Attributes

NameTypeReadableWritableDescription
eventDatestringYesYesEvent date.
labelNamestringYesYesEvent type.
labelIdnumberYesYesEvent type ID.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let event = {
    eventDate: "xxxxxx"
};

Or, create data by configuring an Event object.

// The sample code applies only to JS source files.
let event = new contact.Event();
event.eventDate = "xxxxxx";

Group

Defines a contact group.

System capability: SystemCapability.Applications.ContactsData

NameTypeReadableWritableDescription
groupIdnumberYesYesID of a contact group.
titlestringYesYesName of a contact group.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let group = {
    groupId: 1,
    title: "title"
};

Or, create data by configuring a Group object.

// The sample code applies only to JS source files.
let group = new contact.Group();
group.title = "title";

ImAddress

Enumerates IM addresses.

System capability: SystemCapability.Applications.ContactsData

Constant

NameValueDescription
CUSTOM_LABEL-1Custom IM
IM_AIM0AIM
IM_MSN1MSN
IM_YAHOO2Yahoo
IM_SKYPE3Skype
IM_QQ4QQ
IM_ICQ6ICQ
IM_JABBER7JABBER
INVALID_LABEL_ID-2Invalid IM

Attributes

NameTypeReadableWritableDescription
imAddressstringYesYesIM address.
labelNamestringYesYesIM name.
labelIdnumberYesYesIM ID.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let imAddress = {
    imAddress: "imAddress",
    labelName: "labelName"
};

Or, create data by configuring an ImAddress object.

// The sample code applies only to JS source files.
let imAddress = new contact.ImAddress();
imAddress.imAddress = "imAddress";

Name

Defines a contact's name.

System capability: SystemCapability.Applications.ContactsData

NameTypeReadableWritableDescription
familyNamestringYesYesFamily name.
familyNamePhoneticstringYesYesFamily name in pinyin.
fullNamestringYesYesFull name of the contact.
givenNamestringYesYesGiven name of the contact.
givenNamePhoneticstringYesYesGiven name of the contact in pinyin.
middleNamestringYesYesMiddle name of the contact.
middleNamePhoneticstringYesYesMiddle name of the contact in pinyin.
namePrefixstringYesYesPrefix of the contact name.
nameSuffixstringYesYesSuffix of the contact name.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let name = {
    familyName: "familyName",
    fullName: "fullName"
};

Or, create data by configuring a Name object.

// The sample code applies only to JS source files.
let name = new contact.Name();
name.familyName = "familyName";
name.fullName = "fullName";

NickName

Defines a contact's nickname.

System capability: SystemCapability.Applications.ContactsData

NameTypeReadableWritableDescription
nickNamestringYesYesContact nickname.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let nickName = {
    nickName: "nickName"
};

Or, create data by configuring a NickName object.

// The sample code applies only to JS source files.
let nickName = new contact.NickName();
nickName.nickName = "nickName";

Note

Defines a contact's note.

System capability: SystemCapability.Applications.ContactsData

NameTypeReadableWritableDescription
noteContentstringYesYesNotes of the contact.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let note = {
    noteContent: "noteContent"
};

Or, create data by configuring a Note object.

// The sample code applies only to JS source files.
let note = new contact.Note();
note.noteContent = "noteContent";

Organization

Defines a contact's organization.

System capability: SystemCapability.Applications.ContactsData

NameTypeReadableWritableDescription
namestringYesYesOrganization name.
titlestringYesYesOrganization title.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let organization = {
    name: "name",
    title: "title"
};

Or, create data by configuring an Organization object.

// The sample code applies only to JS source files.
let organization = new contact.Organization();
organization.name = "name";
organization.title = "title";

PhoneNumber

Defines a contact's phone number.

System capability: SystemCapability.Applications.ContactsData

Constant

NameValueDescription
CUSTOM_LABEL0Custom phone type.
NUM_HOME1Home phone.
NUM_MOBILE2Mobile phone.
NUM_WORK3Work phone.
NUM_FAX_WORK4Work fax.
NUM_FAX_HOME5Family fax.
NUM_PAGER6Pager.
NUM_OTHER7Other phone type.
NUM_CALLBACK8Callback phone.
NUM_CAR9Car phone.
NUM_COMPANY_MAIN10Company phone.
NUM_ISDN11Integrated Services Digital Network (ISDN) phone.
NUM_MAIN12Main phone.
NUM_OTHER_FAX13Other fax phone.
NUM_RADIO14Wireless phone.
NUM_TELEX15Telex phone.
NUM_TTY_TDD16Teletypewriter (TTY) or Test Driven Development (TDD) phone.
NUM_WORK_MOBILE17Work mobile phone.
NUM_WORK_PAGER18Work pager.
NUM_ASSISTANT19Assistant phone.
NUM_MMS20MMS phone.
INVALID_LABEL_ID-1Invalid phone type.

Attributes

NameTypeReadableWritableDescription
labelNamestringYesYesPhone number type.
phoneNumberstringYesYesPhone number.
labelIdnumberYesYesPhone number ID.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let phoneNumber = {
    phoneNumber: "138xxxxxxxx",
    labelId: contact.PhoneNumber.NUM_HOME
};

Or, create data by configuring a new PhoneNumber object.

// The sample code applies only to JS source files.
let phoneNumber = new contact.PhoneNumber();
phoneNumber.phoneNumber = "138xxxxxxxx";

Portrait

Defines a contact's portrait.

System capability: SystemCapability.Applications.ContactsData

NameTypeReadableWritableDescription
uristringYesYesContact portrait.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let portrait = {
    uri: "uri"
};

Or, create data by configuring a new Portrait object.

// The sample code applies only to JS source files.
let portrait = new contact.Portrait();
portrait.uri = "uri";

PostalAddress

Defines a contact's postal address.

System capability: SystemCapability.Applications.ContactsData

Constant

NameValueDescription
CUSTOM_LABEL0Custom postal address type.
ADDR_HOME1Home address.
ADDR_WORK2Work address.
ADDR_OTHER3Other addresses.
INVALID_LABEL_ID-1Invalid address type.

Attributes

NameTypeReadableWritableDescription
citystringYesYesCity where the contact is located.
countrystringYesYesCountry/Region where the contact is located.
labelNamestringYesYesPostal address type.
neighborhoodstringYesYesNeighbor of the contact.
poboxstringYesYesEmail of the contact.
postalAddressstringYesYesPostal address of the contact.
postcodestringYesYesPostal code of the region where the contact is located.
regionstringYesYesArea where the contact is located.
streetstringYesYesStreet where the contact resides.
labelIdnumberYesYesPostal address type.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let postalAddress = {
    city: "city"
};

Or, create data by configuring a new PostalAddress object.

// The sample code applies only to JS source files.
let postalAddress = new contact.PostalAddress();
postalAddress.city = "city";

Relation

Defines a contact's relationship.

System capability: SystemCapability.Applications.ContactsData

Constant

NameValueDescription
CUSTOM_LABEL0Custom relationship.
RELATION_ASSISTANT1Assistant.
RELATION_BROTHER2Sibling.
RELATION_CHILD3Child.
RELATION_DOMESTIC_PARTNER4Domestic partner.
RELATION_FATHER5Father.
RELATION_FRIEND6Friend.
RELATION_MANAGER7Manager.
RELATION_MOTHER8Mother.
RELATION_PARENT9Parent.
RELATION_PARTNER10Partner.
RELATION_REFERRED_BY11Referrer.
RELATION_RELATIVE12Relative.
RELATION_SISTER13Sister.
RELATION_SPOUSE14Spouse.
INVALID_LABEL_ID-1Invalid relationship.

Attributes

NameTypeReadableWritableDescription
labelNamestringYesYesRelationship type.
relationNamestringYesYesRelationship name.
labelIdnumberYesYesRelationship ID.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let relation = {
    relationName: "relationName",
    labelId: contact.Relation.RELATION_ASSISTANT
};

Or, create data by configuring a new Relation object.

// The sample code applies only to JS source files.
let relation = new contact.Relation();
relation.relationName = "relationName";
relation.labelId = contact.Relation.RELATION_ASSISTANT;

SipAddress

Defines a contact's SIP address.

System capability: SystemCapability.Applications.ContactsData

Constant

NameValueDescription
CUSTOM_LABEL0Custom SIP address.
SIP_HOME1Home SIP address.
SIP_WORK2Work SIP address.
SIP_OTHER3Other SIP address.
INVALID_LABEL_ID-1Invalid SIP address.

Attributes

NameTypeReadableWritableDescription
labelNamestringYesYesSIP address type.
sipAddressstringYesYesSIP address.
labelIdnumberYesYesSIP address ID.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
var sipAddress = {
    sipAddress: "sipAddress"
};

Or, create data by configuring a new SipAddress object.

// The sample code applies only to JS source files.
let sipAddress = new contact.SipAddress();
sipAddress.sipAddress = "sipAddress";

Website

Defines a contact's website.

System capability: SystemCapability.Applications.ContactsData

NameTypeReadableWritableDescription
websitestringYesYesWebsite of the contact.

Example

Create contact data in JSON format:

// The sample code applies only to JS source files.
let website = {
    website: "website"
};

Or, create data by configuring a new Website object.

// The sample code applies only to JS source files.
let website = new contact.Website();
website.website = "website";

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/608645a3ecf14c83a151e30666b20265