Getting Lync Contact List

From Logic Wiki
Jump to: navigation, search


Getting Contact List Diagram

LyncContacts.png


Searching People Diagram

LyncSearch.jpg

Prerequisities

Lync Client SDK must be installed first. It can only be installed on 64 bit Office. If you have a 32 bit office you have to uninstall it and run fixit (a microsoft tool and can be downloaded and installed free from MS web site). Office 64 bit then can be installed after a restart.

Sample code

https://msdn.microsoft.com/EN-US/library/office/dn391639.aspx

To search for people in the UCS address book

  1. Get the string used as the name search criteria.
  2. Call the ContactManager.BeginSearch method.
  3. In a lambda expression or a callback method passed in the second argument of BeginSearch, get the results of the search by calling the ContactManager.EndSearch method.
  4. A Microsoft.Lync.Model.SearchResults object is returned.
  5. Get the number of people in the search results by reading the SearchResults.ContactsCount property. If the number is zero, nobody in the address book matched the search criteria.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


using Microsoft.Lync.Model;
using Microsoft.Lync.Model.Conversation;


namespace LyncClientTest
{
    public partial class Form1 : Form
    {

        private LyncClient lyncClient;
        private List<Contact> _FoundContacts = new List<Contact>();
        private List<String> FoundUris = new List<String>();

        public List<Contact> ResultContactArray
        {
            get
            {
                return this._FoundContacts;
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _FoundContacts.Clear();
            Connect();
             SignIn();
             GetList();

        }

        public void Connect()
        {

            try
            {
                lyncClient = LyncClient.GetClient();
            }
            catch (ClientNotFoundException clientNotFoundException)
            {
                Console.WriteLine(clientNotFoundException);
                return;
            }
            catch (NotStartedByUserException notStartedByUserException)
            {
                Console.Out.WriteLine(notStartedByUserException);
                return;
            }
            catch (LyncClientException lyncClientException)
            {
                Console.Out.WriteLine(lyncClientException);
                return;
            }
            catch (SystemException systemException)
            {
                
                    throw;
                 
            }
        }


        public void SignIn()
        {
            if (lyncClient.State != ClientState.SignedIn)
            {
                lyncClient.BeginSignIn(null, "ROCHDALE_MBC\\AIAdmin", "C1pralex6+", SignInCallback, null);
            }
        }

        public void GetList()
        {
            lyncClient.ContactManager.BeginSearch(txtSearch.Text, SearchResultsCallback, lyncClient.State);

        }



        public void SearchResultsCallback(IAsyncResult ar)
        {
            SearchResults results = null;
            results = lyncClient.ContactManager.EndSearch(ar);

            if (results == null)
            {
                return;
            }

            if (results.AllResults.Count == 0)
            {
                MessageBox.Show("0 instances found for " + ar.AsyncState.ToString());
                return;
            }
            foreach (SearchResult searchResult in results.AllResults)
            {

                if (searchResult.Result is Contact)
                {
                    Contact foundContact = searchResult.Result as Contact;

                    //array for use of list box

                    _FoundContacts.Add(foundContact);
                
                }
            }
        }
        private void SignInCallback(IAsyncResult result)
        {
            try
            {
                lyncClient.EndSignIn(result);
            }
            catch (LyncClientException e)
            {
                Console.WriteLine(e);
            }
            catch (SystemException systemException)
            {
               
                    // Rethrow the SystemException which did not come from the Lync Model API.
                    throw;
                 
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            _FoundContacts.ForEach(t => listBox1.Items.Add(t.Uri));
        }
    }
}