Get All Contacts using Lync ContactManager
Right now I'm using the the LyncClient.ContactManager.BeginSearch method to find contacts. However, I haven't been able to figure out ho开发者_运维问答w 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");
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.
}
}
精彩评论