How to get the email address from a SharePoint account

I had a situation where I needed the email address (and other information) for a user that was in a field in a SharePoint list. Turns out to be pretty simple to get a handle to, once you know the objects. There are actually a few ways to get the information. I tried a couple ways but found that they were inconsistent: it would work under these conditions but not these. This is the final solution that I ended up using.

Within each site collection, there is a “User Information List” that gets populated with information about the users that have access to any of the content throughout the site collection. This is where you can consistently pull the information (of course, assuming that the data has been provided). The code that I will share is pulled from a SharePoint 2007 workflow. I’m pulling out pieces, so it might not be exactly cut/paste –able, but it will give the objects and relationships necessary.

   1: SPWeb web = workflowProperties.Web;

   2: SPWeb webSC = web.Site.RootWeb; //this gives us a reference to the site collection

   3: SPListItem user = workflowProperties.Item;//in my example, my ListItem represents a user, therefore the variable name "user"

   4: SPFieldUserValue userValue = new SPFieldUserValue(webSC, user["Account"].ToString());

   5: SPList userInfoList = webSC.Lists["User Information List"];

   6: SPListItem userInfoItem = userInfoList.Items.GetItemById(userValue.LookupId);

   7: //now to get the email address, for example:

   8: string email = userInfoItem["Work e-mail"].ToString();

That’s all there is to it. Nice and simple.

Ninja

Advertisements

About lelandholmquest

After serving in the Navy as a Reactor Operator on fast attack submarines, I earned both a Bachelor's and Masters in Information Technology from American InterContinental University and am currently working on my doctorate. I have a beautiful wife and two of the sweetest daughters a man could ask for. And I work for the greatest company: Microsoft. At Microsoft I work on the knowledge management system for Microsoft Services: Campus on Office 365.
This entry was posted in C# Programming, SharePoint Coding. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s