Rishabh Jain
Rishabh Jain

Reputation: 41

How to fetch all the data from the contacts in a Windows Phone app?

I want to have the names and pictures of all the contacts and store it somewhere? How can we do that? From what I have found, it is only allowing to search for one and get its details.

Upvotes: 0

Views: 2049

Answers (2)

aloisdg
aloisdg

Reputation: 23511

You should start with a reading on Contact filtering and matching in msdn. For code snippet, check How to access contact data for Windows Phone.

For example a sample from msdn to count this items :

private void ButtonContacts_Click(object sender, RoutedEventArgs e)
{
    Contacts cons = new Contacts();

    //Identify the method that runs after the asynchronous search completes.
    cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

    //Start the asynchronous search.
    cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
}

void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    //Do something with the results.
    MessageBox.Show(e.Results.Count().ToString());
}

Don't forget to add the ID_CAP_CONTACTS capability in your app's manifest and a using Microsoft.Phone.UserData; to your code.

Update :

For example if you want every name of your contacts :

void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    IEnumerable<Contact> contacts = e.Results; //Here your result
    string everynames = String.Empty;
    foreach (var item in contacts)
    {
        //We can get attributes from each item
        everynames += item.DisplayName +  Environment.NewLine;
    }
    MessageBox.Show(everynames);
}

Update 2 :

For example if you want name, first mail and first number, you could use a code like this :

    void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
    {
        IEnumerable<Contact> contacts = e.Results; //Here your result
        string everynames = String.Empty;
        foreach (var item in contacts)
        {
            //We can get attributes from each item
            everynames += item.DisplayName + ";" //Get name
                + (item.EmailAddresses.Count() > 0 ? (item.EmailAddresses.FirstOrDefault()).EmailAddress : "") + ";" //Check if contact has an email. If so, display it. He can be more than one !
                + (item.PhoneNumbers.Count() > 0 ? (item.PhoneNumbers.FirstOrDefault()).PhoneNumber : "") + ";" //Check if contact has a phonenumber. If so, display it. He can be more than one !
                + Environment.NewLine;
        }
        MessageBox.Show(everynames);
    }

Update 3 :

If you want all pictures, I will share you an example. Don't forget to check the doc :

void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
        foreach (var result in e.Results)
        {
            var stream = result.GetPicture();
            if (stream != null)
            {
                System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                bmp.SetSource(stream); // You can do a list of image if you want to.
                Image img = new Image();
                img.Source = bmp;
                stack.Children.Add(img); // I choose to display in a stackpanel
            }
        }
}

Don't forget to try {} catch {} You can change FilterKind in SearchAsync(). We use FilterKind.None to get everything.

Upvotes: 1

AymenDaoudi
AymenDaoudi

Reputation: 8291

You access Contact Data on WindowsPhone using the Microsoft.Phone.UserData namespace, here's a full article about achieving that How to access contact data for Windows Phone, However if you wanna go little bit further in Creating Contacts, try the ContactStore class it has many methods that help you do what you want.

Update :

if you want to get all contacts :

  1. First declare using Microsoft.Phone.UserData;
  2. Use the following code to fire and subscribe to the search :

code :

private void ButtonContacts_Click(object sender, RoutedEventArgs e)
{
    Contacts cons = new Contacts();

    //Identify the method that runs after the asynchronous search completes.
    cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

    //Start the asynchronous search.
    cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
}

void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    /* Here use the e.Results to return an object of type QueryDataEnumerable<Microsoft.Phone.UserData.Contact> where you can enumerate through the contacts returned*/
}

Using the Asynchronous method SearchAsync with String.empty and FilterKind.None just returns all the contacts you have on your phone it returns an object of type QueryDataEnumerable<Microsoft.Phone.UserData.Contact> which you can loop through and use each contact separately.

I hope this is what you are looking for.

Update 2 :

the ContactQueryResult.GetContactsAsync() you are trying to use works with the ContactStore Class which helps you create a custom contact store for your app. When you save contacts to this store, they will appear on the phone’s People hub, integrated with the user’s other contacts ... (see full article), and I don't think that helps your case, I think using what's already mentioned in this answer , will give you the ability to get all contacts you want, and consume them as you want.

Update 3 :

use such a code in the Contacts_SearchCompleted method to get the picture of a contact

Stream s = ((Contact)e.Results.First()).GetPicture();

Upvotes: 1

Related Questions