Get email from SPUser field in SharePoint

by Mark Deraeve 8. June 2009 07:37

Whe you are working with WSS workflows or webparts, you will come to the point you have to send emails to users.

If you are using a single user field (not allowed to enter multiple users) the value is returned as a string. So if you choose to show the users name and you want to email this users, you cannot get his email out of this field. So how can we get the users email? Here's some functions that might help:

First I get the authors name out of a field in a list:

searchPerson = _doc["Author"].ToString().Substring(_doc["Author"].ToString().IndexOf("#") + 1);
to =  GetEmail(searchPerson);

Sometimes Sharepoint stores #? characters after the names, so multiple values are allowed. The substring will get them out and return the users name correct. E.g. The value could be Mark, Deraeve#2 and after the substring it will be: Mark, Deraeve 

The GetEmail fuction will look for the user in the rootweb, if a user is present in the current web, he will be on the rootweb. Here the code that returns the users email:

        private string GetEmail(string username)
        {
            foreach (SPUser user in SPContext.Current.Site.RootWeb.AllUsers)
            {
                if (user.Name == username)
                {
                    return user.Email;
                }
            }
            return String.Empty;
        } 

So now you get the users email. You could prevent from looping through all users if you have the users login name, but in my case it the users full name, so I cannot use ...AllUsers[loginname] and I have to loop through the entire users collection.

If the user field allows multiple values, you can retrieve the email with the SPFieldUserValueCollection. Because it allows multiple users, it will not return a string, but a SPFieldUserValueCollection. We can use this to build up a list of email addresses:

SPFieldUserValueCollection col = (SPFieldUserValueCollection)_doc["Users"];
StringBuilder sb = new StringBuilder();
foreach (SPFieldUserValue user in col)
{
if (!IsGroup(user,ref sb))
      {
            sb.Append(user.User.Email + ";");
}
}

I will add some extra code that gets the emails of the users who are in a group:

private string GetEmails(string groupName)
        {
            foreach (SPGroup group in SPContext.Current.Site.RootWeb.Groups)
            {
                if (group.Name == groupName)
                {
                    StringBuilder ret = new StringBuilder();
                    foreach (SPUser user in SPContext.Current.Site.RootWeb.Groups[groupName].Users)
                    {
                        ret.Append(user.Email + ";");
                    }
                    return ret.ToString();
                }
            }
            return String.Empty;
        }

I used a function IsGroup, this function simply checks if it is a user or a group and adds all the users in the group to the email addresses (ref sb):

