Cookie CSS

Saturday, May 14, 2016

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

This week we will take a look at the screen layout for our, and look at the process involved in querying active directory.




Here is what I have chosen for our screen layout.  You can see our AD credentials stored at the top.  To the left we have our AD department search, the matching results, the corresponding Skype for Business Group name to create, and a button to begin the process.  On the right we show our existing Skype for Business groups, and at the bottom we have some progress information to let us know what is happening during the add process.

Getting the AD Departments

So get the AD departments we will use some code like this.

private void loadDepartments()
        {
            try
            {
                string user = adUser.Text;
                string pwd = adPwd.Password;
                string domain = adDomain.Text;

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

                DirectorySearcher search = new DirectorySearcher(myLdapConnection);

                search.Filter = "(&(objectCategory=person)(objectClass=user)(department=*" + adSearchbox.Text + "*))";
                search.PropertiesToLoad.Add("department");

                SearchResultCollection allUsers = search.FindAll();

                List<string> depts = new List<string>();

                foreach (System.DirectoryServices.SearchResult result in allUsers)
                {
                    try
                    {
                        string dept = result.Properties["department"][0].ToString();

                        if (!depts.Contains(dept))
                            depts.Add(dept);
                    }
                    catch { }
                }

                adDepts.ItemsSource = depts;

            }
            catch { }
        }

The code to create the directory entity looks something like this.

private DirectoryEntry createDirectoryEntry(string domain, bool secure, string path, string user, string pwd)
        {

            if (domain.Contains("."))
            {
                path = "DC=" + domain.Split('.')[0] + ",DC=" + domain.Split('.')[1].Replace(".", "");
            }
            else
            {
                path = "DC=" + domain + ",DC=com";
            }

            DirectoryEntry ldapConnection;
         
            ldapConnection = new DirectoryEntry("LDAP://" + domain, user, pwd);

            if (!string.IsNullOrEmpty(path))
            {
                ldapConnection.Path = "LDAP://" + domain + "/" + path;
            }

            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;

            try
            {
                if (secure)
                {
                    ldapConnection.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
                }
            }
            catch { }

            return ldapConnection;
        }

Next week we will look at how to get the users from our selected AD Group.

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



No comments:

Post a Comment

Any spam comments will be deleted and your user account will be disabled.