Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
        Console.ReadKey();

        UserPrincipal findAllUser = new UserPrincipal(ctx); 

        PrincipalSearcher ps = new PrincipalSearcher(findAllUser);

        try
        {
            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
                 {
                     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);
                                 Console.ReadKey();

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

                             }
                         }

                     }
                 });
        }
        catch (Exception exx)
        {
            Console.WriteLine(exx.Message);
            Console.WriteLine(exx.StackTrace);
            Console.ReadKey();
        }
        Console.ReadLine();
    }
share|improve this question
add comment

1 Answer

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
1  
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
1  
Hope you could figure out the problems. Share you findings if you can. –  Anatoly Mironov Mar 8 '12 at 16:12
show 1 more comment

Your Answer

 
discard

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.