Wednesday, September 17, 2008

Getting AccountName from a "Person or Group" field

This is one I ran into a while back and blogged for reference:

Let's say you have a "Person or Group" field on a SharePoint list... How do you get the username of that user in code? Finally found the answer to this buried in MSDN.
Here is the code snippet to convert "Person or Group" type field in a SharePoint list(which is stored like: "12345#;Some Name") into the correct account name in the form of “<Domain>\<Username>”.


SPFieldUserValue userValue = new SPFieldUserValue(workflowProperties.Web,retrievedFieldValue);

string userName = userValue.User.LoginName;


In this case the SPWeb is coming from the workflowProperties, but it just needs the one that the SPList lives in.

No comments: