views:

19

answers:

0

It was previously asked but I won't include it for me.

Here is the code I'm using to include users from active directory to MS sharepoint:

private static Collection GetDirectReportsInternal(string ldapBase, string userDN, out long elapsedTime) { Collection result = new Collection();

Stopwatch sw = new Stopwatch();
sw.Start();
string principalname = string.Empty;

using (DirectoryEntry directoryEntry = new DirectoryEntry(ldapBase))
{
    using (DirectorySearcher ds = new DirectorySearcher(directoryEntry))
    {
        ds.SearchScope = SearchScope.Subtree;
        ds.PropertiesToLoad.Clear();
        ds.PropertiesToLoad.Add("userPrincipalName");
        ds.PropertiesToLoad.Add("distinguishedName");
        ds.PageSize = 10;
        ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
        ds.Filter = string.Format("(&(objectCategory=user)(manager={0}))",userDN);

        using (SearchResultCollection src = ds.FindAll())
        {
            Collection<string> tmp = null;
            long subElapsed = 0;
            foreach (SearchResult sr in src)
            {
                result.Add((string)sr.Properties["userPrincipalName"][0]);
                tmp = GetDirectReportsInternal(ldapBase, (string)sr.Properties["distinguishedName"][0], out subElapsed);
                foreach (string s in tmp)
                {
                result.Add(s);
                }
            }
        }
      }
    }
sw.Stop();
elapsedTime = sw.ElapsedMilliseconds;
return result;

}