I have to read data from an external source which contains names inside an XML-structure.
I have to map these names to users from an active directory. This is part of the code:
foreach (SPUser user in site.RootWeb.Users.Cast<SPUser>().Where(user => user.Name.Equals(displayName)))
{
ret = user.ID;
return;
}
foreach (SPGroup group in site.RootWeb.Groups)
{
foreach (SPUser groupUser in group.Users)
{
if (groupUser.IsDomainGroup)
{
bool reachedMaxCount;
using (SPWeb web = site.RootWeb)
{
string adGroup = groupUser.LoginName;
SPPrincipalInfo[] users = SPUtility.GetPrincipalsInGroup(web, adGroup, limit, out reachedMaxCount);
if (users != null)
{
foreach (SPPrincipalInfo userPrinciple in users)
{
if (userPrinciple.DisplayName.Equals(displayName))
{
ret = site.RootWeb.EnsureUser(userPrinciple.LoginName).ID;
return;
}
}
}
}
}
}
}
});
return ret;
Because we have to map the user only by his name this can lead to problems, which did: One user was twice inside the active directory.
We deleted the "bad" one inside the active directory. But while this user cannot be seen anymore in SharePoint, the code above still collects that deleted user and is able to save it as a value from a "people picker" field.
How can this be solved?