0
votes

I have an EmployeeID column in my Sql-Table, which is also synchronized with a SharePoint list created in SharePoint Designer from “External Content Type”. Active Directory accounts at our company also have an EmployeeID field. As the page loads, how can I read the users Login name, query the Active Directory for the EmployeeID and filter the list using the retrieved EmployeID in SharePoint foundation.

P.S. I was able to do this with a Silverlight Webpart and a WCF Servie but I need to do this in native SharePoint. 

1

1 Answers

0
votes

Here's code that I use to query AD:

public class ADHelper
{
        public static Employee getUserName(string loginName)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            UserPrincipal principal = new UserPrincipal(ctx);
            principal.SamAccountName = Regex.Replace(loginName, ".*\\\\(.*)", "$1", RegexOptions.None);

            // create your principal searcher passing in the QBE principal    
            PrincipalSearcher srch = new PrincipalSearcher(principal);

            // find all matches
            Employee employee = new Employee();
            foreach (var found in srch.FindAll())
            {
                UserPrincipal principal2 = (UserPrincipal)found;
                //FirstName
                employee.FirstName = principal2.GivenName;
                //Middle Name
                employee.MiddleName = principal2.MiddleName;
                //LastName
                employee.LastName = principal2.Surname;
            }
            return employee;
        }
    }

    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
        public string getFullName()
        {
            return LastName + ", " + FirstName + " " + MiddleName;
        }       
    }

And this is how I call it from SharePoint:

ADHelper.getUserName(SPContext.Current.Web.CurrentUser.LoginName);