So if I am based in "Glasgow" and I am in the Active Directory Group "Scotland_All" - they wanted some code to update my user profile property, on a mass scale (12,000 users)..
I did some googling before i set about the task but didn't find very much so figured i'd post a quick how-to.
First I went through active directory to get all users that were a member of X group.
const string ADaddress = "domain.com";const string ADuserName = @"domain\admin";const string ADPassword = "DomainAdminPassword";const string ad_group = "Xgroup";const string domain_name = "Domain"; //not entirely neededconst string sp_site = "http://yourSharePointSite.domain.com";const string location_value = "Value";
I had to use an account and password with access to AD.
try{//Create connection to AD and get all users in specified groupPrincipalContext ctx = new PrincipalContext(ContextType.Domain,ADaddress,ADuserName,ADPassword);//Search for members in groupGroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, ad_group); //CL_France being the DL we needPrincipalSearchResultmembers = group.GetMembers(); //getting them all //Get the user names and add the domain (we are assuming that all these users will be "s7")foreach (Principal member in members){string domainUserName = domain_name + @"\" + member.SamAccountName;//Update in SharepointUpdatespProfile(domainUserName);}}catch (Exception ex){Console.WriteLine(ex); //show me the errorConsole.Read(); //pause}}
Once have the users details, I start updating their user profile. This is done using the object model so the console app was run locally. It can be done via web services but with so many users and transactions I found that simelar tasks would time out..
//take user names, check they have a user profile - if they do, update "localtion" fieldstatic void UpdatespProfile(string uName){//show user in consoleConsole.WriteLine(uName);//member.SamAccountName);try{using (SPSite site = new SPSite(sp_site)){//Connect to user manager and pass in user nameUserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(site));UserProfile userProfile = profileManager.GetUserProfile(uName);//check if we have a valid user profileif (userProfile != null){//Update location fielduserProfile["Location"].Value = location_value;//update the user profileuserProfile.Commit();//show it worked in consoleConsole.WriteLine("Updated Location to" +" " + location_value);}else{//show we didn't have a valid user profile in consoleConsole.WriteLine("No user profile");}}}catch (Exception ex){//show the errorConsole.WriteLine(ex);//pauseConsole.Read();}}
Works very nicely. Hope someone out there finds it useful.