SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for a script or code to make Sharepoint know 'bout users from active directory.. I have 3 domains and People Picker sees people but cannot resolve that (suggest only).. I read in many place that to EnsureUser, Sharepoint must know 'bout them so that if I manually add user to sharepoint it works..

How can I do to add entire Active Directory users to SP?

This is my actual code:

 public static void Import(string username, string domain,string completeDomain)
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, completeDomain);
        Console.WriteLine(ctx.Name + " --> " + ctx.UserName + " --> " + ctx.ConnectedServer);

        UserPrincipal findAllUser = new UserPrincipal(ctx); 

        PrincipalSearcher ps = new PrincipalSearcher(findAllUser);

                     using (SPSite elevatedSite = new SPSite(GetUrl()))
                         using (SPWeb web = elevatedSite.OpenWeb())
                             foreach (Principal user in ps.FindAll())
                                 web.AllowUnsafeUpdates = true;
                                 elevatedSite.AllowUnsafeUpdates = true;

                                 Console.WriteLine(user.Name + "-->" + user.DistinguishedName);

                                 var loginName = domain + "\\" + user.SamAccountName;
                                 var name = user.DisplayName;
                                 web.AssociatedVisitorGroup.Users.Add(loginName, string.Empty, name, string.Empty);


        catch (Exception exx)
share|improve this question
up vote 4 down vote accepted

If you just want to add all users, just add NT AUTHORITY\authenticated users

If you want to add every user individually, you have to get users (maybe as Principals) from AD, and add them to a group in SharePoint.

var principal...
var loginName = @"MYDOMAIN\" + principal.SamAccountName;
var name = principal.DisplayName;
web.AssociatedMemberGroup.Users.Add(loginName, "", name, "");
share|improve this answer
Even adding NT Authority\authenticated users, People Picker cannot solve immediatly names and Ensure User wont work... To make it work i need to "introduce personally" every single user from my AD domains to SP – Ziba Leah Mar 8 '12 at 9:04
Allright, you can iterate over all ad groups, get members and add them to SharePoint. Check out my blog post about ou, but write about getting members from ad groups as well – Anatoly Mironov Mar 8 '12 at 9:18
I can get users from AD thanks to you... But in Principal form.. How can i convert this to SPUser?? Or SPPrincipal... – Ziba Leah Mar 8 '12 at 14:05
Then you have to add users to some group in SharePoint.. I'll update my post – Anatoly Mironov Mar 8 '12 at 14:27
Hope you could figure out the problems. Share you findings if you can. – Anatoly Mironov Mar 8 '12 at 16:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.