Contacts.SearchAsync Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Asynchronously searches for contacts in the user’s contact data.
Namespace: Microsoft.Phone.UserData
Assembly: Microsoft.Phone (in Microsoft.Phone.dll)
Syntax
Public Sub SearchAsync ( _
filter As String, _
filterKind As FilterKind, _
state As Object _
)
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.
Remarks
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"/>
Examples
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.
}
}
}
}
Imports Microsoft.Phone.UserData
Partial Public Class ContactsPage
Inherits PhoneApplicationPage
Public Sub New()
InitializeComponent()
End Sub
Private Sub SearchContacts_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
ContactResultsData.DataContext = Nothing
Dim cons As Contacts = New Contacts()
AddHandler cons.SearchCompleted, AddressOf Contacts_SearchCompleted
cons.SearchAsync("A", FilterKind.DisplayName, "Contacts Test #1")
End Sub
Private Sub Contacts_SearchCompleted(sender As Object, e As ContactsSearchEventArgs)
Try
'Bind the results to the list box that displays them in the UI.
ContactResultsData.DataContext = e.Results
Catch ex As System.Exception
'That's okay, no results.
End Try
End Sub
End Class
Version Information
Windows Phone OS
Supported in: 8.1, 8.0, 7.1
Platforms
Windows Phone
See Also
Reference
Microsoft.Phone.UserData Namespace
Other Resources
Contacts and Calendar for Windows Phone 8