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