private bool IsGroup(SPFieldUserValue userOrGroup, ref StringBuilder sb)
        {
            SPGroup qsgroup = null;
            try
            {
                qsgroup = SPContext.Current.Site.AllWebs[_sopSiteName].Groups[userOrGroup.ToString()
	.Substring(userOrGroup.ToString().IndexOf("#") + 1)];
                //Page.Response.Write("group");
                foreach (SPUser user in qsgroup.Users)
                {
                    sb.Append(user.Email + ";");
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

Just for fun another function I used to check if a user is a member of a particular group:

        private bool UserInGroup(string group)
        {
            SPGroupCollection grps = CurrentUser().Groups;
            foreach (SPGroup grp in grps)
            {
                if (grp.Name.ToLower().TrimEnd() == group.ToLower().TrimEnd())
                { return true; }
            }
            return false;
        }

 


Tags: , ,

Comments (30) -

austin texas persaonl injury attorneys
austin texas persaonl injury attorneys United States
1/4/2014 7:46:10 AM #

My website :: on line coupon codes - hilden.rene-pickhardt.de/.../Coupon_Fraud_How_To_Distinguish_A_Counterfeit_Coupon Hey are employing Wordpress to your website platform? I am new into the website globe but I'm trying to acquire commenced and set up my very own. Do you want any html coding know-how to generate your very own website? Any aid can be greatly appreciated!

Austin injury attorneys
Austin injury attorneys United States
1/9/2014 8:02:41 AM #

not sure irrespective of whether this post iss written by him as nobody else know such

car accident attorney Austin
car accident attorney Austin United States
1/23/2014 4:59:41 PM #

Thank you.

accident attorney Austin
accident attorney Austin United States
1/26/2014 11:49:04 AM #

Should you are using WordPress, to about arrive spam difficulty just set up plug-in contact Akismet.

car accident attorney Austin
car accident attorney Austin United States
2/25/2014 12:35:34 AM #

Check out out my weblog :: lear funds gold bullion - stonersnetworking.com/.../ There is noticeably plenty of cash to comprehend relating to this. I assume you've got produced specified superior points in functions also. I like this online web site so significantly, saved to favorites . Thanks for give very very good informations. Your web web page is goodI am impressed by the specifics which you have on this weblog. It shows how effectively you realize this topic. Bookmarked this webpage, will come back for any good deal extra. You, my close friend, ROCK! I identified just the aspects I currently searched everywhere and just couldn There are unquestionably quite a lot of aspects like that to assume addicted to consideration. With the function of can be a great height to convey awake. I submit the concepts previously mentioned as natural inspiration but evidently right here are queries such as 1 you convey awake in which by far the most weighty fixation can ensue powerful in straightforward very good faith. I don?t identify if best procedures have emerged round possessions comparable that, within the contrary I am positive that your occupation is openly identified like a sunny match. All girls and boys certainly touch the impact of only a second's enjoyment, on behalf in the rest of their lives. Hey! Would you thoughts if I share your site with my facebook group? You will find lots of folks that i believe would definitely enjoy your written content. Be sure to permit me know. Many thanks

Sherry Eileen Broderick
Sherry Eileen Broderick United States
5/9/2014 8:57:58 PM #

Have a excellent day!  Your WordPress is verty useful and nice Game information .  What? And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A><A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A>  . If you want to get more pageview , you can contact us .

Sherry Eileen Broderick
Sherry Eileen Broderick United States
5/9/2014 9:16:26 PM #

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A> and  .

Sherry Eileen Broderick
Sherry Eileen Broderick United States
5/9/2014 9:58:40 PM #

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Sarah Jacqueline Marion Renshaw
Sarah Jacqueline Marion Renshaw United States
5/9/2014 10:03:07 PM #

�E�I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">Homepage Sakusei</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Jeannie Jibson
Jeannie Jibson United States
5/9/2014 10:42:05 PM #

Have a excellent day!  Your WordPress is verty useful and nice Game information .  What? And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A><A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A>  . If you want to get more pageview , you can contact us .

Jeannie Jibson
Jeannie Jibson United States
5/9/2014 10:48:30 PM #

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A> and  .

Sherry Eileen Broderick
Sherry Eileen Broderick United States
5/9/2014 11:21:24 PM #

Have a excellent day!  Your WordPress is verty useful and nice Game information .  What? And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A><A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A>  . If you want to get more pageview , you can contact us .

Naomi Karen Boult
Naomi Karen Boult United States
5/9/2014 11:24:48 PM #

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A> and  .

Madeleine McKay
Madeleine McKay United States
5/9/2014 11:58:33 PM #

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Sarah Jacqueline Marion Renshaw
Sarah Jacqueline Marion Renshaw United States
5/10/2014 12:05:20 AM #

�E�I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">Homepage Sakusei</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Martha More
Martha More United States
5/10/2014 7:35:22 AM #

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Sherry Eileen Broderick
Sherry Eileen Broderick United States
5/10/2014 7:48:18 AM #

�E�I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">Homepage Sakusei</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Naomi Karen Boult
Naomi Karen Boult United States
5/10/2014 8:24:38 AM #

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A> and <A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A> .

Phyllis Chloe MacKenzie
Phyllis Chloe MacKenzie United States
5/10/2014 8:28:26 AM #

Have a excellent day!  Your WordPress is verty useful and nice Game information .  What? And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A><A href="http://www.blogdehp.jp">;ホ�Eムペ�Eジ作�E</A>  . If you want to get more pageview , you can contact us .

Phyllis Chloe MacKenzie
Phyllis Chloe MacKenzie United States
5/10/2014 9:08:16 AM #

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Naomi Karen Boult
Naomi Karen Boult United States
5/10/2014 9:14:29 AM #

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A> and  .

Sarah Jacqueline Marion Renshaw
Sarah Jacqueline Marion Renshaw United States
5/10/2014 9:46:03 AM #

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作���Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Deanna Shearsmith
Deanna Shearsmith United States
5/10/2014 9:50:04 AM #

Have a excellent day!  Your WordPress is verty useful and nice Game information .  What? And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A><A href="http://www.blogdehp.jp">;ホ�Eムペ�Eジ作�E</A>  . If you want to get more pageview , you can contact us .

Sherry Eileen Broderick
Sherry Eileen Broderick United States
5/10/2014 10:27:52 AM #

Have a excellent day!  Your WordPress is verty useful and nice Game information .  What? And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A><A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A>  . If you want to get more pageview , you can contact us .

Martha More
Martha More United States
5/10/2014 10:34:32 AM #

�E�I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">Homepage Sakusei</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Deanna Shearsmith
Deanna Shearsmith United States
5/11/2014 3:36:48 AM #

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.akibare-hp.jp">;ホ�Eムペ�Eジ作�E</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Madeleine McKay
Madeleine McKay United States
5/11/2014 3:54:10 AM #

�E�I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">Homepage Sakusei</A><A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A>  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Jeannie Jibson
Jeannie Jibson United States
5/14/2014 11:03:09 PM #

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.And We Make HomePage And WebSite AT <A href="http://www.akibare-hp.jp">HomePage sakusei</A> .

Naomi Karen Boult
Naomi Karen Boult United States
5/14/2014 11:05:20 PM #

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.And We Make HomePage And WebSite AT <A href="http://www.blogdehp.jp">;ビジネスブログホ�Eムペ�Eジ作�Eビジネスブログ</A> and  .

Sherry Eileen Broderick
Sherry Eileen Broderick United States
8/3/2014 11:52:24 AM #

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT�@<A href="www.akibare-hp.jp">www.akibare-hp.jp</A>;  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

About the author

I started as a VB6 developer, programming all kinds of applications. I also got involved in the development of a e-market in ASP.

When .Net was introduced, I had the privilege to start in a team of professionals and develop a framework. I learned a lot and started to work as a c#.Net consultant from then on.

After 2 years I also got the chance to work for several months with SharePoint. SharePoint kept chasing me all the years after and when the product got more and more mature I started to love it. Then SharePoint was a daily occupation for me. I implemented a large quality document management system at PMRL being the only developer/technical analyst. Working close together with the QA department we succeeded in making and improving a very good Quality document management system. Its used up until this day.

I worked for 6 years as a .Net consultant for 2 of the largest consultancy companies in Belgium. One of my last projects with the consultancy was creating a custom LIMS system in c#.Net. This was a success and the customer asked me to stay and help to expand and maintain this LIMS. I had great years in this company. There was a focus on innovation and a lot of opportunities for improving and developing new IS systems. Apart from several small projects I further more improved the custom LIMS system, created a SOP library based on SharePoint and a web based Skill matrix system to handle the skills of the employees in the company. 

Then I was involved in transferring the LIMS to a new lab in Singapore. This was done in a short time frame. The deployment was again a success and it passed the GLP inspection tests. I learned a lot from the Quality engineers about validation of computerized systems.

At my current job I have a wide range of challenges. Mainly I'm still working with .Net (VB.net and C#.Net). But outside of the Microsoft world I also do some ABAB programming in our SAP system. Like developing Adobe Forms and the program behind it. Or making small changes to customized code.

I'm working for 2 years with objective C now. Creating apps for factories across Europe. People in the factory are working with IPad to take pictures and upload them to orders and more. Already more and more projects are coming my way. The iOS apps are supported by a home made .Net architecture that exposes WCF services. Its working great. My second app is almost ready for deployment in factories. Now I started an App for the management team so they can manage projects on their IPAD and IPHONE. Next generation apps are build with a .Net Web API background combined with EF 6.0.

My linkedIn!

Month List

Tag cloud

Locations of visitors to this page