Reputation: 1411
I have a column that has names of the user. The column type is Single line of Text and the user name is stored in the format LastName, FirstName;
I would like to get the user id and email address of the stored user name.
I tried
string fieldValue = item["ProjectManager"] as string;
SPFieldUserValueCollection users = new SPFieldUserValueCollection(item.Web, fieldValue);
if (users != null)
{
/* The users.count is always zero */
foreach (SPFieldUserValue user in users)
{
if (user.User != null)
{
}
}
}
I cannot change the column type to Person or Group and it would remain as Single Line of Text. Please let me know how can I achieve this . I have been trying this for couple of hours now.
Upvotes: 1
Views: 2255
Reputation: 6001
Try this:
public SPUser GetUser(SPWeb web, string userstring)
{
var user = FetchUser(web, userstring);
if (user == null)
{
SPPrincipalInfo info = SPUtility.ResolvePrincipal(web, userstring, SPPrincipalType.User, SPPrincipalSource.All, null, false);
if (info!=null)
user = FetchUser(web, info.LoginName);
}
return user;
}
public SPUser FetchUser(SPWeb web, string userstring)
{
return web.AllUsers.Cast<SPUser>().FirstOrDefault(
i => i.Name == userstring || i.LoginName == userstring || i.Email == userstring);
}
From SPUser you get get the user ID.
Get SPUser in your case with:
var user = GetUser(item.Web,fieldValue )
Upvotes: 1