Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Contacts.SearchAsync Method

July 28, 2014

Asynchronously searches for contacts in the user’s contact data.

Namespace:  Microsoft.Phone.UserData
Assembly:  Microsoft.Phone (in Microsoft.Phone.dll)

public void SearchAsync(
	string filter,
	FilterKind filterKind,
	Object state
)

Parameters

filter
Type: System.String
The filter to use to search for contacts.
filterKind
Type: Microsoft.Phone.UserData.FilterKind
The kind of filter to use when searching for contacts.
state
Type: System.Object
A user-defined object that contains information about the operation.

If the filterKind is EmailAddress, PhoneNumber, or DisplayName, then filter should contain a string to attempt to match. The filter parameter is ignored if the filterKind is None or PinnedToStart. For more information about how the filter is used to match contacts, see Contact filtering and matching for Windows Phone 8.

The state object is stored in the ContactsSearchEventArgs class and passed to the SearchCompleted delegate when the operation is complete.

Applications that use the Contacts APIs may fail to start. This can occur for applications that are upgraded from Windows Phone OS 7.0 to Windows Phone OS 7.1. To resolve this issue, open the file WMAppManifest.xml and add the following element.

<Capability Name="ID_CAP_CONTACTS"/>

The following example assumes that you have a Windows Phone application that has a page with a button named SearchContacts. The code assumes that you have a databound list box named ContactResultsData. For the full example, including the XAML, see How to access contact data for Windows Phone 8.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using Microsoft.Phone.UserData;

namespace ContactsAndCalendarTestApp
{
    public partial class ContactsPage : PhoneApplicationPage
    {
        // Constructor
        public ContactsPage()
        {
            InitializeComponent();
        }


        private void SearchContacts_Click(object sender, RoutedEventArgs e)
        {
            ContactResultsData.DataContext = null;

            Contacts cons = new Contacts();

            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

            cons.SearchAsync(“A”, FilterKind.DisplayName, "Contacts Test #1");
        }


        void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            //MessageBox.Show(e.State.ToString());

            try
            {
                //Bind the results to the list box that displays them in the UI.
                ContactResultsData.DataContext = e.Results;
            }
            catch (System.Exception)
            {
                //That's okay, no results.
            }
        }
    }
}

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2014 Microsoft