0

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();
    }

1 Answer 1

4

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, "");
6
  • 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
    Commented Mar 8, 2012 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 Commented Mar 8, 2012 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
    Commented Mar 8, 2012 at 14:05
  • Then you have to add users to some group in SharePoint.. I'll update my post Commented Mar 8, 2012 at 14:27
  • 1
    Hope you could figure out the problems. Share you findings if you can. Commented Mar 8, 2012 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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