ThangBM
ThangBM

Reputation: 301

(Windows Phone 10) Is possible to edit, add new contact programmatically in windows phone 10?

I want to implement function edit and add contact programatically in windows phone 10.

Is it possible? Has any sample about it ?

Upvotes: 4

Views: 1139

Answers (1)

gregkalapos
gregkalapos

Reputation: 3619

Here is a code snippet for creating the contact:

 public async Task AddContact(String FirstName, String LastName)
  {
    var contact = new Windows.ApplicationModel.Contacts.Contact();
    contact.FirstName = FirstName;
    contact.LastName = LastName;
    //Here you can set other properties...

    //Get he contact store for the app (so no lists from outlook and other stuff will be in the returned lists..)
    var contactstore = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync(Windows.ApplicationModel.Contacts.ContactStoreAccessType.AppContactsReadWrite);

    try
    {
        var contactLists = await contactstore.FindContactListsAsync();

        Windows.ApplicationModel.Contacts.ContactList contactList;

        //if there is no contact list we create one
        if (contactLists.Count == 0)
        {
            contactList = await contactstore.CreateContactListAsync("MyList");
        }
        //otherwise if there is one then we reuse it
        else
        {
            contactList = contactLists.FirstOrDefault();
        }

        await contactList.SaveContactAsync(contact);
    }
    catch
    {
        //Handle it properly... 
    }
}

And here is a short sample for changing an existing contact:

//you can obviusly couple the changes better then this... this is just to show the basics 
public async Task ChangeContact(Windows.ApplicationModel.Contacts.Contact ContactToChange, String NewFirstName, String NewLastName)
{
    var contactStore = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync(Windows.ApplicationModel.Contacts.ContactStoreAccessType.AppContactsReadWrite);

    var contactList = await contactStore.GetContactListAsync(ContactToChange.ContactListId);

    var contact = await contactList.GetContactAsync(ContactToChange.Id);

    contact.FirstName = NewFirstName;
    contact.LastName = NewLastName;

    await contactList.SaveContactAsync(contact);
}

And very important: In the appxmanifest you have to add the contacts capability. Right click to it in the solution explorer and "View Code" and then under Capabilities put

<uap:Capability Name="contacts" />

There is no UI for this. See this.

Both samples are meant to be for starting point... obviously it's not production ready and you have to adapt it to your scenario.

Update

Since this came up in the comments I extend my answer a little bit.

Based on this (plus my own experimentation) the ContactListId for aggregated contacts is null (which makes sense if you think about it). Here is how to get the raw contact with ContactlLstId (code is based on the comment from the link)

 public async Task IterateThroughContactsForContactListId()
 {            
            ContactStore allAccessStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);

            var contacts = await allAccessStore.FindContactsAsync();
            foreach (var contact in contacts)
            {
                //process aggregated contacts
                if (contact.IsAggregate)
                {
                    //here contact.ContactListId is "" (null....)                  
                    //in this case if you need the the ContactListId then you need to iterate through the raw contacts
                    var rawContacts = await allAccessStore.AggregateContactManager.FindRawContactsAsync(contact);
                    foreach (var rawContact in rawContacts)
                    {
                        //Here you should have ContactListId
                        Debug.WriteLine($"aggregated, name: {rawContact.DisplayName }, ContactListId: {rawContact.ContactListId}");
                    }
                }
                else //not aggregated contacts should work
                {
                    Debug.WriteLine($"not aggregated, name: {contact.DisplayName }, ContactListId: {contact.ContactListId}");
                }
            }

}

And another important thing:

According to the documentation you won’t be able to change all the contacts which are created by other apps.

AllContactsReadWrite:

Read and write access to all app and system contacts. This value is not available to all apps. Your developer account must be specially provisioned by Microsoft in order to request this level of access.

In some cases, I get a System.UnauthorizedAccessException when SaveContactAsync(contact) is called. One example for this was when the contact was in the Skype Contact List.

Upvotes: 5

Related Questions