1# @ohos.contact (Contacts) 2 3>**NOTE** 4> 5>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. 6 7 8## Modules to Import 9 10```js 11import contact from '@ohos.contact'; 12``` 13 14## contact.addContact 15 16addContact(contact:Contact, callback:AsyncCallback<number>): void 17 18Adds a contact. This API uses an asynchronous callback to return the result. 19 20**Permission required**: ohos.permission.WRITE_CONTACTS 21 22**System capability**: SystemCapability.Applications.ContactsData 23 24**Parameters** 25 26| Name | Type | Mandatory| Description | 27| -------- | --------------------------- | ---- | ------------------------------ | 28| contact | [Contact](#contact) | Yes | Contact information. | 29| callback | AsyncCallback<number> | Yes | Callback used to return the contact ID.| 30 31**Example** 32 33 ```js 34 contact.addContact({ 35 name: {fullName: 'xxx'}, 36 phoneNumbers: [{phoneNumber: '138xxxxxxxx'}] 37 }, (err, data) => { 38 if (err) { 39 console.log(`addContact callback: err->${JSON.stringify(err)}`); 40 return; 41 } 42 console.log(`addContact callback: success data->${JSON.stringify(data)}`); 43 }); 44 ``` 45 46 47## contact.addContact 48 49addContact(contact: Contact): Promise<number> 50 51Adds a contact. This API uses a promise to return the result. 52 53**Permission required**: ohos.permission.WRITE_CONTACTS 54 55**System capability**: SystemCapability.Applications.ContactsData 56 57**Parameters** 58 59| Name | Type | Mandatory| Description | 60| ------- | ------------------- | ---- | ------------ | 61| contact | [Contact](#contact) | Yes | Contact information.| 62 63**Return Value** 64 65| Type | Description | 66| --------------------- | ------------------------------------------- | 67| Promise<number> | Promise used to return the contact ID.| 68 69**Example** 70 71 ```js 72 let promise = contact.addContact({ 73 name: {fullName: 'xxx'}, 74 phoneNumbers: [{phoneNumber: '138xxxxxxxx'}] 75 }); 76 promise.then((data) => { 77 console.log(`addContact success: data->${JSON.stringify(data)}`); 78 }).catch((err) => { 79 console.error(`addContact fail: err->${JSON.stringify(err)}`); 80 }); 81 ``` 82 83 84## contact.deleteContact 85 86deleteContact(key: string, callback: AsyncCallback<void>): void 87 88Deletes a contact based on the specified contact key. This API uses an asynchronous callback to return the result. 89 90**Permission required**: ohos.permission.WRITE_CONTACTS 91 92**System capability**: SystemCapability.Applications.ContactsData 93 94**Parameters** 95 96| Name | Type | Mandatory| Description | 97| -------- | ------------------------- | ---- | ------------------------------------ | 98| key | string | Yes | Contact key. Each contact corresponds to one key.| 99| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 100 101**Example** 102 103 ```js 104 contact.deleteContact('xxx', (err) => { 105 if (err) { 106 console.log(`deleteContact callback: err->${JSON.stringify(err)}`); 107 return; 108 } 109 console.log('deleteContact success'); 110 }); 111 ``` 112 113 114## contact.deleteContact 115 116deleteContact(key: string): Promise<void> 117 118Deletes a contact based on the specified contact key. This API uses a promise to return the result. 119 120**Permission required**: ohos.permission.WRITE_CONTACTS 121 122**System capability**: SystemCapability.Applications.ContactsData 123 124**Parameters** 125 126| Name| Type | Mandatory| Description | 127| ------ | ------ | ---- | -------------------------------------- | 128| key | string | Yes | Contact key. Each contact corresponds to one key.| 129 130**Return Value** 131 132| Type | Description | 133| ------------------- | --------------------------------------------- | 134| Promise<void> | Promise used to return the result.| 135 136**Example** 137 138 ```js 139 let promise = contact.deleteContact('xxx'); 140 promise.then(() => { 141 console.log(`deleteContact success`); 142 }).catch((err) => { 143 console.error(`deleteContact fail: err->${JSON.stringify(err)}`); 144 }); 145 ``` 146 147 148## contact.updateContact 149 150updateContact(contact: Contact, callback: AsyncCallback<void>): void 151 152Updates a contact based on the specified contact information. This API uses an asynchronous callback to return the result. 153 154**Permission required**: ohos.permission.WRITE_CONTACTS 155 156**System capability**: SystemCapability.Applications.ContactsData 157 158**Parameters** 159 160| Name | Type | Mandatory| Description | 161| -------- | ------------------------- | ---- | ------------------------------------ | 162| contact | [Contact](#contact) | Yes | Contact information. | 163| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 164 165**Example** 166 167 ```js 168 contact.updateContact({ 169 id: 1, 170 name: {fullName: 'xxx'}, 171 phoneNumbers: [{phoneNumber: '138xxxxxxxx'}] 172 }, (err) => { 173 if (err) { 174 console.log('updateContact callback: err->${JSON.stringify(err)}'); 175 return; 176 } 177 console.log('updateContact success'); 178 }); 179 ``` 180 181 182## contact.updateContact 183 184updateContact(contact: Contact, attrs: ContactAttributes, callback: AsyncCallback<void>): void 185 186Updates a contact based on the specified contact information and attributes. This API uses an asynchronous callback to return the result. 187 188**Permission required**: ohos.permission.WRITE_CONTACTS 189 190**System capability**: SystemCapability.Applications.ContactsData 191 192**Parameters** 193 194| Name | Type | Mandatory| Description | 195| -------- | --------------------------------------- | ---- | ------------------------------------ | 196| contact | [Contact](#contact) | Yes | Contact information. | 197| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 198| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 199 200**Example** 201 202 ```js 203 contact.updateContact({ 204 id: 1, 205 name: {fullName: 'xxx'}, 206 phoneNumbers: [{phoneNumber: '138xxxxxxxx'}] 207 }, { 208 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 209 }, (err) => { 210 if (err) { 211 console.log('updateContact callback: err->${JSON.stringify(err)}'); 212 return; 213 } 214 console.log('updateContact success'); 215 }); 216 ``` 217 218 219## contact.updateContact 220 221updateContact(contact: Contact, attrs?: ContactAttributes): Promise<void> 222 223Updates a contact based on the specified contact information and attributes. This API uses a promise to return the result. 224 225**Permission required**: ohos.permission.WRITE_CONTACTS 226 227**System capability**: SystemCapability.Applications.ContactsData 228 229**Parameters** 230 231| Name | Type | Mandatory| Description | 232| ------- | --------------------------------------- | ---- | ------------------ | 233| contact | [Contact](#contact) | Yes | Contact information. | 234| attrs | [ContactAttributes](#contactattributes) | No | List of contact attributes.| 235 236**Return Value** 237 238| Type | Description | 239| ------------------- | ------------------------------------------------- | 240| Promise<void> | Promise used to return the result.| 241 242**Example** 243 244 ```js 245 let promise = contact.updateContact({ 246 id: 1, 247 name: {fullName: 'xxx'}, 248 phoneNumbers: [{phoneNumber: '138xxxxxxxx'}] 249 }, { 250 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 251 }); 252 promise.then(() => { 253 console.log('updateContact success'); 254 }).catch((err) => { 255 console.error(`updateContact fail: err->${JSON.stringify(err)}`); 256 }); 257 ``` 258 259 260## contact.isLocalContact 261 262isLocalContact(id: number, callback: AsyncCallback<boolean>): void 263 264Checks whether the ID of this contact is in the local address book. This API uses an asynchronous callback to return the result. 265 266**Permission required**: ohos.permission.READ_CONTACTS 267 268**System capability**: SystemCapability.Applications.ContactsData 269 270**Parameters** 271 272| Name | Type | Mandatory| Description | 273| -------- | ---------------------------- | ---- | ------------------------------------------------------------ | 274| id | number | Yes | Contact ID. Each contact corresponds to one ID. | 275| callback | AsyncCallback<boolean> | Yes | Callback used to return a boolean value. The value **true** indicates that the contact ID is in the local address book, and the value **false** indicates the opposite.| 276 277**Example** 278 279 ```js 280 contact.isLocalContact(/*id*/1, (err, data) => { 281 if (err) { 282 console.log(`isLocalContact callback: err->${JSON.stringify(err)}`); 283 return; 284 } 285 console.log(`isLocalContact callback: success data->${JSON.stringify(data)}`); 286 }); 287 ``` 288 289 290## contact.isLocalContact 291 292isLocalContact(id: number): Promise<boolean> 293 294Checks whether the ID of this contact is in the local address book. This API uses a promise to return the result. 295 296**Permission required**: ohos.permission.READ_CONTACTS 297 298**System capability**: SystemCapability.Applications.ContactsData 299 300**Parameters** 301 302| Name| Type | Mandatory| Description | 303| ------ | ------ | ---- | ------------------------------------------ | 304| id | number | Yes | Contact ID. Each contact corresponds to one ID.| 305 306**Return Value** 307 308| Type | Description | 309| ---------------------- | ------------------------------------------------------------ | 310| 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.| 311 312**Example** 313 314 ```js 315 let promise = contact.isLocalContact(/*id*/1); 316 promise.then((data) => { 317 console.log(`isLocalContact success: data->${JSON.stringify(data)}`); 318 }).catch((err) => { 319 console.error(`isLocalContact fail: err->${JSON.stringify(err)}`); 320 }); 321 ``` 322 323 324## contact.isMyCard 325 326isMyCard(id: number, callback: AsyncCallback<boolean>): void 327 328Checks whether a contact is included in my card. This API uses an asynchronous callback to return the result. 329 330**Permission required**: ohos.permission.READ_CONTACTS 331 332**System capability**: SystemCapability.Applications.ContactsData 333 334**Parameters** 335 336| Name | Type | Mandatory| Description | 337| -------- | ---------------------------- | ---- | ------------------------------------------------------------ | 338| id | number | Yes | Contact ID. | 339| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. The value **true** indicates that the contact is included in my card, and the value **false** indicates the opposite.| 340 341**Example** 342 343 ```js 344 contact.isMyCard(/*id*/1, (err, data) => { 345 if (err) { 346 console.log(`isMyCard callback: err->${JSON.stringify(err)}`); 347 return; 348 } 349 console.log(`isMyCard callback: success data->${JSON.stringify(data)}`); 350 }); 351 ``` 352 353 354## contact.isMyCard 355 356isMyCard(id: number): Promise<boolean> 357 358Checks whether a contact is included in my card. This API uses a promise to return the result. 359 360**Permission required**: ohos.permission.READ_CONTACTS 361 362**System capability**: SystemCapability.Applications.ContactsData 363 364**Parameters** 365 366| Name| Type | Mandatory| Description | 367| ------ | ------ | ---- | -------------------- | 368| id | number | Yes | Contact ID.| 369 370**Return Value** 371 372| Type | Description | 373| ---------------------- | ------------------------------------------------------------ | 374| 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.| 375 376**Example** 377 378 ```js 379 let promise = contact.isMyCard(/*id*/1); 380 promise.then((data) => { 381 console.log(`isMyCard success: data->${JSON.stringify(data)}`); 382 }).catch((err) => { 383 console.error(`isMyCard fail: err->${JSON.stringify(err)}`); 384 }); 385 ``` 386 387 388## contact.queryMyCard 389 390queryMyCard(callback: AsyncCallback<Contact>): void 391 392Queries my card. This API uses an asynchronous callback to return the result. 393 394**Permission required**: ohos.permission.READ_CONTACTS 395 396**System capability**: SystemCapability.Applications.ContactsData 397 398**Parameters** 399 400| Name | Type | Mandatory| Description | 401| -------- | ---------------------------------------- | ---- | ------------------------------ | 402| callback | AsyncCallback<[Contact](#contact)> | Yes | Callback used to return the result.| 403 404**Example** 405 406 ```js 407 contact.queryMyCard((err, data) => { 408 if (err) { 409 console.log(`queryMyCard callback: err->${JSON.stringify(err)}`); 410 return; 411 } 412 console.log(`queryMyCard callback: success data->${JSON.stringify(data)}`); 413 }); 414 ``` 415 416 417## contact.queryMyCard 418 419queryMyCard(attrs: ContactAttributes, callback: AsyncCallback<Contact>): void 420 421Queries my card based on the specified contact attributes. This API uses an asynchronous callback to return the result. 422 423**Permission required**: ohos.permission.READ_CONTACTS 424 425**System capability**: SystemCapability.Applications.ContactsData 426 427**Parameters** 428 429| Name | Type | Mandatory| Description | 430| -------- | ---------------------------------------- | ---- | ------------------------------ | 431| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 432| callback | AsyncCallback<[Contact](#contact)> | Yes | Callback used to return the result.| 433 434**Example** 435 436 ```js 437 contact.queryMyCard({ 438 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 439 }, (err, data) => { 440 if (err) { 441 console.log(`queryMyCard callback: err->${JSON.stringify(err)}`); 442 return; 443 } 444 console.log(`queryMyCard callback: success data->${JSON.stringify(data)}`); 445 }); 446 ``` 447 448 449## contact.queryMyCard 450 451queryMyCard(attrs?: ContactAttributes): Promise<Contact> 452 453Queries my card based on the specified contact attributes. This API uses a promise to return the result. 454 455**Permission required**: ohos.permission.READ_CONTACTS 456 457**System capability**: SystemCapability.Applications.ContactsData 458 459**Parameters** 460 461| Name| Type | Mandatory| Description | 462| ------ | --------------------------------------- | ---- | ------------------ | 463| attrs | [ContactAttributes](#contactattributes) | No | List of contact attributes.| 464 465**Return Value** 466 467| Type | Description | 468| ---------------------------------- | ------------------------------------------- | 469| Promise<[Contact](#contact)> | Promise used to return the result.| 470 471**Example** 472 473 ```js 474 let promise = contact.queryMyCard({ 475 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 476 }); 477 promise.then((data) => { 478 console.log(`queryMyCard success: data->${JSON.stringify(data)}`); 479 }).catch((err) => { 480 console.error(`queryMyCard fail: err->${JSON.stringify(err)}`); 481 }); 482 ``` 483 484 485## contact.selectContact 486 487selectContact(callback: AsyncCallback<Array<Contact>>): void 488 489Selects a contact. This API uses an asynchronous callback to return the result. 490 491**System capability**: SystemCapability.Applications.Contacts 492 493**Parameters** 494 495| Name | Type | Mandatory| Description | 496| -------- | ----------------------------------------------------- | ---- | ------------------------------------ | 497| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 498 499**Example** 500 501 ```js 502 contact.selectContact((err, data) => { 503 if (err) { 504 console.log(`selectContact callback: err->${JSON.stringify(err)}`); 505 return; 506 } 507 console.log(`selectContact callback: success data->${JSON.stringify(data)}`); 508 }); 509 ``` 510 511 512## contact.selectContact 513 514selectContact(): Promise<Array<Contact>> 515 516Selects a contact. This API uses a promise to return the result. 517 518**System capability**: SystemCapability.Applications.Contacts 519 520**Return Value** 521 522| Type | Description | 523| ----------------------------------------------- | ------------------------------------------------- | 524| Promise<Array<[Contact](#contact)>> | Promise used to return the result.| 525 526**Example** 527 528 ```js 529 let promise = contact.selectContact(); 530 promise.then((data) => { 531 console.log(`selectContact success: data->${JSON.stringify(data)}`); 532 }).catch((err) => { 533 console.error(`selectContact fail: err->${JSON.stringify(err)}`); 534 }); 535 ``` 536 537 538## contact.queryContact 539 540queryContact(key: string, callback: AsyncCallback<Contact>): void 541 542Queries a contact based on the specified key. This API uses an asynchronous callback to return the result. 543 544**Permission required**: ohos.permission.READ_CONTACTS 545 546**System capability**: SystemCapability.Applications.ContactsData 547 548**Parameters** 549 550| Name | Type | Mandatory| Description | 551| -------- | ---------------------------------------- | ---- | -------------------------------------- | 552| key | string | Yes | Contact key. Each contact corresponds to one key.| 553| callback | AsyncCallback<[Contact](#contact)> | Yes | Callback used to return the result. | 554 555**Example** 556 557 ```js 558 contact.queryContact('xxx', (err, data) => { 559 if (err) { 560 console.log(`queryContact callback: err->${JSON.stringify(err)}`); 561 return; 562 } 563 console.log(`queryContact callback: success data->${JSON.stringify(data)}`); 564 }); 565 ``` 566 567 568## contact.queryContact 569 570queryContact(key: string, holder: Holder, callback: AsyncCallback<Contact>): void 571 572Queries contacts based on the specified key and application. This API uses an asynchronous callback to return the result. 573 574**Permission required**: ohos.permission.READ_CONTACTS 575 576**System capability**: SystemCapability.Applications.ContactsData 577 578**Parameters** 579 580| Name | Type | Mandatory| Description | 581| -------- | ---------------------------------------- | ---- | -------------------------------------- | 582| key | string | Yes | Contact key. Each contact corresponds to one key.| 583| holder | [Holder](#holder) | Yes | Application that creates the contacts. | 584| callback | AsyncCallback<[Contact](#contact)> | Yes | Callback used to return the result. | 585 586**Example** 587 588 ```js 589 contact.queryContact('xxx', { 590 holderId: 0, 591 bundleName: "", 592 displayName: "" 593 }, (err, data) => { 594 if (err) { 595 console.log(`queryContact callback: err->${JSON.stringify(err)}`); 596 return; 597 } 598 console.log(`queryContact callback: success data->${JSON.stringify(data)}`); 599 }); 600 ``` 601 602 603## contact.queryContact 604 605queryContact(key: string, attrs: ContactAttributes, callback: AsyncCallback<Contact>): void 606 607Queries contacts based on the specified key and attributes. This API uses an asynchronous callback to return the result. 608 609**Permission required**: ohos.permission.READ_CONTACTS 610 611**System capability**: SystemCapability.Applications.ContactsData 612 613**Parameters** 614 615| Name | Type | Mandatory| Description | 616| -------- | ---------------------------------------- | ---- | -------------------------------------- | 617| key | string | Yes | Contact key. Each contact corresponds to one key.| 618| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 619| callback | AsyncCallback<[Contact](#contact)> | Yes | Callback used to return the result. | 620 621**Example** 622 623 ```js 624 contact.queryContact('xxx', { 625 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 626 }, (err, data) => { 627 if (err) { 628 console.log(`queryContact callback: err->${JSON.stringify(err)}`); 629 return; 630 } 631 console.log(`queryContact callback: success data->${JSON.stringify(data)}`); 632 }); 633 ``` 634 635 636## contact.queryContact 637 638queryContact(key: string, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Contact>): void 639 640Queries contacts based on the specified key, application, and attributes. This API uses an asynchronous callback to return the result. 641 642**Permission required**: ohos.permission.READ_CONTACTS 643 644**System capability**: SystemCapability.Applications.ContactsData 645 646**Parameters** 647 648| Name | Type | Mandatory| Description | 649| -------- | ---------------------------------------- | ---- | -------------------------------------- | 650| key | string | Yes | Contact key. Each contact corresponds to one key.| 651| holder | [Holder](#holder) | Yes | Application that creates the contacts. | 652| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 653| callback | AsyncCallback<[Contact](#contact)> | Yes | Callback used to return the result. | 654 655**Example** 656 657 ```js 658 contact.queryContact('xxx', { 659 holderId: 0 660 holderId: 0, 661 bundleName: "", 662 displayName: "" 663 }, { 664 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 665 }, (err, data) => { 666 if (err) { 667 console.log(`queryContact callback: err->${JSON.stringify(err)}`); 668 return; 669 } 670 console.log(`queryContact callback: success data->${JSON.stringify(data)}`); 671 }); 672 ``` 673 674 675## contact.queryContact 676 677queryContact(key: string, holder?: Holder, attrs?: ContactAttributes): Promise<Contact> 678 679Queries contacts based on the specified key, application, and attributes. This API uses a promise to return the result. 680 681**Permission required**: ohos.permission.READ_CONTACTS 682 683**System capability**: SystemCapability.Applications.ContactsData 684 685**Parameters** 686 687| Name| Type | Mandatory| Description | 688| ------ | --------------------------------------- | ---- | -------------------------------------- | 689| key | string | Yes | Contact key. Each contact corresponds to one key.| 690| holder | [Holder](#holder) | No | Application that creates the contacts. | 691| attrs | [ContactAttributes](#contactattributes) | No | List of contact attributes. | 692 693**Return Value** 694 695| Type | Description | 696| ---------------------------------- | ----------------------------------------------- | 697| Promise<[Contact](#contact)> | Promise used to return the result.| 698 699**Example** 700 701 ```js 702 let promise = contact.queryContact('xxx', { 703 holderId: 0 704 holderId: 0, 705 bundleName: "", 706 displayName: "" 707 }, { 708 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 709 }); 710 promise.then((data) => { 711 console.log(`queryContact success: data->${JSON.stringify(data)}`); 712 }).catch((err) => { 713 console.error(`queryContact fail: err->${JSON.stringify(err)}`); 714 }); 715 ``` 716 717 718## contact.queryContacts 719 720queryContacts(callback: AsyncCallback<Array<Contact>>): void 721 722Queries all contacts. This API uses an asynchronous callback to return the result. 723 724**Permission required**: ohos.permission.READ_CONTACTS 725 726**System capability**: SystemCapability.Applications.ContactsData 727 728**Parameters** 729 730| Name | Type | Mandatory| Description | 731| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 732| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 733 734**Example** 735 736 ```js 737 contact.queryContacts((err, data) => { 738 if (err) { 739 console.log(`queryContacts callback: err->${JSON.stringify(err)}`); 740 return; 741 } 742 console.log(`queryContacts callback: success data->${JSON.stringify(data)}`); 743 }); 744 ``` 745 746 747## contact.queryContacts 748 749queryContacts(holder: Holder, callback: AsyncCallback<Array<Contact>>): void 750 751Queries all contacts based on the specified application. This API uses an asynchronous callback to return the result. 752 753**Permission required**: ohos.permission.READ_CONTACTS 754 755**System capability**: SystemCapability.Applications.ContactsData 756 757**Parameters** 758 759| Name | Type | Mandatory| Description | 760| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 761| holder | [Holder](#holder) | Yes | Application that creates the contacts. | 762| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 763 764**Example** 765 766 ```js 767 contact.queryContacts({ 768 holderId: 0, 769 bundleName: "", 770 displayName: "" 771 }, (err, data) => { 772 if (err) { 773 console.log(`queryContacts callback: err->${JSON.stringify(err)}`); 774 return; 775 } 776 console.log(`queryContacts callback: success data->${JSON.stringify(data)}`); 777 }); 778 ``` 779 780 781## contact.queryContacts 782 783queryContacts(attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void 784 785Queries all contacts based on the specified attributes. This API uses an asynchronous callback to return the result. 786 787**Permission required**: ohos.permission.READ_CONTACTS 788 789**System capability**: SystemCapability.Applications.ContactsData 790 791**Parameters** 792 793| Name | Type | Mandatory| Description | 794| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 795| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 796| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 797 798**Example** 799 800 ```js 801 contact.queryContacts({ 802 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 803 }, (err, data) => { 804 if (err) { 805 console.log(`queryContacts callback: err->${JSON.stringify(err)}`); 806 return; 807 } 808 console.log(`queryContacts callback: success data->${JSON.stringify(data)}`); 809 }); 810 ``` 811 812 813## contact.queryContacts 814 815queryContacts(holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void 816 817Queries all contacts based on the specified application and attributes. This API uses an asynchronous callback to return the result. 818 819**Permission required**: ohos.permission.READ_CONTACTS 820 821**System capability**: SystemCapability.Applications.ContactsData 822 823**Parameters** 824 825| Name | Type | Mandatory| Description | 826| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 827| holder | [Holder](#holder) | Yes | Application that creates the contacts. | 828| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 829| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 830 831**Example** 832 833 ```js 834 contact.queryContacts({ 835 holderId: 0, 836 bundleName: "", 837 displayName: "" 838 }, { 839 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 840 }, (err, data) => { 841 if (err) { 842 console.log(`queryContacts callback: err->${JSON.stringify(err)}`); 843 return; 844 } 845 console.log(`queryContacts callback: success data->${JSON.stringify(data)}`); 846 }); 847 ``` 848 849 850## contact.queryContacts 851 852queryContacts(holder?: Holder, attrs?: ContactAttributes): Promise<Array<Contact>> 853 854Queries all contacts based on the specified application and attributes. This API uses a promise to return the result. 855 856**Permission required**: ohos.permission.READ_CONTACTS 857 858**System capability**: SystemCapability.Applications.ContactsData 859 860**Parameters** 861 862| Name| Type | Mandatory| Description | 863| ------ | --------------------------------------- | ---- | ---------------------- | 864| holder | [Holder](#holder) | No | Application that creates the contacts.| 865| attrs | [ContactAttributes](#contactattributes) | No | List of contact attributes. | 866 867**Return Value** 868 869| Type | Description | 870| ----------------------------------------------- | --------------------------------------------------- | 871| Promise<Array<[Contact](#contact)>> | Promise used to return the result.| 872 873**Example** 874 875 ```js 876 let promise = contact.queryContacts({ 877 holderId: 0, 878 bundleName: "", 879 displayName: "" 880 }, { 881 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 882 }); 883 promise.then((data) => { 884 console.log(`queryContacts success: data->${JSON.stringify(data)}`); 885 }).catch((err) => { 886 console.error(`queryContacts fail: err->${JSON.stringify(err)}`); 887 }); 888 ``` 889 890 891## contact.queryContactsByPhoneNumber 892 893queryContactsByPhoneNumber(phoneNumber: string, callback: AsyncCallback<Array<Contact>>): void 894 895Queries contacts based on the specified phone number. This API uses an asynchronous callback to return the result. 896 897**Permission required**: ohos.permission.READ_CONTACTS 898 899**System capability**: SystemCapability.Applications.ContactsData 900 901**Parameters** 902 903| Name | Type | Mandatory| Description | 904| ----------- | ----------------------------------------------------- | ---- | -------------------------------------- | 905| phoneNumber | string | Yes | Phone number of the contacts. | 906| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 907 908**Example** 909 910 ```js 911 contact.queryContactsByPhoneNumber('138xxxxxxxx', (err, data) => { 912 if (err) { 913 console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`); 914 return; 915 } 916 console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`); 917 }); 918 ``` 919 920 921## contact.queryContactsByPhoneNumber 922 923queryContactsByPhoneNumber(phoneNumber: string, holder: Holder, callback: AsyncCallback<Array<Contact>>): void 924 925Queries contacts based on the specified phone number and application. This API uses an asynchronous callback to return the result. 926 927**Permission required**: ohos.permission.READ_CONTACTS 928 929**System capability**: SystemCapability.Applications.ContactsData 930 931**Parameters** 932 933| Name | Type | Mandatory| Description | 934| ----------- | ----------------------------------------------------- | ---- | -------------------------------------- | 935| phoneNumber | string | Yes | Phone number of the contacts. | 936| holder | [Holder](#holder) | Yes | Application that creates the contacts. | 937| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 938 939**Example** 940 941 ```js 942 contact.queryContactsByPhoneNumber('138xxxxxxxx', { 943 holderId: 0, 944 bundleName: "", 945 displayName: "" 946 }, (err, data) => { 947 if (err) { 948 console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`); 949 return; 950 } 951 console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`); 952 }); 953 ``` 954 955 956## contact.queryContactsByPhoneNumber 957 958queryContactsByPhoneNumber(phoneNumber: string, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void 959 960Queries contacts based on the specified phone number and attributes. This API uses an asynchronous callback to return the result. 961 962**Permission required**: ohos.permission.READ_CONTACTS 963 964**System capability**: SystemCapability.Applications.ContactsData 965 966**Parameters** 967 968| Name | Type | Mandatory| Description | 969| ----------- | ----------------------------------------------------- | ---- | -------------------------------------- | 970| phoneNumber | string | Yes | Phone number of the contacts. | 971| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 972| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 973 974**Example** 975 976 ```js 977 contact.queryContactsByPhoneNumber('138xxxxxxxx', { 978 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 979 }, (err, data) => { 980 if (err) { 981 console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`); 982 return; 983 } 984 console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`); 985 }); 986 ``` 987 988 989## contact.queryContactsByPhoneNumber 990 991queryContactsByPhoneNumber(phoneNumber: string, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void 992 993Queries contacts based on the specified phone number, application, and attributes. This API uses an asynchronous callback to return the result. 994 995**Permission required**: ohos.permission.READ_CONTACTS 996 997**System capability**: SystemCapability.Applications.ContactsData 998 999**Parameters** 1000 1001| Name | Type | Mandatory| Description | 1002| ----------- | ----------------------------------------------------- | ---- | -------------------------------------- | 1003| phoneNumber | string | Yes | Phone number of the contacts. | 1004| holder | [Holder](#holder) | Yes | Application that creates the contacts. | 1005| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 1006| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 1007 1008**Example** 1009 1010 ```js 1011 contact.queryContactsByPhoneNumber('138xxxxxxxx', { 1012 holderId: 0, 1013 bundleName: "", 1014 displayName: "" 1015 }, { 1016 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 1017 }, (err, data) => { 1018 if (err) { 1019 console.log(`queryContactsByPhoneNumber callback: err->${JSON.stringify(err)}`); 1020 return; 1021 } 1022 console.log(`queryContactsByPhoneNumber callback: success data->${JSON.stringify(data)}`); 1023 }); 1024 ``` 1025 1026 1027## contact.queryContactsByPhoneNumber 1028 1029queryContactsByPhoneNumber(phoneNumber: string, holder?: Holder, attrs?: ContactAttributes): Promise<Array<Contact>> 1030 1031Queries contacts based on the specified phone number, application, and attributes. This API uses a promise to return the result. 1032 1033**Permission required**: ohos.permission.READ_CONTACTS 1034 1035**System capability**: SystemCapability.Applications.ContactsData 1036 1037**Parameters** 1038 1039| Name | Type | Mandatory| Description | 1040| ----------- | --------------------------------------- | ---- | ---------------------- | 1041| phoneNumber | string | Yes | Phone number of the contacts. | 1042| holder | [Holder](#holder) | No | Application that creates the contacts.| 1043| attrs | [ContactAttributes](#contactattributes) | No | List of contact attributes. | 1044 1045**Return Value** 1046 1047| Type | Description | 1048| ----------------------------------------------- | --------------------------------------------------- | 1049| Promise<Array<[Contact](#contact)>> | Promise used to return the result.| 1050 1051**Example** 1052 1053 ```js 1054 let promise = contact.queryContactsByPhoneNumber('138xxxxxxxx', { 1055 holderId: 0, 1056 bundleName: "", 1057 displayName: "" 1058 }, { 1059 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 1060 }); 1061 promise.then((data) => { 1062 console.log(`queryContactsByPhoneNumber success: data->${JSON.stringify(data)}`); 1063 }).catch((err) => { 1064 console.error(`queryContactsByPhoneNumber fail: err->${JSON.stringify(err)}`); 1065 }); 1066 ``` 1067 1068 1069## contact.queryContactsByEmail 1070 1071queryContactsByEmail(email: string, callback: AsyncCallback<Array<Contact>>): void 1072 1073Queries contacts based on the specified email address. This API uses an asynchronous callback to return the result. 1074 1075**Permission required**: ohos.permission.READ_CONTACTS 1076 1077**System capability**: SystemCapability.Applications.ContactsData 1078 1079**Parameters** 1080 1081| Name | Type | Mandatory| Description | 1082| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 1083| email | string | Yes | Email address of the contact. | 1084| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 1085 1086**Example** 1087 1088 ```js 1089 contact.queryContactsByEmail('xxx@email.com', (err, data) => { 1090 if (err) { 1091 console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`); 1092 return; 1093 } 1094 console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`); 1095 }); 1096 ``` 1097 1098 1099## contact.queryContactsByEmail 1100 1101queryContactsByEmail(email: string, holder: Holder, callback: AsyncCallback<Array<Contact>>): void 1102 1103Queries contacts based on the specified email address and application. This API uses an asynchronous callback to return the result. 1104 1105**Permission required**: ohos.permission.READ_CONTACTS 1106 1107**System capability**: SystemCapability.Applications.ContactsData 1108 1109**Parameters** 1110 1111| Name | Type | Mandatory| Description | 1112| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 1113| email | string | Yes | Email address of the contact. | 1114| holder | [Holder](#holder) | Yes | Application that creates the contacts. | 1115| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 1116 1117**Example** 1118 1119 ```js 1120 contact.queryContactsByEmail('xxx@email.com', { 1121 holderId: 0, 1122 bundleName: "", 1123 displayName: "" 1124 }, (err, data) => { 1125 if (err) { 1126 console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`); 1127 return; 1128 } 1129 console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`); 1130 }); 1131 ``` 1132 1133 1134## contact.queryContactsByEmail 1135 1136queryContactsByEmail(email: string, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void 1137 1138Queries contacts based on the specified email address and attributes. This API uses an asynchronous callback to return the result. 1139 1140**Permission required**: ohos.permission.READ_CONTACTS 1141 1142**System capability**: SystemCapability.Applications.ContactsData 1143 1144**Parameters** 1145 1146| Name | Type | Mandatory| Description | 1147| -------- | ----------------------------------------------------- | ---- | ------------------------------------ | 1148| email | string | Yes | Email address of the contact. | 1149| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 1150| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 1151 1152**Example** 1153 1154 ```js 1155 contact.queryContactsByEmail('xxx@email.com', { 1156 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 1157 }, (err, data) => { 1158 if (err) { 1159 console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`); 1160 return; 1161 } 1162 console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`); 1163 }); 1164 ``` 1165 1166 1167## contact.queryContactsByEmail 1168 1169queryContactsByEmail(email: string, holder: Holder, attrs: ContactAttributes, callback: AsyncCallback<Array<Contact>>): void 1170 1171Queries contacts based on the specified email address, application, and attributes. This API uses an asynchronous callback to return the result. 1172 1173**Permission required**: ohos.permission.READ_CONTACTS 1174 1175**System capability**: SystemCapability.Applications.ContactsData 1176 1177**Parameters** 1178 1179| Name | Type | Mandatory| Description | 1180| -------- | ----------------------------------------------------- | ---- | ------------------------------------ | 1181| email | string | Yes | Email address of the contact. | 1182| holder | [Holder](#holder) | Yes | Application that creates the contacts. | 1183| attrs | [ContactAttributes](#contactattributes) | Yes | List of contact attributes. | 1184| callback | AsyncCallback<Array<[Contact](#contact)>> | Yes | Callback used to return the result.| 1185 1186**Example** 1187 1188 ```js 1189 contact.queryContactsByEmail('xxx@email.com', { 1190 holderId: 0, 1191 bundleName: "", 1192 displayName: "" 1193 }, { 1194 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 1195 }, (err, data) => { 1196 if (err) { 1197 console.log(`queryContactsByEmail callback: err->${JSON.stringify(err)}`); 1198 return; 1199 } 1200 console.log(`queryContactsByEmail callback: success data->${JSON.stringify(data)}`); 1201 }); 1202 ``` 1203 1204 1205## contact.queryContactsByEmail 1206 1207queryContactsByEmail(email: string, holder?: Holder, attrs?: ContactAttributes): Promise<Array<Contact>> 1208 1209Queries contacts based on the specified email address, application, and attributes. This API uses a promise to return the result. 1210 1211**Permission required**: ohos.permission.READ_CONTACTS 1212 1213**System capability**: SystemCapability.Applications.ContactsData 1214 1215**Parameters** 1216 1217| Name| Type | Mandatory| Description | 1218| ------ | --------------------------------------- | ---- | ---------------------- | 1219| email | string | Yes | Email address of the contact. | 1220| holder | [Holder](#holder) | No | Application that creates the contacts.| 1221| attrs | [ContactAttributes](#contactattributes) | No | List of contact attributes. | 1222 1223**Return Value** 1224 1225| Type | Description | 1226| ----------------------------------------------- | --------------------------------------------------- | 1227| Promise<Array<[Contact](#contact)>> | Promise used to return the result.| 1228 1229**Example** 1230 1231 ```js 1232 let promise = contact.queryContactsByEmail('xxx@email.com', { 1233 holderId: 0, 1234 bundleName: "", 1235 displayName: "" 1236 }, { 1237 attributes: [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME] 1238 }); 1239 promise.then((data) => { 1240 console.log(`queryContactsByEmail success: data->${JSON.stringify(data)}`); 1241 }).catch((err) => { 1242 console.error(`queryContactsByEmail fail: err->${JSON.stringify(err)}`); 1243 }); 1244 ``` 1245 1246 1247## contact.queryGroups 1248 1249queryGroups(callback: AsyncCallback<Array<Group>>): void 1250 1251Queries all groups of this contact. This API uses an asynchronous callback to return the result. 1252 1253**Permission required**: ohos.permission.READ_CONTACTS 1254 1255**System capability**: SystemCapability.Applications.ContactsData 1256 1257**Parameters** 1258 1259| Name | Type | Mandatory| Description | 1260| -------- | ------------------------------------------------- | ---- | ------------------------------------ | 1261| callback | AsyncCallback<Array<[Group](#group)>> | Yes | Callback used to return the result.| 1262 1263**Example** 1264 1265 ```js 1266 contact.queryGroups((err, data) => { 1267 if (err) { 1268 console.log(`queryGroups callback: err->${JSON.stringify(err)}`); 1269 return; 1270 } 1271 console.log(`queryGroups callback: success data->${JSON.stringify(data)}`); 1272 }); 1273 ``` 1274 1275 1276## contact.queryGroups 1277 1278queryGroups(holder: Holder, callback: AsyncCallback<Array<Group>>): void 1279 1280Queries all groups of this contact based on the specified application. This API uses an asynchronous callback to return the result. 1281 1282**Permission required**: ohos.permission.READ_CONTACTS 1283 1284**System capability**: SystemCapability.Applications.ContactsData 1285 1286**Parameters** 1287 1288| Name | Type | Mandatory| Description | 1289| -------- | ------------------------------------------------- | ---- | ------------------------------------ | 1290| holder | Holder | Yes | Application that creates the contacts. | 1291| callback | AsyncCallback<Array<[Group](#group)>> | Yes | Callback used to return the result.| 1292 1293**Example** 1294 1295 ```js 1296 contact.queryGroups({ 1297 holderId: 0, 1298 bundleName: "", 1299 displayName: "" 1300 }, (err, data) => { 1301 if (err) { 1302 console.log(`queryGroups callback: err->${JSON.stringify(err)}`); 1303 return; 1304 } 1305 console.log(`queryGroups callback: success data->${JSON.stringify(data)}`); 1306 }); 1307 ``` 1308 1309 1310## contact.queryGroups 1311 1312queryGroups(holder?: Holder): Promise<Array<Group>> 1313 1314Queries all groups of this contact based on the specified application. This API uses a promise to return the result. 1315 1316**Permission required**: ohos.permission.READ_CONTACTS 1317 1318**System capability**: SystemCapability.Applications.ContactsData 1319 1320**Parameters** 1321 1322| Name| Type | Mandatory| Description | 1323| ------ | ----------------- | ---- | ---------------------- | 1324| holder | [Holder](#holder) | No | Application that creates the contacts.| 1325 1326**Return Value** 1327 1328| Type | Description | 1329| ------------------------------------------- | ------------------------------------------------- | 1330| Promise<Array<[Group](#group)>> | Promise used to return the result.| 1331 1332**Example** 1333 1334 ```js 1335 let promise = contact.queryGroups({ 1336 holderId: 0, 1337 bundleName: "", 1338 displayName: "" 1339 }); 1340 promise.then((data) => { 1341 console.log(`queryGroups success: data->${JSON.stringify(data)}`); 1342 }).catch((err) => { 1343 console.error(`queryGroups fail: err->${JSON.stringify(err)}`); 1344 }); 1345 ``` 1346 1347 1348## contact.queryHolders 1349 1350queryHolders(callback: AsyncCallback<Array<Holder>>): void 1351 1352Queries all applications that have created contacts. This API uses an asynchronous callback to return the result. 1353 1354**Permission required**: ohos.permission.READ_CONTACTS 1355 1356**System capability**: SystemCapability.Applications.ContactsData 1357 1358**Parameters** 1359 1360| Name | Type | Mandatory| Description | 1361| -------- | --------------------------------------------------- | ---- | ---------------------------------------------------- | 1362| callback | AsyncCallback<Array<[Holder](#holder)>> | Yes | Callback used to return the result.| 1363 1364**Example** 1365 1366 ```js 1367 contact.queryHolders((err, data) => { 1368 if (err) { 1369 console.log(`queryHolders callback: err->${JSON.stringify(err)}`); 1370 return; 1371 } 1372 console.log(`queryHolders callback: success data->${JSON.stringify(data)}`); 1373 }); 1374 ``` 1375 1376 1377## contact.queryHolders 1378 1379queryHolders(): Promise<Array<Holder>> 1380 1381Queries all applications that have created contacts. This API uses a promise to return the result. 1382 1383**Permission required**: ohos.permission.READ_CONTACTS 1384 1385**System capability**: SystemCapability.Applications.ContactsData 1386 1387**Return Value** 1388 1389| Type | Description | 1390| --------------------------------------------- | ------------------------------------------------------------ | 1391| Promise<Array<[Holder](#holder)>> | Promise used to return the result.| 1392 1393**Example** 1394 1395 ```js 1396 let promise = contact.queryHolders(); 1397 promise.then((data) => { 1398 console.log(`queryHolders success: data->${JSON.stringify(data)}`); 1399 }).catch((err) => { 1400 console.error(`queryHolders fail: err->${JSON.stringify(err)}`); 1401 }); 1402 ``` 1403 1404 1405## contact.queryKey 1406 1407queryKey(id: number, callback: AsyncCallback<string>): void 1408 1409Queries the key of a contact based on the specified contact ID. This API uses an asynchronous callback to return the result. 1410 1411**Permission required**: ohos.permission.READ_CONTACTS 1412 1413**System capability**: SystemCapability.Applications.ContactsData 1414 1415**Parameters** 1416 1417| Name | Type | Mandatory| Description | 1418| -------- | --------------------------- | ---- | --------------------------------------- | 1419| id | number | Yes | Contact ID. | 1420| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1421 1422**Example** 1423 1424 ```js 1425 contact.queryKey(/*id*/1, (err, data) => { 1426 if (err) { 1427 console.log(`queryKey callback: err->${JSON.stringify(err)}`); 1428 return; 1429 } 1430 console.log(`queryKey callback: success data->${JSON.stringify(data)}`); 1431 }); 1432 ``` 1433 1434 1435## contact.queryKey 1436 1437queryKey(id: number, holder: Holder, callback: AsyncCallback<string>): void 1438 1439Queries the key of a contact based on the specified contact ID and application. This API uses an asynchronous callback to return the result. 1440 1441**Permission required**: ohos.permission.READ_CONTACTS 1442 1443**System capability**: SystemCapability.Applications.ContactsData 1444 1445**Parameters** 1446 1447| Name | Type | Mandatory| Description | 1448| -------- | --------------------------- | ---- | --------------------------------------- | 1449| id | number | Yes | Contact ID. | 1450| holder | [Holder](#holder) | Yes | Application that creates the contacts. | 1451| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1452 1453**Example** 1454 1455 ```js 1456 contact.queryKey(/*id*/1, { 1457 holderId: 0, 1458 bundleName: "", 1459 displayName: "" 1460 }, (err, data) => { 1461 if (err) { 1462 console.log(`queryKey callback: err->${JSON.stringify(err)}`); 1463 return; 1464 } 1465 console.log(`queryKey callback: success data->${JSON.stringify(data)}`); 1466 }); 1467 ``` 1468 1469 1470## contact.queryKey 1471 1472queryKey(id: number, holder?: Holder): Promise<string> 1473 1474Queries the key of a contact based on the specified contact ID and application. This API uses a promise to return the result. 1475 1476**Permission required**: ohos.permission.READ_CONTACTS 1477 1478**System capability**: SystemCapability.Applications.ContactsData 1479 1480**Parameters** 1481 1482| Name| Type | Mandatory| Description | 1483| ------ | ----------------- | ---- | ---------------------- | 1484| id | number | Yes | Contact ID. | 1485| holder | [Holder](#holder) | No | Application that creates the contacts.| 1486 1487**Return Value** 1488 1489| Type | Description | 1490| --------------------- | ---------------------------------------------------- | 1491| Promise<string> | Promise used to return the result.| 1492 1493**Example** 1494 1495 ```js 1496 let promise = contact.queryKey(/*id*/1, { 1497 holderId: 0, 1498 bundleName: "", 1499 displayName: "" 1500 }); 1501 promise.then((data) => { 1502 console.log(`queryKey success: data->${JSON.stringify(data)}`); 1503 }).catch((err) => { 1504 console.error(`queryKey fail: err->${JSON.stringify(err)}`); 1505 }); 1506 ``` 1507 1508 1509## Contact 1510 1511Defines a contact. 1512 1513**System capability**: SystemCapability.Applications.ContactsData 1514 1515### Constant 1516 1517| Name | Value | Description | 1518| ------------------ | ---- | ---------------- | 1519| INVALID_CONTACT_ID | -1 | Default contact ID.| 1520 1521 1522### Attributes 1523 1524| Name | Type | Readable| Writable| Description | 1525| ----------------- | --------------------------------------- | ---- | ---- | -------------------------------------- | 1526| id | number | Yes | No | Contact ID. | 1527| key | string | Yes | No | Contact key. | 1528| contactAttributes | [ContactAttributes](#contactattributes) | Yes | Yes | List of contact attributes. | 1529| emails | [Email](#email)[] | Yes | Yes | List of email addresses of the contact. | 1530| events | [Event](#event)[] | Yes | Yes | List of important dates such as birthdays and anniversaries of the contact.| 1531| groups | [Group](#group)[] | Yes | Yes | List of groups of the contact. | 1532| imAddresses | [ImAddress](#imaddress)[] | Yes | Yes | List of instant message addresses of the contact. | 1533| phoneNumbers | [PhoneNumber](#phonenumber)[] | Yes | Yes | List of phone numbers of the contact. | 1534| portrait | [Portrait](#portrait) | Yes | Yes | Contact portrait. | 1535| postalAddresses | [PostalAddress](#postaladdress)[] | Yes | Yes | List of postal addresses of the contact. | 1536| relations | [Relation](#relation)[] | Yes | Yes | List of relationships with the contact. | 1537| sipAddresses | [SipAddress](#sipaddress)[] | Yes | Yes | List of Session Initiation Protocol (SIP) addresses of the contact. | 1538| websites | [Website](#website)[] | Yes | Yes | List of websites of the contact. | 1539| name | [Name](#name) | Yes | Yes | Contact name. | 1540| nickName | [NickName](#nickname) | Yes | Yes | Contact nickname. | 1541| note | [Note](#note) | Yes | Yes | Contact notes. | 1542| organization | [Organization](#organization) | Yes | Yes | Organization of the contact. | 1543 1544 1545**Example** 1546 1547Create contact data in JSON format: 1548 1549 1550```js 1551let myContact = { 1552 phoneNumbers: [{ 1553 phoneNumber: "138xxxxxxxx" 1554 }], 1555 name: { 1556 fullName: "fullName", 1557 namePrefix: "namePrefix" 1558 }, 1559 nickName: { 1560 nickName: "nickName" 1561 } 1562}; 1563``` 1564 1565 1566 Or, create data by configuring a new Contact object. 1567 1568```js 1569let myContact = new contact.Contact(); 1570let name = new contact.Name(); 1571name.fullName = "fullName"; 1572let phoneNumber = new contact.PhoneNumber(); 1573phoneNumber.phoneNumber = "138xxxxxxxx"; 1574myContact.name = name; 1575myContact.phoneNumbers = [phoneNumber]; 1576``` 1577 1578 1579## ContactAttributes 1580 1581Provides a list of contact attributes, which are generally used as arguments. 1582If **null** is passed, all attributes are queried by default. 1583 1584**System capability**: SystemCapability.Applications.ContactsData 1585 1586| Name | Type | Readable| Writable| Description | 1587| ---------- | ------------------------- | ---- | ---- | ---------------- | 1588| attributes | [Attribute](#attribute)[] | Yes | Yes | List of contact attributes.| 1589 1590 1591**Example** 1592 1593Create contact data in JSON format: 1594 1595 1596```js 1597let contactAttributes = { 1598 attributes: [ 1599 contact.Attribute.ATTR_EMAIL, 1600 contact.Attribute.ATTR_NAME, 1601 contact.Attribute.ATTR_PHONE 1602 ] 1603}; 1604``` 1605 1606Or, create data by configuring a **ContactAttributes** object. 1607 1608 1609```js 1610let contactAttributes = new contact.ContactAttributes(); 1611contactAttributes.attributes = [contact.Attribute.ATTR_EMAIL]; 1612``` 1613 1614 1615## Attribute 1616 1617Enumerates contact attributes. 1618 1619**System capability**: SystemCapability.Applications.ContactsData 1620 1621| Name | Description | 1622| --------------------- | ---------------------------------- | 1623| ATTR_CONTACT_EVENT | Important dates such as birthday and anniversaries of the contact.| 1624| ATTR_EMAIL | Email address of the contact. | 1625| ATTR_GROUP_MEMBERSHIP | Groups of the contact. | 1626| ATTR_IM | IM addresses of the contact. | 1627| ATTR_NAME | Contact name. | 1628| ATTR_NICKNAME | Contact nickname. | 1629| ATTR_NOTE | Contact notes. | 1630| ATTR_ORGANIZATION | Organization of the contact. | 1631| ATTR_PHONE | Phone number of the contacts. | 1632| ATTR_PORTRAIT | Contact portrait. | 1633| ATTR_POSTAL_ADDRESS | Postal address of the contact. | 1634| ATTR_RELATION | Relationship with the contact. | 1635| ATTR_SIP_ADDRESS | SIP addresses of the contact. | 1636| ATTR_WEBSITE | Website that stores the contact information. | 1637 1638 1639**Example** 1640 1641Create contact data in JSON format: 1642 1643```js 1644let attributes = [contact.Attribute.ATTR_EMAIL, contact.Attribute.ATTR_NAME, contact.Attribute.ATTR_PHONE]; 1645``` 1646 1647 1648## Email 1649 1650Defines a contact's email. 1651 1652**System capability**: SystemCapability.Applications.ContactsData 1653 1654### Constant 1655 1656| Name | Value | Description | 1657| ---------------- | ---- | ---------------- | 1658| CUSTOM_LABEL | 0 | Custom mailbox type.| 1659| EMAIL_HOME | 1 | Home mailbox. | 1660| EMAIL_WORK | 2 | Work mailbox. | 1661| EMAIL_OTHER | 3 | Other mailbox. | 1662| INVALID_LABEL_ID | -1 | Invalid mailbox. | 1663 1664 1665### Attributes 1666 1667| Name | Type| Readable| Writable| Description | 1668| ----------- | -------- | ---- | ---- | ---------------- | 1669| email | string | Yes | Yes | Email addresses | 1670| labelName | string | Yes | Yes | Name of the mailbox type.| 1671| displayName | string | Yes | Yes | Displayed name of the mailbox.| 1672| labelId | number | Yes | Yes | Mailbox type. | 1673 1674 1675**Example** 1676 1677 Create contact data in JSON format: 1678 1679```js 1680let email = { 1681 email: "xxx@email.com", 1682 displayName: "displayName" 1683} 1684``` 1685 1686 1687 Or, create data by configuring an **Email** object. 1688 1689```js 1690let email = new contact.Email(); 1691email.email = "xxx@email.com"; 1692``` 1693 1694 1695## Holder 1696 1697Defines an application that creates the contact. 1698 1699**System capability**: SystemCapability.Applications.ContactsData 1700 1701| Name | Type| Readable| Writable| Description | 1702| ----------- | -------- | ---- | ---- | ---------- | 1703| bundleName | string | Yes | No | Bundle name. | 1704| displayName | string | Yes | No | Application name.| 1705| holderId | number | Yes | Yes | Application ID. | 1706 1707 1708**Example** 1709 1710 Create contact data in JSON format: 1711 1712```js 1713let holder = { 1714 holderId: 0 1715}; 1716``` 1717 1718 Or, create data by configuring a **Holder** object. 1719 1720```js 1721let holder = new contact.Holder(); 1722holder.holderId = 0; 1723``` 1724 1725 1726## Event 1727 1728Defines a contact's event. 1729 1730**System capability**: SystemCapability.Applications.ContactsData 1731 1732### Constant 1733 1734| Name | Value | Description | 1735| ----------------- | ---- | ------------------ | 1736| CUSTOM_LABEL | 0 | Custom event. | 1737| EVENT_ANNIVERSARY | 1 | Anniversary event.| 1738| EVENT_OTHER | 2 | Other event. | 1739| EVENT_BIRTHDAY | 3 | Birthday event. | 1740| INVALID_LABEL_ID | -1 | Invalid event. | 1741 1742 1743### Attributes 1744 1745| Name | Type| Readable| Writable| Description | 1746| --------- | -------- | ---- | ---- | -------------- | 1747| eventDate | string | Yes | Yes | Event date. | 1748| labelName | string | Yes | Yes | Event type.| 1749| labelId | number | Yes | Yes | Event type ID. | 1750 1751 1752**Example** 1753 1754 Create contact data in JSON format: 1755 1756```js 1757let event = { 1758 eventDate: "xxxxxx" 1759}; 1760``` 1761 1762 Or, create data by configuring an **Event** object. 1763 1764```js 1765let event = new contact.Event(); 1766event.eventDate = "xxxxxx"; 1767``` 1768 1769 1770## Group 1771 1772Defines a contact group. 1773 1774**System capability**: SystemCapability.Applications.ContactsData 1775 1776| Name | Type| Readable| Writable| Description | 1777| ------- | -------- | ---- | ---- | ------------------ | 1778| groupId | number | Yes | Yes | ID of a contact group. | 1779| title | string | Yes | Yes | Name of a contact group.| 1780 1781 1782**Example** 1783 1784 Create contact data in JSON format: 1785 1786```js 1787let group = { 1788 groupId: 1, 1789 title: "title" 1790}; 1791``` 1792 1793 Or, create data by configuring a **Group** object. 1794 1795```js 1796let group = new contact.Group(); 1797group.title = "title"; 1798``` 1799 1800 1801## ImAddress 1802 1803Enumerates IM addresses. 1804 1805**System capability**: SystemCapability.Applications.ContactsData 1806 1807### Constant 1808 1809| Name | Value | Description | 1810| ---------------- | ---- | -------------------- | 1811| CUSTOM_LABEL | -1 | Custom IM| 1812| IM_AIM | 0 | AIM | 1813| IM_MSN | 1 | MSN | 1814| IM_YAHOO | 2 | Yahoo | 1815| IM_SKYPE | 3 | Skype | 1816| IM_QQ | 4 | QQ | 1817| IM_ICQ | 6 | ICQ | 1818| IM_JABBER | 7 | JABBER| 1819| INVALID_LABEL_ID | -2 | Invalid IM| 1820 1821 1822### Attributes 1823 1824| Name | Type| Readable| Writable| Description | 1825| --------- | -------- | ---- | ---- | ------------------ | 1826| imAddress | string | Yes | Yes | IM address. | 1827| labelName | string | Yes | Yes | IM name.| 1828| labelId | number | Yes | Yes | IM ID. | 1829 1830 1831**Example** 1832 1833 Create contact data in JSON format: 1834 1835```js 1836let imAddress = { 1837 imAddress: "imAddress", 1838 labelName: "labelName" 1839}; 1840``` 1841 1842 1843 Or, create data by configuring an **ImAddress** object. 1844 1845```js 1846let imAddress = new contact.ImAddress(); 1847imAddress.imAddress = "imAddress"; 1848``` 1849 1850 1851## Name 1852 1853Defines a contact's name. 1854 1855**System capability**: SystemCapability.Applications.ContactsData 1856 1857| Name | Type| Readable| Writable| Description | 1858| ------------------ | -------- | ---- | ---- | --------------------------- | 1859| familyName | string | Yes | Yes | Family name. | 1860| familyNamePhonetic | string | Yes | Yes | Family name in pinyin. | 1861| fullName | string | Yes | Yes | Full name of the contact. | 1862| givenName | string | Yes | Yes | Given name of the contact.| 1863| givenNamePhonetic | string | Yes | Yes | Given name of the contact in pinyin. | 1864| middleName | string | Yes | Yes | Middle name of the contact. | 1865| middleNamePhonetic | string | Yes | Yes | Middle name of the contact in pinyin. | 1866| namePrefix | string | Yes | Yes | Prefix of the contact name. | 1867| nameSuffix | string | Yes | Yes | Suffix of the contact name. | 1868 1869 1870**Example** 1871 1872 Create contact data in JSON format: 1873 1874```js 1875let name = { 1876 familyName: "familyName", 1877 fullName: "fullName" 1878}; 1879``` 1880 1881 Or, create data by configuring a **Name** object. 1882 1883```js 1884let name = new contact.Name(); 1885name.familyName = "familyName"; 1886name.fullName = "fullName"; 1887``` 1888 1889 1890## NickName 1891 1892Defines a contact's nickname. 1893 1894**System capability**: SystemCapability.Applications.ContactsData 1895 1896| Name | Type| Readable| Writable| Description | 1897| -------- | -------- | ---- | ---- | -------------- | 1898| nickName | string | Yes | Yes | Contact nickname.| 1899 1900 1901**Example** 1902 1903 Create contact data in JSON format: 1904 1905```js 1906let nickName = { 1907 nickName: "nickName" 1908}; 1909``` 1910 1911 Or, create data by configuring a **NickName** object. 1912 1913```js 1914let nickName = new contact.NickName(); 1915nickName.nickName = "nickName"; 1916``` 1917 1918 1919## Note 1920 1921Defines a contact's note. 1922 1923**System capability**: SystemCapability.Applications.ContactsData 1924 1925| Name | Type| Readable| Writable| Description | 1926| ----------- | -------- | ---- | ---- | ------------------ | 1927| noteContent | string | Yes | Yes | Notes of the contact.| 1928 1929 1930**Example** 1931 1932 Create contact data in JSON format: 1933 1934```js 1935let note = { 1936 noteContent: "noteContent" 1937}; 1938``` 1939 1940 Or, create data by configuring a **Note** object. 1941 1942```js 1943let note = new contact.Note(); 1944note.noteContent = "noteContent"; 1945``` 1946 1947 1948## Organization 1949 1950Defines a contact's organization. 1951 1952**System capability**: SystemCapability.Applications.ContactsData 1953 1954| Name | Type| Readable| Writable| Description | 1955| ----- | -------- | ---- | ---- | ---------- | 1956| name | string | Yes | Yes | Organization name.| 1957| title | string | Yes | Yes | Organization title.| 1958 1959 1960**Example** 1961 1962 Create contact data in JSON format: 1963 1964```js 1965let organization = { 1966 name: "name", 1967 title: "title" 1968}; 1969``` 1970 1971 Or, create data by configuring an **Organization** object. 1972 1973```js 1974let organization = new contact.Organization(); 1975organization.name = "name"; 1976organization.title = "title"; 1977``` 1978 1979 1980## PhoneNumber 1981 1982Defines a contact's phone number. 1983 1984**System capability**: SystemCapability.Applications.ContactsData 1985 1986### Constant 1987 1988| Name | Value | Description | 1989| ---------------- | ---- | ------------------------------------------------ | 1990| CUSTOM_LABEL | 0 | Custom phone type. | 1991| NUM_HOME | 1 | Home phone. | 1992| NUM_MOBILE | 2 | Mobile phone. | 1993| NUM_WORK | 3 | Work phone. | 1994| NUM_FAX_WORK | 4 | Work fax. | 1995| NUM_FAX_HOME | 5 | Family fax. | 1996| NUM_PAGER | 6 | Pager. | 1997| NUM_OTHER | 7 | Other phone type. | 1998| NUM_CALLBACK | 8 | Callback phone. | 1999| NUM_CAR | 9 | Car phone. | 2000| NUM_COMPANY_MAIN | 10 | Company phone. | 2001| NUM_ISDN | 11 | Integrated Services Digital Network (ISDN) phone. | 2002| NUM_MAIN | 12 | Main phone. | 2003| NUM_OTHER_FAX | 13 | Other fax phone. | 2004| NUM_RADIO | 14 | Wireless phone. | 2005| NUM_TELEX | 15 | Telex phone. | 2006| NUM_TTY_TDD | 16 | Teletypewriter (TTY) or Test Driven Development (TDD) phone.| 2007| NUM_WORK_MOBILE | 17 | Work mobile phone. | 2008| NUM_WORK_PAGER | 18 | Work pager. | 2009| NUM_ASSISTANT | 19 | Assistant phone. | 2010| NUM_MMS | 20 | MMS phone. | 2011| INVALID_LABEL_ID | -1 | Invalid phone type. | 2012 2013 2014### Attributes 2015 2016| Name | Type| Readable| Writable| Description | 2017| ----------- | -------- | ---- | ---- | ------------------ | 2018| labelName | string | Yes | Yes | Phone number type.| 2019| phoneNumber | string | Yes | Yes | Phone number. | 2020| labelId | number | Yes | Yes | Phone number ID. | 2021 2022 2023**Example** 2024 2025 Create contact data in JSON format: 2026 2027```js 2028let phoneNumber = { 2029 phoneNumber: "138xxxxxxxx", 2030 labelId: contact.PhoneNumber.NUM_HOME 2031}; 2032``` 2033 2034 Or, create data by configuring a new **PhoneNumber** object. 2035 2036```js 2037let phoneNumber = new contact.PhoneNumber(); 2038phoneNumber.phoneNumber = "138xxxxxxxx"; 2039``` 2040 2041 2042## Portrait 2043 2044Defines a contact's portrait. 2045 2046**System capability**: SystemCapability.Applications.ContactsData 2047 2048| Name| Type| Readable| Writable| Description | 2049| ---- | -------- | ---- | ---- | -------------- | 2050| uri | string | Yes | Yes | Contact portrait.| 2051 2052 2053**Example** 2054 2055 Create contact data in JSON format: 2056 2057```js 2058let portrait = { 2059 uri: "uri" 2060}; 2061``` 2062 2063 Or, create data by configuring a new **Portrait** object. 2064 2065```js 2066let portrait = new contact.Portrait(); 2067portrait.uri = "uri"; 2068``` 2069 2070 2071## PostalAddress 2072 2073Defines a contact's postal address. 2074 2075**System capability**: SystemCapability.Applications.ContactsData 2076 2077### Constant 2078 2079| Name | Value | Description | 2080| ---------------- | ---- | -------------------- | 2081| CUSTOM_LABEL | 0 | Custom postal address type.| 2082| ADDR_HOME | 1 | Home address. | 2083| ADDR_WORK | 2 | Work address. | 2084| ADDR_OTHER | 3 | Other addresses. | 2085| INVALID_LABEL_ID | -1 | Invalid address type. | 2086 2087 2088### Attributes 2089 2090| Name | Type| Readable| Writable| Description | 2091| ------------- | -------- | ---- | ---- | -------------------------- | 2092| city | string | Yes | Yes | City where the contact is located. | 2093| country | string | Yes | Yes | Country/Region where the contact is located. | 2094| labelName | string | Yes | Yes | Postal address type. | 2095| neighborhood | string | Yes | Yes | Neighbor of the contact. | 2096| pobox | string | Yes | Yes | Email of the contact. | 2097| postalAddress | string | Yes | Yes | Postal address of the contact. | 2098| postcode | string | Yes | Yes | Postal code of the region where the contact is located.| 2099| region | string | Yes | Yes | Area where the contact is located. | 2100| street | string | Yes | Yes | Street where the contact resides. | 2101| labelId | number | Yes | Yes | Postal address ID. | 2102 2103 2104**Example** 2105 2106 Create contact data in JSON format: 2107 2108```js 2109let postalAddress = { 2110 city: "city" 2111}; 2112``` 2113 2114 Or, create data by configuring a new **PostalAddress** object. 2115 2116```js 2117let postalAddress = new contact.PostalAddress(); 2118postalAddress.city = "city"; 2119``` 2120 2121 2122## Relation 2123 2124Defines a contact's relationship. 2125 2126**System capability**: SystemCapability.Applications.ContactsData 2127 2128### Constant 2129 2130| Name | Value | Description | 2131| ------------------------- | ---- | ------------------ | 2132| CUSTOM_LABEL | 0 | Custom relationship. | 2133| RELATION_ASSISTANT | 1 | Assistant. | 2134| RELATION_BROTHER | 2 | Sibling. | 2135| RELATION_CHILD | 3 | Child. | 2136| RELATION_DOMESTIC_PARTNER | 4 | Domestic partner.| 2137| RELATION_FATHER | 5 | Father. | 2138| RELATION_FRIEND | 6 | Friend. | 2139| RELATION_MANAGER | 7 | Manager. | 2140| RELATION_MOTHER | 8 | Mother. | 2141| RELATION_PARENT | 9 | Parent. | 2142| RELATION_PARTNER | 10 | Partner.| 2143| RELATION_REFERRED_BY | 11 | Referrer. | 2144| RELATION_RELATIVE | 12 | Relative. | 2145| RELATION_SISTER | 13 | Sister. | 2146| RELATION_SPOUSE | 14 | Spouse. | 2147| INVALID_LABEL_ID | -1 | Invalid relationship. | 2148 2149 2150### Attributes 2151 2152| Name | Type| Readable| Writable| Description | 2153| ------------ | -------- | ---- | ---- | -------------- | 2154| labelName | string | Yes | Yes | Relationship type.| 2155| relationName | string | Yes | Yes | Relationship name. | 2156| labelId | number | Yes | Yes | Relationship ID. | 2157 2158 2159**Example** 2160 2161 Create contact data in JSON format: 2162 2163```js 2164let relation = { 2165 relationName: "relationName", 2166 labelId: contact.Relation.RELATION_ASSISTANT 2167}; 2168``` 2169 2170 Or, create data by configuring a new **Relation** object. 2171 2172```js 2173let relation = new contact.Relation(); 2174relation.relationName = "relationName"; 2175relation.labelId = contact.Relation.RELATION_ASSISTANT; 2176``` 2177 2178 2179## SipAddress 2180 2181Defines a contact's SIP address. 2182 2183**System capability**: SystemCapability.Applications.ContactsData 2184 2185### Constant 2186 2187| Name | Value | Description | 2188| ---------------- | ---- | ----------------------------------- | 2189| CUSTOM_LABEL | 0 | Custom SIP address.| 2190| SIP_HOME | 1 | Home SIP address. | 2191| SIP_WORK | 2 | Work SIP address. | 2192| SIP_OTHER | 3 | Other SIP address. | 2193| INVALID_LABEL_ID | -1 | Invalid SIP address. | 2194 2195 2196### Attributes 2197 2198| Name | Type| Readable| Writable| Description | 2199| ---------- | -------- | ---- | ---- | --------------------------------- | 2200| labelName | string | Yes | Yes | SIP address type.| 2201| sipAddress | string | Yes | Yes | SIP address. | 2202| labelId | number | Yes | Yes | SIP address ID. | 2203 2204 2205**Example** 2206 2207 Create contact data in JSON format: 2208 2209```js 2210var sipAddress = { 2211 sipAddress: "sipAddress" 2212}; 2213``` 2214 2215 Or, create data by configuring a new **SipAddress** object. 2216 2217```js 2218let sipAddress = new contact.SipAddress(); 2219sipAddress.sipAddress = "sipAddress"; 2220``` 2221 2222 2223## Website 2224 2225Defines a contact's website. 2226 2227**System capability**: SystemCapability.Applications.ContactsData 2228 2229| Name | Type| Readable| Writable| Description | 2230| ------- | -------- | ---- | ---- | ------------------ | 2231| website | string | Yes | Yes | Website of the contact.| 2232 2233 2234**Example** 2235 2236 Create contact data in JSON format: 2237 2238```js 2239let website = { 2240 website: "website" 2241}; 2242``` 2243 2244 Or, create data by configuring a new **Website** object. 2245 2246```js 2247let website = new contact.Website(); 2248website.website = "website"; 2249```