Partager via


Quick Fix for UpdateUsers() in SPUserUtil

I've seen this problem reported at least 3 or 4 times in the past moth, so I thought I would provide this Quick Fix until I can complete the changes for the next release of SPUserUtil.

The basic problem is described as follows:

When using the -o update operation with the -adu switch, this causes SPUserUtil to query Active Directory to get the current Email and DisplayName attributes to update the account with. The problem, is that if SPUserUtil encounters an error on a user (say by trying to query on a user that no longer exists in AD) it just gives the error message 'User cannot be found. Error in ADUpdateUsers', then bails out rather than just continuing to the next user.

The fix was to move the try/catch block within the foreach() loop of the UpdateUsers() method in the WebUserUtil.cs file.

If you would like to update your copy of SPUserUtil until I get the next update complete, simply replace that method with the following:

public int UpdateUsers(SPSite site)
{
string sUserLoginName;
string sSid;
string sDisplayName;
string sEmail;

Logging.Information("Updating User information...");

foreach (UserMapItem uMap in UserMapItems())
{
try
{
sUserLoginName=null;
sDisplayName=null;
sEmail=null;
sSid=null;
sUserLoginName = uMap.LoginName;
sSid = uMap.Sid;

// Get the user based on the old login name
SPUser user = site.RootWeb.AllUsers[sUserLoginName];

if(!this.m_Options.bADUpdate)
{
// Update from MapFile
Logging.Information("Getting updated user information from map file...");
sDisplayName = uMap.DisplayName;
sEmail = uMap.Email;

}
else
{
// Update from AD.
Logging.Information("Getting updated user information from Active Directory...");
// Get the new user properties from AD using SID
string sByteString;
sByteString = SIDUtil.ConvertStringSidToSidByteString(sSid);

DirectoryEntry rDE = null;

if(this.m_Options.strGCLdapCtx != null)
{
Console.WriteLine(" Connecting to GC://" + this.m_Options.strGCLdapCtx);
DirectoryEntry gcDE = new DirectoryEntry("GC://" + this.m_Options.strGCLdapCtx);
System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(gcDE);
mySearcher.ReferralChasing = System.DirectoryServices.ReferralChasingOption.All;
if(m_Options.bADSHUpdate)
{
mySearcher.Filter = "(sIDHistory=" + sSid + ")";
Console.WriteLine( " Searching for (sIDHistory=" + sSid + ")");
}
else
{
mySearcher.Filter = "(objectSID=" + sSid + ")";
Console.WriteLine( " Searching for (objectSID=" + sSid + ")");
}

foreach(System.DirectoryServices.SearchResult result in mySearcher.FindAll())
{
rDE = result.GetDirectoryEntry();
Console.WriteLine( result.GetDirectoryEntry().Path );
}
}
else
{
try
{
if(m_Options.bADSHUpdate)
{
// KRICHIE: TODO: NEED TO IMPLEMENT FOR SIDHISTORY
rDE = new DirectoryEntry("LDAP://<SID=" + sByteString + ">");
Console.WriteLine( " Search for LDAP://<SID=" + sByteString + ">");
}
else
{
rDE = new DirectoryEntry("LDAP://<SID=" + sByteString + ">");
Console.WriteLine( " Search for LDAP://<SID=" + sByteString + ">");
}
}
catch(Exception e)
{
Logging.Warning("Cound not find user using LDAP://<SID=" + sByteString + ">");
Logging.Warning(e.Message);
}
}

if(rDE == null)
Logging.Warning("Unable to find reference for " + sSid);
else
{

sDisplayName = (string)rDE.Properties["displayName"].Value;
sEmail = (string)rDE.Properties["mail"].Value;
}
}

if(sDisplayName == null || sEmail == null)
{
Logging.Warning("Not updating " + sUserLoginName + ". DisplayName (" + sDisplayName + ") or mail attribute (" + sEmail + ") is null.");
}
else
{
user.Name = sDisplayName;
user.Email = sEmail;
user.Update();
}
user = null;
// Get the user again based on new login name
user = site.RootWeb.AllUsers[sUserLoginName];
Logging.Trace("New information for " + sUserLoginName);
Logging.Trace(m_strDivider);
Logging.Trace(" LoginName : " + user.LoginName);
Logging.Trace("DisplayName : " + user.Name);
Logging.Trace(" Email : " + user.Email);
Logging.Trace(" Notes : " + user.Notes);
Logging.Trace("");

}
catch (Exception e)
{
// Let the user know what went wrong.
Logging.Exception("Error in UpdateUsers", e);
}
}
return 0;
}

HTH - Keith

Comments

  • Anonymous
    March 08, 2006
    In my previous post ( SharePoint Utility Suite 2.1 - Coming Soon&amp;nbsp;) I noted that It will include...
  • Anonymous
    March 10, 2006
    Thanks Keith, it worked.

    If anyone else using this code, please note there is one extra ]; in SPUser user = site.RootWeb.AllUsers[sUserLoginName];]; which need to be removed.
  • Anonymous
    March 10, 2006
    Good catch Nishant (Darn editor!!! :))
    I corrected the post
  • Anonymous
    March 25, 2006

    I’ve decided that I’m not going to put much more effort into the 2.5 incarnation of SPUserUtil for...