How to send email to People or group field user

March 2nd, 2010 | Categories: Customization, SharePoint, WSS 3.0 | Tags: , ,

People/Group field in SharePoint allow us to keep adding the existing user(s) or group(s) into the document library or list item. Let’s think, I have a list where one field name i.e. Assigned To (reviewer) is type of People/Group. Objective is when I assign someone to review the list item, and then an email notification needs to be fired up to that reviewer.
In other words, you have to find that person from the UserCollection list in SharePoint and send an email to that associated person.

Now once you assign one person on this field, you will found how the SharePoint display  his/her name in the New/Edit OTB form and it is like DOMAIN\LOGINNAME. So if your domain name is SP-SERV and your name is Himadrish Laha, it will be looks like SP-SERV\Himadrish Laha.

People O rGroup Field

Below the simple code I have used to fetch the string data and it returns me something else which I found in the debugging mode “1;#SP-SERV\\kiran” (here I have added user Kiran). When I have added my name it looks like “16;#SP-SERV\\Himadrish Laha“. String represent is kind of <UserID>;#<UserLoginName>.More difficult for you to work.

People Or Group Field Value

People Or Group Field Value

My initial thought is to split the string value and use the UserID. However latter I found this is not the actual UserID associated with the user, instead of this, it is kind of index ID associated with this user in SharePoint. So, if you try to use this ID as below, I bet you will found different user in SPUser object. So the following code is incorrect to trap actual user:


string UserAccount = _ListItem["Assigned To (Reviewer)"].ToString();
int UserID;
if (UserAccount.Trim() != string.Empty)
{
UserID = Convert.ToInt32(UserAccount.Substring(0, UserAccount.IndexOf(“;”)));

SPUser _SPUser = properties.OpenWeb().Users[UserID];
string UserEmail = _SPUser.Email.ToString();
}

What we have left now is only the login display name. Here is also one drawback to trap the user. This string is Login Display Name and it is not the LoginID. So if my login id is Himadrish.Laha and my display name is Himadrish Laha, it will show me Himadrish Laha in the People filed control text box. Note the missing of the DOT (.). For people having same string in loginID as well as Login Display name shouldn’t be a problem, but for other it will make a big difference.

To resolve this issue, the following code may help you. In most of the cases, you can simply add one DOT (.) in between user’s given name and his/her family name. Inside the code, the web represent the SPWeb object. So the method is being used here is SPWeb.EnsureUser(). You need to pass user’s login id over there, and it will return you SPUser object.


string UserAccount = _ListItem["Assigned To (Reviewer)"].ToString();
string UserLogin;
string UserEmail = string.Empty;

if (UserAccount.Trim() != string.Empty)
{
UserLogin = UserAccount.Substring(UserAccount.IndexOf(“#”), (UserAccount.Length – UserAccount.IndexOf(“#”)));
UserLogin = UserLogin.Replace(“#”, “”).Replace(“;”, “”);
UserLogin = @”SP-SERV\” + UserLogin.Replace(” “,”.”);

SPUser _SPUser = web.EnsureUser(UserLogin);
UserEmail = _SPUser.Email.ToString();
_SPUser = null;
}


Kindly note, the above code may need to change according to get the login id from the login name. If in your organization the login id is something the first character of your given name i.e. H for Himadrish and then your last name i.e. Laha, then your login id would be HLAHA. So you need to change the above code and found the actual login id what it would be. Rest of the line would be same.

Hopes this will help you.

  1. February 11th, 2011 at 04:25

    I collected so many interesting things in your blog especially its discussion. From the tons of comments on your posts, I think I am not the only one having all of the enjoyment here! continue the good work.Regards

  2. February 11th, 2011 at 06:26

    Great blog here i like all the information thats being shared, congratulations.

  3. February 11th, 2011 at 23:19

    That completely makes sense when you put it in that way. You actually cleared things up for me personally.

  4. February 12th, 2011 at 00:04

    Thank you for the amazing post, I actually really liked it!! I added this specific internet page and I’m going to make sure to revisit.

  5. February 12th, 2011 at 02:50

    First time commentor to this blog and just googled in. I like the design of this site. I just saved it, so you now have a follower! :) Thank you!

  6. Wrinkle Repair
    February 12th, 2011 at 03:42

    That makes sense.

  7. February 12th, 2011 at 07:21

    I’d have to buy into with you here. Which is not something I typically do! I love reading a post that will make people think. Also, thanks for allowing me to comment!

  8. February 13th, 2011 at 11:05

    I’m still learning from you, as I’m improving myself. I absolutely love reading everything that is written on your site.Keep the tips coming. I liked it!

  9. February 25th, 2011 at 18:39

    Great, this is simple tips and easy to understand. Thanks for share this post next I will always go here future for your update.

  10. March 2nd, 2011 at 09:19

    Cool information it is without doubt. We’ve been waiting for this content

  11. March 3rd, 2011 at 21:54

    Wow, great post Thanks for sharing !

  12. March 4th, 2011 at 05:58

    kinda like the post you have written . it just isn’t that easy to find great text toactually read (you know READ! and not simply going through it like some uniterested and flesh eating zombie before going somewhere else), so cheers man for not wasting my time on the god forsaken internet. :D

  13. March 5th, 2011 at 16:35

    Great post!

  14. March 6th, 2011 at 08:38

    I love this blog layout One of the best I’ve seen.

  15. March 14th, 2011 at 00:14

    Extremely cool, some wonderful points! I appreciate you making this article available, the rest of your site is also well done. I hope you have a great day.

  16. March 16th, 2011 at 09:38

    Your framework is valueble forwards other self. Thanks!…

  17. March 17th, 2011 at 18:54

    I am extremely a new comer to the internet and required to review this subject. Thought it was a great article very well written and helpful. I’ll definitely be coming back for your site to read more articles when i loved this one..

  18. March 17th, 2011 at 20:10

    I completely agree with most of the ideas you’ve presented in this blog. They’re especially convincing and will most likely work. Could you please extend on this a bit more next time?

  19. March 17th, 2011 at 23:00

    I find myself coming to your blog more and more often to the point where my visits are almost daily now!

  20. March 18th, 2011 at 16:30

    I was pretty pleased to discover this web-site.I want to thanks for your precious time with this amazing learn!! I positively taking advantage of any little it and I’ve you book marked to look at brand-new stuff you writing.

  21. Himadrish
    December 1st, 2010 at 17:58

    This is WordPress blog application. You can get it in Google :)

    Thanks for the post.

Comment pages

2 trackbacks

  1. office 2012 Trackback | 2011/04/22
  2. Scarlet Horizon Trackback | 2011/11/20
You must be logged in to post a comment.