0

I am trying to connect and sync a local addressbook from my UWP app.

//Create or Find Contact List
                ContactList contactList;
                var contactLists = await store.FindContactListsAsync();
                if (0 == contactLists.Count) { contactList = await store.CreateContactListAsync("Practice Visuals"); }
                else { contactList = contactLists.Where(x => x.DisplayName == "Practice Visuals").FirstOrDefault(); }

                foreach (var p in Reporting.Patients.Take(10))
                {
                    Contact contact = null; try { contact = await contactList.GetContactAsync(p.PatNum); } catch { }
                    if (contact is null) //Contact not found hence create new contact
                    {
                        contact = new Contact()
                        {
                            Id = p.PatNum.ToString(),
                            RemoteId = p.PatNum.ToString(),
                            FirstName = p.FName,
                            LastName = p.LName,
                            MiddleName = p.MiddleI,
                            Nickname = p.Preferred,
                        };

                        contact.Emails.Add(new ContactEmail() { Address = p.Email, Kind = ContactEmailKind.Other });

                        contact.Phones.Add(new ContactPhone() { Kind = ContactPhoneKind.Mobile, Number = p.WirelessPhone });
                        contact.Phones.Add(new ContactPhone() { Kind = ContactPhoneKind.Home, Number = p.HmPhone });
                        contact.Phones.Add(new ContactPhone() { Kind = ContactPhoneKind.Work, Number = p.WkPhone });

                        //contact.Addresses.Add(new ContactAddress() { StreetAddress = p.Address + Environment.NewLine + p.Address2, Locality = p.City, Region = p.State, PostalCode = p.Zip });
                        //contact.ImportantDates.Add(new ContactDate() { Kind = ContactDateKind.Birthday, Day = (uint)((DateTime)p.Birthdate).Day, Month = (uint)((DateTime)p.Birthdate).Month, Year = ((DateTime)p.Birthdate).Year });
                        //contact.ImportantDates.Add(new ContactDate() { Kind = ContactDateKind.Anniversary, Day = (uint)((DateTime)p.DateFirstVisit).Day, Month = (uint)((DateTime)p.DateFirstVisit).Month, Year = ((DateTime)p.DateFirstVisit).Year });

                        //contact.Fields.Add(new ContactField("Clinic", new Converters.DataTypeValueConverter().Convert(p.ClinicNum, parameter: "ClinicNum"), ContactFieldType.Custom, ContactFieldCategory.None));
                        //contact.Fields.Add(new ContactField("Status", new Converters.DataTypeValueConverter().Convert(p.PatStatus, parameter: "PatStatus"), ContactFieldType.Custom, ContactFieldCategory.None));
                        //contact.Fields.Add(new ContactField("Gender", new Converters.DataTypeValueConverter().Convert(p.Gender, parameter: "Gender"), ContactFieldType.Custom, ContactFieldCategory.None));

                        await contactList.SaveContactAsync(contact);
                    }
                    else //Contact Found - check to see if update necessary
                    {

                    }
                }

                ContactsList.ItemsSource = await store.FindContactsAsync();

I have disabled (//) some lines to rule them out as being the issue but the problem still persists.

How do I fix this issue?

Novin S
  • 23
  • 5
  • Is there any updates about your issue? – Roy Li - MSFT Mar 25 '22 at 03:03
  • Will test over this weekend. – Novin S Mar 25 '22 at 12:56
  • Not setting ID but setting RemoteID works as expected. I don't understand why the description says ID can be set or get!? Anyways thanks. – Novin S Mar 25 '22 at 23:26
  • The sample code referenced is not about creating contacts and retrieving them. It is about using the contactPanel to pull contacts already in the addressbook. Setting RemoteID does not really stick. When I retrieve the list again by querying the store, they are all null strings. I am basically trying to sync contacts in the contactlist with my contacts that are on a mySQL database. I cannot find a way to update a contact so I tried deleting the contacts everytime it is syncing and adding it. – Novin S Mar 26 '22 at 01:31
  • Since there is no way to differentiate contacts created by my app from those already existing from other apps (like Mail and Calendar), I added a note to distinguish it. Contact contact = null; try { contact = contacts.Find(x => x.Notes.Contains($"Patient Number = {p.PatNum.ToString()}")); } catch {} if (!(contact is null)) { await contactList.DeleteContactAsync(contact); } Getting "Value does not fall within an expected range" when using DeleteContactAsync(contact) – Novin S Mar 26 '22 at 01:37

2 Answers2

0

The documentation is pretty clear about that exception:

Throws a System.ArgumentException: 'Value does not fall within the expected range.' when the contact passed as parameter has a RemoteID set which is identical to a contacts RemoteID already saved on this device.

In short, check the RemoteID you're passing.

aybe
  • 15,516
  • 9
  • 57
  • 105
  • I disabled both remoteid and it still throws the same error. I disabled id as well and now it works but problem is I can't use id at all since they are all empty. – Novin S Mar 16 '22 at 04:13
  • Also how do I get the contactlist associated with a contact? All the contactlistids are "". – Novin S Mar 16 '22 at 04:14
  • @NovinS The `RemoteID` should be a necessary parameter and you could try to give it a value that you haven't used before. – Roy Li - MSFT Mar 16 '22 at 08:24
  • It is being given a unique number but it seems to fail. Removing it completely works but then there are no IDs. Also, there doesn't seem to be a way to isolate and see contacts from a specific account or even all the contacts within the contact list that the app created or am I missing something? – Novin S Mar 19 '22 at 19:48
0

I tried your code to go through the process. I found there is a little mistake that causes this behavior. Please do not define the ContactList.Id Property when you are creating a new ContactList Object.

After removing that, the code will work correctly. Like the following:

 Contact contact = new Contact();

        contact.FirstName = "Jane";
        contact.LastName = "ContactPanelSample";
        contact.RemoteId = "3334";
        //contact.Id = "123";
        contact.Emails.Add(new ContactEmail { Address = "janedoe@example.com" });
        contact.Phones.Add(new ContactPhone { Number = "4255550123" });
        contact.SourceDisplayPicture = RandomAccessStreamReference.CreateFromUri(new Uri("https://learn.microsoft.com/en-us/windows/uwp/contacts-and-calendar/images/shoulder-tap-static-payload.png"));

        await contactList.SaveContactAsync(contact);

Besides, there is an official sample that shows how to use these APIs-ContactPanel. You could check that sample as well.

Roy Li - MSFT
  • 8,043
  • 1
  • 7
  • 13
  • How do I retrieve a list of all contacts from this contact list or others that are in the address book? – Novin S Mar 25 '22 at 12:57