Home > Blogroll, development, event handlers, MOSS, MS SharePoint Sever, sharepoint, sps, Uncategorized > Send email to a user, getting it from a ‘Person or Group’ field in a List

Send email to a user, getting it from a ‘Person or Group’ field in a List

First, we have to read from “Person or Group” field.
It could be a “Person or Group” field that allows one user to be selected.
It could be a “Person or Group” field that allows multiple users to be selected.
If its a single user, get user, get his email address, send him email.
If there are multiple users, get each of them one by one, get their email addresses, send them emails.

Do not copy/paste the code; and if you do, look for the typos :); should be mostly alright though….

and also be careful coz inverted commas sometimes doesn’t get copied propertly from a web page….

using Microsoft.SharePoint.Utilities; //this one is for sending emails..

using (SPSite mySite = new SPSite("http://server/site"))
{
   using(SPWeb myWeb = mySite.OpenWeb())
   {
        SPList myList = myWeb.Lists["myListNameHere"];
        SPFieldCollection allMyListFields = myList.Fields;

foreach(SPListItem myItem in myList.Items)  //go through all the items
{
         foreach (SPField myField in allMyListFields)  //go through all the fields
         {
                 if (myField.GetType() == typeof(SPFieldUser))  //check if the field is Person or Group field i.e. SPFieldUser
                {          
                          Type fieldType = myField.GetFieldValue(myItem[myField].ToString()).GetType();
                            
                           if (fieldType == typeof(SPFieldUserValueCollection))  //check if the field allows multiple selection of users                
                          {
                                string emailToFieldValues = myItem[myField].ToString();
                                SPFieldUserValueCollection emailToUsers = new SPFieldUserValueCollection(myItem.Web, emailToFieldValues.ToString());
                                foreach (SPFieldUserValue emailUser in emailToUsers)
                                {
                                    SPUser emailThisUser = emailUser.User;
                                    string emailAddress = emailThisUser.Email.ToString();
                                    SPUtility.SendEmail(myWeb, true, true, emailAddress, emailSubject, emailBody);
                                }
                            }
                            else
                            {
                             SPFieldUserValue emailToFieldValue = (SPFieldUserValue)myField.GetFieldValue(myItem[myField].ToString());
                                SPUser emailToUser = emailToFieldValue.User;
                                string emailToAddress = emailToUser.Email.ToString();
                                SPUtility.SendEmail(myWeb, true, true, emailToAddress, emailSubject, emailBody);
                            } 
                        } //if1
                     } //foreach2
                   } //foreach1
                 } //SPWeb
               } //SPSite

 

So the quesiton is where can you put this code? Could be an event handler that sends an email to a user when an item is added to a document library. How to do that, look here:

https://rehmangul.wordpress.com/2009/11/17/writing-event-handlers-and-deploying-using-features/

Hope this helps–

Advertisements
  1. ankit
    September 28, 2010 at 11:01 am

    Hey,

    Thanks
    It really worked for me.

    Is there any way so that I can avoid foreach loop for all fields?

  1. No trackbacks yet.

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

%d bloggers like this: