public void search(string searchCriteria) { string[] args = JSON.JsonHelper.Deserialize <string[]>(searchCriteria); ContactSearchParams searchParams = new ContactSearchParams(); try { searchParams.fields = JSON.JsonHelper.Deserialize <string[]>(args[0]); searchParams.options = JSON.JsonHelper.Deserialize <SearchOptions>(args[1]); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, INVALID_ARGUMENT_ERROR)); return; } if (searchParams.options == null) { searchParams.options = new SearchOptions(); searchParams.options.filter = ""; searchParams.options.multiple = true; } DeviceContacts deviceContacts = new DeviceContacts(); deviceContacts.SearchCompleted += new EventHandler <ContactsSearchEventArgs>(contacts_SearchCompleted); // default is to search all fields FilterKind filterKind = FilterKind.None; // if only one field is specified, we will try the 3 available DeviceContact search filters if (searchParams.fields.Count() == 1) { if (searchParams.fields.Contains("name")) { filterKind = FilterKind.DisplayName; } else if (searchParams.fields.Contains("emails")) { filterKind = FilterKind.EmailAddress; } else if (searchParams.fields.Contains("phoneNumbers")) { filterKind = FilterKind.PhoneNumber; } } try { deviceContacts.SearchAsync(searchParams.options.filter, filterKind, searchParams); } catch (Exception ex) { Debug.WriteLine("search contacts exception :: " + ex.Message); } }
public void search(string searchCriteria) { string[] args = JSON.JsonHelper.Deserialize<string[]>(searchCriteria); ContactSearchParams searchParams = new ContactSearchParams(); try { searchParams.fields = JSON.JsonHelper.Deserialize<string[]>(args[0]); searchParams.options = JSON.JsonHelper.Deserialize<SearchOptions>(args[1]); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, INVALID_ARGUMENT_ERROR)); return; } if (searchParams.options == null) { searchParams.options = new SearchOptions(); searchParams.options.filter = ""; searchParams.options.multiple = true; } DeviceContacts deviceContacts = new DeviceContacts(); deviceContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted); // default is to search all fields FilterKind filterKind = FilterKind.None; // if only one field is specified, we will try the 3 available DeviceContact search filters if (searchParams.fields.Count() == 1) { if (searchParams.fields.Contains("name")) { filterKind = FilterKind.DisplayName; } else if (searchParams.fields.Contains("emails")) { filterKind = FilterKind.EmailAddress; } else if (searchParams.fields.Contains("phoneNumbers")) { filterKind = FilterKind.PhoneNumber; } } try { deviceContacts.SearchAsync(searchParams.options.filter, filterKind, searchParams); } catch (Exception ex) { Debug.WriteLine("search contacts exception :: " + ex.Message); } }
private void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e) { ContactSearchParams searchParams = (ContactSearchParams)e.State; List <Contact> foundContacts = null; // if we have multiple search fields if (searchParams.options.filter.Length > 0 && searchParams.fields.Count() > 1) { foundContacts = new List <Contact>(); if (searchParams.fields.Contains("emails")) { foundContacts.AddRange(from Contact con in e.Results from ContactEmailAddress a in con.EmailAddresses where a.EmailAddress.Contains(searchParams.options.filter) select con); } if (searchParams.fields.Contains("displayName")) { foundContacts.AddRange(from Contact con in e.Results where con.DisplayName.Contains(searchParams.options.filter) select con); } if (searchParams.fields.Contains("name")) { foundContacts.AddRange(from Contact con in e.Results where con.CompleteName != null && con.CompleteName.ToString().Contains(searchParams.options.filter) select con); } if (searchParams.fields.Contains("phoneNumbers")) { foundContacts.AddRange(from Contact con in e.Results from ContactPhoneNumber a in con.PhoneNumbers where a.PhoneNumber.Contains(searchParams.options.filter) select con); } if (searchParams.fields.Contains("urls")) { foundContacts.AddRange(from Contact con in e.Results from string a in con.Websites where a.Contains(searchParams.options.filter) select con); } } else { foundContacts = new List <Contact>(e.Results); } //List<string> contactList = new List<string>(); string strResult = ""; IEnumerable <Contact> distinctContacts = foundContacts.Distinct(); foreach (Contact contact in distinctContacts) { strResult += FormatJSONContact(contact, null) + ","; //contactList.Add(FormatJSONContact(contact, null)); if (!searchParams.options.multiple) { break; // just return the first item } } PluginResult result = new PluginResult(PluginResult.Status.OK); result.Message = "[" + strResult.TrimEnd(',') + "]"; DispatchCommandResult(result); }