locked
Presence unknown RRS feed

  • Question

  • Hi,

    I create the application which loads the list of all contacts on Lync server.

    private delegate void ChangeDataSourse(List<Contact> contacts);
    private
    LyncClient _lyncClient = null; private static char[] Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); public TestWinForm() { InitializeComponent(); } private void TestWinForm_Load(object sender, EventArgs e) { _lyncClient = LyncClient.GetClient(); GetAllContacts(); } public void GetAllContacts() { int initialLetterIndex = 0; _lyncClient.ContactManager.BeginSearch ( Alphabet[initialLetterIndex].ToString(), SearchProviders.ExchangeService, 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 FillGrid(contacts); } private void FillGrid(List<Contact> contacts) { if (userGrid.InvokeRequired) { ChangeDataSourse chDS = new ChangeDataSourse(FillGrid); userGrid.Invoke(chDS, new object[] { contacts }); } else { DataTable table = new DataTable(); table.Columns.Add("ContactName", typeof(string)); table.Columns.Add("ContactState", typeof(string)); table.Columns.Add("Uri", typeof(string)); foreach (Contact contact in contacts.OrderBy(x => x.GetContactInformation(ContactInformationType.DisplayName))) { DataRow row = table.NewRow(); row["ContactName"] = contact.GetContactInformation(ContactInformationType.DisplayName).ToString(); row["ContactState"] = contact.GetContactInformation(ContactInformationType.Activity).ToString(); row["Uri"] = contact.Uri; table.Rows.Add(row); contact.ContactInformationChanged += new EventHandler<ContactInformationChangedEventArgs>(contact_ContactInformationChanged); } userGrid.DataSource = table; userGrid.Columns["ContactName"].HeaderText = "Contact name"; userGrid.Columns["ContactState"].HeaderText = "Contact state"; userGrid.Columns["Uri"].Visible = false; } }

    But if contact not in my contact list(Lync), its status "Presence unknown". Then I open Lync, find this contact and status in userGrid changes from "Presence unknown" on his real status.

    How to make that when I start app, real statuses were loaded?

     

     


    • Edited by rpirozhkov Wednesday, February 1, 2012 7:08 AM
    Wednesday, February 1, 2012 7:07 AM

Answers

  • The Lync client platform subscribes to presence information for all contact objects that a user has added to his contact list. Any contact that you find programatically that is NOT in the contact list, you need to subscribe by:

    1) create a ContactSubscription object (One-to-many relationship between a ContactSubscription and Contact object instances). 

    2) add the contact information types you are interested in.

    3) add each contact to the new ContactSubscription created in step 1.

    4) call Subscribe on the contact subscription.

    5) handle Contact.ContactInformationChanged event that is raised when any of the contacts that you have added to your subscription have re-published presence.

    You do not need to add the contact to a group. Adding a contact to a group forces the Lync client to add the contact to the contact list.  It now becomes a permenent member of the contact list and visible to the user on the Lync client.  This may not be ideal.  In addition, you would have to remove the contact from the group when you no longer need to use the contact.

    Friday, February 3, 2012 8:28 PM

All replies

  • Hi,

    You should first sign in to lync in your application to make sure that All contacts is got from the Lync Server

    You could use this methods

    List<Contact> MyContacts = new List<Contact>();
    Contact _self;
    string LyncStatus;
    string MyStatus;
            
    void SignIn()
            {
                _lyncClient.BeginSignIn(
                    null,
                    null,
                    null,
                    result =>
                    {
                        if (result.IsCompleted)
                        {
                            _lyncClient.EndSignIn(result);
    
                            this.Dispatcher.Invoke(new Action(() => PerformApplicationStartupLogic()));
                        }
                    },
                    "Local user signing in" as object);
            }
    
            void PerformApplicationStartupLogic()
            {            
                _contactManager = _lyncClient.ContactManager;
                _self = _lyncClient.Self;
    
    
                this.MyContacts = new List<Contact>();
                foreach (var group in _contactManager.Groups)
                {
                    foreach (var contact in group)
                    {
                        if (!this.MyContacts.Contains(contact))
                        {
                            this.MyContacts.Add(contact);
                            contact.GetContactInformation(ContactInformationType.DisplayName);
                        }
                    }
                }
    
                this.LyncStatus = _lyncClient.State.ToString();
                this.MyStatus = _self.Contact.GetContactInformation(ContactInformationType.Activity).ToString();
            }
    


    I hope this helps

    Thanks,

    Mohamed A. Sakr

    • Proposed as answer by MohamedSakr Thursday, February 2, 2012 8:14 AM
    Thursday, February 2, 2012 8:14 AM
  • Hi,

    thanks fo reply. But after

    _lyncClient = LyncClient.GetClient();

    _lyncClient.State is SignedIn and there is no necessity to call BeginSignIn.

    Also your get contats only in my contact list, but i need to load contacts from lync server. In my lync's contact list i have 3 contacts, but on the server several hundreds contacts. And when I load its, the status "Presence unknown"

    Thursday, February 2, 2012 9:36 AM
  • You need to search contacts (as you would in client itself), and than you need to add them to your group. Also, you need to subscribe to that contacts presence events to receive information when client updates info from server.

     

    search:

    Contact foundContact = _lyncClient.ContactManager.GetContactByUri(URI);

    add to group:

    groupAdd.ContactAdded += group_ContactAdded;
    IAsyncResult ar = groupAdd.BeginAddContact(contactToAdd, null, null);

    Thursday, February 2, 2012 9:53 AM
  • You should wait until the _lyncClient != null also you should wait until it has contacts (Yous Sleep)

    Another solution use While loop till it loads the Contacts

     

    If you need to load contacts from lync server you should get these enabled users from the Active Directory Using LDAP Query

     

    Thanks

    • Proposed as answer by MohamedSakr Thursday, February 9, 2012 9:44 AM
    Thursday, February 2, 2012 10:54 AM
  • The Lync client platform subscribes to presence information for all contact objects that a user has added to his contact list. Any contact that you find programatically that is NOT in the contact list, you need to subscribe by:

    1) create a ContactSubscription object (One-to-many relationship between a ContactSubscription and Contact object instances). 

    2) add the contact information types you are interested in.

    3) add each contact to the new ContactSubscription created in step 1.

    4) call Subscribe on the contact subscription.

    5) handle Contact.ContactInformationChanged event that is raised when any of the contacts that you have added to your subscription have re-published presence.

    You do not need to add the contact to a group. Adding a contact to a group forces the Lync client to add the contact to the contact list.  It now becomes a permenent member of the contact list and visible to the user on the Lync client.  This may not be ideal.  In addition, you would have to remove the contact from the group when you no longer need to use the contact.

    Friday, February 3, 2012 8:28 PM
  • Hi John,

    Your approach of subscribing to status change works perfectly, but only when contact status is changed. On initial load, I still get the status as Presence unknown. 

    Can you kindly help me on the scenario when I am required to load the status of contacts not in my contact list at startup itself and not when they change their status?

    Monday, April 16, 2018 5:02 AM
  • Did you ever find a solution to your problem rpirozhkov?

    I am on the same ship which you sailed 6 years back. Wanted to know if you were able to get the status of contacts at initial load?




    • Edited by raj16587 Monday, April 16, 2018 5:37 AM
    Monday, April 16, 2018 5:36 AM