0

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);
           }
       }
   } 

}
3
  • 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?
    – Geeky Guy
    Commented Jul 5, 2013 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. Commented Jul 6, 2013 at 11:40
  • What you have there is a hash set, not a List Array.
    – Geeky Guy
    Commented Jul 6, 2013 at 16:47

1 Answer 1

0

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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