Showing posts with label People Picker. Show all posts
Showing posts with label People Picker. Show all posts

Monday, May 20, 2013

How to Get User Emails from Sharepoint 2010 using People Editor

This code shows few options on selecting users from sharepoint site and retrieving their mails using people editor web control

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Collections;

namespace CustomPeoplePickerOne.PeoplePicker_swp_1
{
    [ToolboxItemAttribute(false)]
    public class PeoplePicker_swp_1 : WebPart
    {
        PeopleEditor pe = null;
        SPWeb web = null;
        protected override void CreateChildControls()
        {
            web = SPContext.Current.Web;

            pe = new PeopleEditor();
            pe.ValidatorEnabled = true;
            this.Controls.Add(pe);

            Button viewPeople = new Button();
            viewPeople.Text = "View";
            viewPeople.Click += new EventHandler(viewPeople_Click);
            this.Controls.Add(viewPeople);
                 
        }

        void viewPeople_Click(object sender, EventArgs e)
        {
            viewPeopleOption4();
        }

        void viewPeopleOption() 
        {
            foreach (var loginName in pe.CommaSeparatedAccounts.Split(';', ','))
            {
                SPUser user = SPContext.Current.Web.EnsureUser(loginName);
                TextBox tb = new TextBox();
                tb.Text = string.Format("Name : {0} Email : {1}", user.Name, user.Email);
                this.Controls.Add(tb);
            }
        }

        void viewPeopleOption2() 
        {
            //Create the data table and add data to it
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn(" Name ", typeof(String)));
            dt.Columns.Add(new DataColumn(" Email ", typeof(String)));

            //get data from the people editor
            for (int i = 0; i < pe.ResolvedEntities.Count; i++)
            {
                string accName = "";
                string email = "";
                PickerEntity pickerEntity = (PickerEntity)pe.ResolvedEntities[i];
                Hashtable hsEntityData = pickerEntity.EntityData;
                if (hsEntityData.ContainsKey("Account Name"))
                {
                    accName = Convert.ToString(hsEntityData["Account Name"]);
                }
                else
                {
                    accName = "Not available";
                }
                if (hsEntityData.ContainsKey("Email"))
                {
                    email = Convert.ToString(hsEntityData["Email"]);
                }
                else
                {
                    email = "Not available";
                }


                TextBox tb = new TextBox();
                tb.Text = string.Format("Name : {0} Email : {1}", accName, email);
                this.Controls.Add(tb);

            }
        }

        void viewPeopleOption3() 
        {
            var accountName = pe.Accounts[0];
            SPUser user = web.EnsureUser((string)accountName);

            Label userLable = new Label();
            userLable.Text = string.Format("User : {0} Email : {1}",user.Name,user.Email);
            this.Controls.Add(userLable);

        }

        void viewPeopleOption4() 
        {
            if (pe.IsValid)
            {
                for (int i = 0; i < pe.ResolvedEntities.Count; i++)
                {
                    PickerEntity picker = (PickerEntity)pe.ResolvedEntities[i];
                    Hashtable hstEntityData = picker.EntityData;
                    string[] name = picker.DisplayText.Split(' ');
                    string Email = Convert.ToString(hstEntityData["Email"]);
                    //string FName = name[0].ToString();
                    //string SName = (name[1].ToString() != null) ? name[1].ToString() : "";
                    Label userLable = new Label();
                    userLable.Text = Email;
                    this.Controls.Add(userLable);

                }
            }
        }
    }
}