Here requirement is to get the SPUser object from the SPFieldUser. Sharepoint stores the “People and Group” column value in following format id;#domain\user (eg. 1;#SPVM\user) .So we need to get the SPUser object from the SPFieldUser .Once the SPUser object is retrieved ,we can access all the properties of the SPUser like Email,LoginName etc.
Similary we can access any column created using “People and Group” column type
Here is the sample code .Here I am taking the 1st object from the task List.
-----------------------------------------------------------
SPSite site = new SPSite("http://spvm:2409");
SPWeb oSubWeb = site.OpenWeb();
string assignedTo = string.Empty;
//Get the first item from the “Task” List
SPListItem oItem = oSubWeb.Lists["Tasks"].Items[0];
if (oItem["Assigned To"] != null)
{
assignedTo = oItem["Assigned To"].ToString();
SPFieldUserValue spfl = new SPFieldUserValue(oSubWeb, assignedTo);
SPUser oUser = spfl.User;
if (oUser != null)
{
assignedTo = oUser.Email;
//userName = oUser.Name;
}
}
----------------------------------------------------------
So your job is done.
Enjoy!!!
No comments:
Post a Comment