Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Can you please help me to get username ,email from a people picker control.

I want this in the same method.

for example if i enter 2 groups in people picker control then it should return

his Name and Email address in the same method. if it is of table then it would be gr8

Below is the sample code In this method i want to retuen as table.... for group as well as notmal users

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace GETEMAILSFROMPEOPLEPICKER
{
    public static class SPFieldUserValueCollectionExtensions
   {     
       public static IEnumerable<string> GetAllEmails(this SPFieldUserValueCollection collection, SPWeb web) 
       {
           //System.Diagnostics.Debugger.Break();

           var emails = new HashSet<string>();

           foreach (var item in collection)  
           {        
               if (item.User == null)  
               {          
                   try  
                   {   
                       // is it a SharePoint group?   
                       var group = web.SiteGroups.GetByID(item.LookupId); 
                       emails.AddEmailsFromSPGroup(group);

                   }           
                   catch    
                   {  

                      // ad group  
                       var group = web.EnsureUser(item.LookupValue); 
                       emails.AddIfNotNull(group.Email);   
                   }          
               }            
               else           
               {               
                   emails.AddIfNotNull(item.User.Email);  
               }      
           }         
           return emails;  
       }
       private static void AddEmailsFromSPGroup(this HashSet<string> emails,SPGroup group)  
       {       
           if (!string.IsNullOrEmpty(group.DistributionGroupEmail))   
           {        
               emails.Add(group.DistributionGroupEmail); 
           }         
           else      
           {         
               foreach (SPUser user in group.Users)    
               {    
                   emails.AddIfNotNull(user.Email);    
               }      
           }    
       }      

       private static void AddIfNotNull(this HashSet<string> set, string s)   
       {
           if(!string.IsNullOrEmpty(s)) 
           {           
               set.Add(s); 
           }     
       }

       public static IEnumerable<string> GetAllLoginNames(this SPFieldUserValueCollection collection, SPWeb web)
       {
            var usernames = new HashSet<string>();

            foreach (var item in collection)
            {
                if (item.User == null)
                {
                    try
                    {
                        // is it a SharePoint group?   
                        var group = web.SiteGroups.GetByID(item.LookupId);

                        usernames.UserNamesFromSPGroup(group);
                    }
                    catch
                    {

                        // ad group  
                        var group = web.EnsureUser(item.LookupValue);
                        usernames.IfNotNull(group.Name);
                    }
                }
                else
                {
                    usernames.AddIfNotNull(item.User.Name);
                }
            }   
            return usernames;

       }

       private static void UserNamesFromSPGroup(this HashSet<string> usernames, SPGroup group)
       {
           if (!string.IsNullOrEmpty(group.DistributionGroupEmail))
           {
               usernames.Add(group.DistributionGroupAlias);
           }
           else
           {
               foreach (SPUser user in group.Users)
               {
                   usernames.AddIfNotNull(user.Name);
               }
           }
       }

       private static void IfNotNull(this HashSet<string> set, string s)
       {
           if (!string.IsNullOrEmpty(s))
           {
               set.Add(s);
           }
       }
   } 

}
share|improve this question
    
Can you please be more clear? How do you want to fetch that information? Programmatically, with C#? In a workflow? Or do you want the user info expanded in another control? –  Renan Jul 5 '13 at 19:24
    
Hi Renan, I have written the below method which retuen emails and usernames as List array. But i want this method to return as table for Group as well as users. –  sandy_9866141457 Jul 6 '13 at 11:40
    
What you have there is a hash set, not a List Array. –  Renan Jul 6 '13 at 16:47

1 Answer 1

Replace:

var emails = new HashSet<string>();

With:

DataTable emails = new DataTable();
emails.Columns.Add("username");
emails.Columns.Add("emailAddress");

Now, everywhere where you're using a HashSet<string>, use that DataTable instead. When adding items, do it this way:

DataRow newRow = emails.NewRow();
newRow["username"] = /*add the user name here*/
newRow["emailAddress"] = /*add the user email address here*/
emails.Rows.Add(newRow);

When you're done, you'll have your users and their email addresses in a tabular format. You'll even be able to use that emails object as the datasource for a GridView.

Don't forget to put a using System.Data at the top of the code.

share|improve this answer
    
No it is not working ... I have tried but still it fails if i try to get a sharepoint group users in a table –  sandy_9866141457 Jul 7 '13 at 4:23
    
How does it fail? What error do you get? –  Renan Jul 7 '13 at 10:59
    
Error 7 Instance argument: cannot convert from 'System.Data.DataTable' to 'System.Collections.Generic.HashSet<string>' C:\Users\sp2010_install\documents\v‌​isual studio 2010\Projects\Task and Alert Schedular\Task and Alert Schedular\GetAllUsers.cs 30 25 Task and Alert Schedular –  sandy_9866141457 Jul 7 '13 at 11:55
    
You are mixing different types in your code. Stop using HashSet altogether and use only DataTable instead. –  Renan Jul 7 '13 at 17:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.