Bridge Communications

Saturday, May 21, 2016

Syncing Active Directory Departments to Skype for Business Groups - Part III

This week we will continue our series.  Last week we showed you how to get the list of matching departments related to our search.  This week we'll look at the first step in creating a Skype for Business group containing those users.  Once we have selected a department name, now we want to query AD for the users that match, this is done like this.

 private void loadUsers(Group g)
        {
            try
            {

                DirectoryEntry myLdapConnection = createDirectoryEntry(domain, false, "", user, pwd);

                DirectorySearcher search = new DirectorySearcher(myLdapConnection);

                search.Filter = "(&(objectCategory=person)(objectClass=user)(department=" + dept + "))";
                search.PropertiesToLoad.Add("department");
                search.PropertiesToLoad.Add("msRTCSIP-Line");
                search.PropertiesToLoad.Add("msRTCSIP-PrimaryUserAddress");
                search.PropertiesToLoad.Add("telephoneNumber");
                search.PropertiesToLoad.Add("mail");

                SearchResultCollection allUsers = search.FindAll();



                foreach (System.DirectoryServices.SearchResult result in allUsers)
                {
     
                    try
                    {
                    string uri = result.Properties["msRTCSIP-PrimaryUserAddress"][0].ToString();
                    Contact c = lc.ContactManager.GetContactByUri(uri);

                    try
                    {
                        if (c == null)
                        {
                            c = lc.ContactManager.GetContactByUri(result.Properties["mail"][0].ToString());
                        }
                    }
                    catch { }

                    try
                    {
                        if (c == null)
                        {
                            c = lc.ContactManager.GetContactByUri(result.Properties["telephoneNumber"][0].ToString());
                        }
                    }
                    catch { }


                        this.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            statusText.Text = "Adding " + c.Uri + " to " + g.Name;
                        }));

                       
                    addToGroup(g, c);

                    }
                    catch { }
                }

              

            }
            catch { }
        }

Some important things to note.  First we make an ldap connection the same way we did when getting the departments.  The difference is we are going to load some different properties, many of which are specific to Skype for Business users.  Once we have the msRTCSIP-PrimaryUserAddress property, we use that in conjunction with the Lync client sdk to create a contact object.  In the unlikely event we didn't find a match, we try again using the email address, or telephone number in the same manor.  This should cover the bases for users that exist in the department but perhaps not voice.  They could be users of another phone system as well, this will give us the ability to at least make an audio call to them with proper routing.

Next week we'll examine the last pieces which create the Skype for Business Group and add those members.  Following that I will post the project in Technet for download.

Doug Routledge, C# Lync, Skype for Business, SQL, Exchange, UC Developer  BridgeOC
Twitter - @droutledge @ndbridge


No comments:

Post a Comment