My network admin have deactivated a lot of users from the Active Directory, and recreated them using the same username but with a different OU, Now the SP 2010 does not recognize these users or their Permissions unless i manually Remove them from the "Members" group i created and manually add them back (then they get recognized noticeable by their display name changing).
Since i have a lot of different site collections which these users can be in any one of them i tried using the object model to go through all the site collections under the web application and remove and re-add each user permissions like so:
SPSite site = new SPSite("http://localhost:47700/Divisions/100/");
SPWebApplication webApp = site.WebApplication;
foreach (SPSite currentSiteCollection in webApp.Sites)
{
// user is struct i created to keep the user info.
List<user> members = new List<user>();
SPWeb web = currentSiteCollection.RootWeb;
try
{
SPGroup membersGroup = web.SiteGroup["Members"];
foreach (SPUser currentUser in membersGroup.Users)
{
user userData;
userData.login = currentUser.LoginName;
userData.email = currentUser.Email;
userData.name = currentUser.Name;
userData.notes = currentUser.Notes;
members.Add(userData);
}
foreach (user currentMember in members)
{
membersGroup.Users.Remove(currentMember.login);
}
membersGroup.Update();
foreach (user currentMember in members)
{
membersGroup.Users.Add(currentMember.login, currentMember.email, currentMember.name, currentMember.notes);
}
membersGroup.Update();
}
catch (Exception ex)
{
throw ex;
}
For some reason it dosen't work. Does anyone have any idea why it doesn't work? How to make it work? or any better idea of how to to it?
Thanks in advance, Itay.