I ran into this same issue, and searched high and low for an answer. I came across this link which pointed me in the right direction:
http://www.sharepoint-tips.com/2006_06_01_archive.htmlScroll down to Tuesday June 27th 2006. The code example there uses the ParentWeb.Users collection. I could not get that working, so I used the AllUsers collection, and that did work.
I have since created a custom action that does a lookup based on the email.
The code looks something like this:
Code Snippet
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
SPSite site = __Context.Site;
SPWeb web = __Context.Web;
string errorString = "empty.";
bool allowit = web.AllowUnsafeUpdates;
try
{
// Do nothing if the field value to assign is empty.
if (this.FieldValue != "")
{
//Assign the guid for the list.
errorString =
"Workflow error: Assigning GUID " + this.List;
this.List.Trim();
this.List.ToLower();
Guid listGUID = new Guid(this.List);
//Get the list object
errorString =
"Workflow error: Assigning List " + this.List;
SPList list = web.Lists[listGUID];
//Get the item within the list
errorString =
"Workflow error: Assigning List Item " + Convert.ToString(this.Item);
SPListItem item = list.GetItemById(this.Item);
//Check for lookup by email
this.FieldValue.Trim();
errorString =
"Workflow error: Checking for @ in " + this.FieldValue;
if (this.FieldValue.Contains("@"))
{
errorString =
"Workflow error: New variable user.";
SPUser user;
//Get the user by the email address passed
errorString =
"Workflow error: Get user for this email address: " + this.FieldValue;
user = web.AllUsers.GetByEmail(
this.FieldValue);
//Assign the user.
errorString =
"Workflow error: Checking for no user returned using email.";
if (user != null)
{
web.AllowUnsafeUpdates =
true;
errorString =
"Assigning user to field after email lookup: field-" + this.FieldToUpdate +
" Name-" + Convert.ToString(user.Name) + " EMail-" + user.Email;
item[
this.FieldToUpdate] = user;
item.UpdateOverwriteVersion();
web.AllowUnsafeUpdates = allowit;
}
else
throw new Exception("Workflow error: user returned is null " + this.FieldValue);
}
else
{
//Assign the name from the field passed
errorString =
"Getting particular user from name.";
string[] domainName = new string[1];
domainName[0] =
this.FieldValue;
errorString =
"Workflow error: New variable user.";
SPUser user;
//Assign the user from the user collection
errorString =
"Workflow error: Assigning user from get collection.";
user = web.AllUsers.GetCollection(domainName)[0];
//Assign the user
errorString =
"Workflow error: Checking for no user returned using name.";
if (user != null)
{
web.AllowUnsafeUpdates =
true;
errorString =
"Assigning user to field after name lookup: " + this.FieldToUpdate +
" " + Convert.ToString(user.Name);
item[
this.FieldToUpdate] = user;
item.UpdateOverwriteVersion();
web.AllowUnsafeUpdates = allowit;
}
else
{
throw new Exception("Workflow error: Didn't find user by name for " + this.FieldValue);
}
}
}
}
catch (Exception e)
{
//Uncomment the following line to log errors
//throw new Exception("Workflow error: " + errorString + " *****" + e.Message + " *****");
}
finally
{
//Get rid of the resources
web.Close();
web.Dispose();
site.Close();
site.Dispose();
}
//Return execution status
return ActivityExecutionStatus.Closed;
}
I had to use the UpdateOverwriteVersion method to avoid creating several hundred versions (apparently one for each field in the list) as I have versioning turned on for the list where I am using this. Some of the code is superfluous but after I finally got it working, I needed to move on, and it is not really hurting anything being in there.
I have not actually tried using this to update from an account name, but I know the email assignment works.
Hope this helps.
Tom