Right now I'm using the the LyncClient.ContactManager.BeginSearch method to find contacts. However, I haven't been able to figure out how to get all the contacts. I've tried passing "*" and "%" as wild-card characters but that has not worked. Right now here is my function call.
_lyncClient.ContactManager.BeginSearch("*", SearchProviders.GlobalAddressList, SearchFields.DisplayName, SearchOptions.ContactsOnly, 400, SearchCallback, "Searching Contacts");
The contact manager lets you manage a user’s contact list as it appears in the Lync 2013 client and your own custom client. Use the contact manager to search for contacts and groups that are not in the contact list, add contacts or a distribution group, rename custom groups, add new custom groups, or remove a custom group.
Open Lync, and, in your Contacts list, right-click any group name (for example, Frequent Contacts ), click Create New Group, and then name the group. To add people to the new group, search for a contact, point to the contacts name in the search results, and then click the plus sign (+).
In the search box in the Contacts view of the Lync main window, type the name, email address, or phone number of the person you’re looking for. Can I have Skype contacts?
To unpin a contact, right-click, and then click Unpin from Frequent Contacts. When you create a group in Lync, it's called a contact group because it organizes your contacts in groups that are meaningful to you. You can IM them, email them, and set up meetings with them. You might have heard of Microsoft 365 Groups.
Lync contacts are organised into groups, so you need to start at the Groups level. Once you've got a group, you can then enumerate through it's Contacts
foreach(var group in _client.ContactManager.Groups)
{
foreach (var contact in group)
{
MessageBox.Show(contact.Uri);
}
}
This article is good for background, and more advanced features
Edit: Specifically, for the distribution groups expansion question, I think the sample here is flawed.
Instead of calling BeginExpand and waiting on the WaitHandle, provide a callback method to handle the Expand callback. So, instead of:
asyncOpResult = DGGroup.BeginExpand(null, null);
asyncOpResult.AsyncWaitHandle.WaitOne();
DGGroup.EndExpand(asyncOpResult);
try this:
...
asyncOpResult = DGGroup.BeginExpand(ExpandCallback, DGGroup);
...
public void ExpandCallback(IAsyncResult ar)
{
DistributionGroup DGGroup = (DistributionGroup)ar.AsyncState;
DGGroup.EndExpand(ar);
etc...
}
This works perfectly for me.
I ended up doing multiple searches for now to get all the contacts. I go through each letter of the alphabet to find them. The load time is quick enough and I'll just show a loading image on the grid for a little while when it fires up. This worked well for the 200 or so contacts we have though I would recommend Paul's solution for 150 or less. Here is what I did:
private static char[] Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
...
public void GetAllContacts()
{
int initialLetterIndex = 0;
_lyncClient.ContactManager.BeginSearch(
Alphabet[initialLetterIndex].ToString();
SearchProviders.GlobalAddressList,
SearchFields.FirstName,
SearchOptions.ContactsOnly,
300,
SearchAllCallback
new object[] { initialLetterIndex, new List<Contact>() }
);
}
private void SearchAllCallback(IAsyncResult result)
{
object[] parameters = (object[])result.AsyncState;
int letterIndex = (int)parameters[0] + 1;
List<Contact> contacts = (List<Contact>)parameters[1];
SearchResults results = _lyncClient.ContactManager.EndSearch(result);
contacts.AddRange(results.Contacts);
if (letterIndex < Alphabet.Length)
{
_lyncClient.ContactManager.BeginSearch(
Alphabet[letterIndex].ToString(),
SearchAllCallback,
new object[] { letterIndex, contacts }
);
}
else
{
//Now that we have all the contacts
//trigger an event with 'contacts' as the event arguments.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With