public async Task ExportAsync(TdWindows.Users result) { using (await _syncLock.WaitAsync()) { Debug.WriteLine("» Exporting contacts"); var store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite); if (store == null) { return; } var contactList = await GetContactListAsync(store); var annotationList = await GetAnnotationListAsync(); if (contactList != null && annotationList != null) { await ExportAsync(contactList, annotationList, result); } Debug.WriteLine("» Exporting contacts completed"); } }
private async Task ExportAsync(ContactList contactList, ContactAnnotationList annotationList, TdWindows.Users result) { if (result == null) { return; } foreach (var item in result.UserIds) { var user = _protoService.GetUser(item); var contact = await contactList.GetContactFromRemoteIdAsync("u" + user.Id); if (contact == null) { contact = new Contact(); } contact.FirstName = user.FirstName ?? string.Empty; contact.LastName = user.LastName ?? string.Empty; //contact.Nickname = item.Username ?? string.Empty; contact.RemoteId = "u" + user.Id; //contact.Id = item.Id.ToString(); var phone = contact.Phones.FirstOrDefault(); if (phone == null) { phone = new ContactPhone(); phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); contact.Phones.Add(phone); } else { phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); } await contactList.SaveContactAsync(contact); ContactAnnotation annotation; var annotations = await annotationList.FindAnnotationsByRemoteIdAsync(user.Id.ToString()); if (annotations.Count == 0) { annotation = new ContactAnnotation(); } else { annotation = annotations[0]; } annotation.ContactId = contact.Id; annotation.RemoteId = contact.RemoteId; annotation.SupportedOperations = ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.Message | ContactAnnotationOperations.AudioCall; if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5)) { annotation.SupportedOperations |= ContactAnnotationOperations.Share; } if (annotation.ProviderProperties.Count == 0) { annotation.ProviderProperties.Add("ContactPanelAppID", Package.Current.Id.FamilyName + "!App"); annotation.ProviderProperties.Add("ContactShareAppID", Package.Current.Id.FamilyName + "!App"); } var added = await annotationList.TrySaveAnnotationAsync(annotation); } }