Search code examples
c#lync-2010lync

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 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");

Solution

  • 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.