方法在共享点2010中的人员选取器控件中返回所有用户的电子邮件

时间:2013-07-02 14:28:53

标签: sharepoint sharepoint-2010 peoplepicker

有人可以尝试在SharePoint 2010中的人员选择器控件数组中获取所有用户的用户名和电子邮件吗?

如果是这样,你可以给我一个方法吗?

该方法应具有(sharepointgroupname,sharepoint user)

等参数

我试过像这样。但它只返回人员选择器控件中的一个用户名和电子邮件地址。我想为多个用户检索Sharepoint组和Active Directory组。请帮帮我。

 private void GetNameAndEmailFromPeoplePicker(SPListItem mySourceListItem, out String PeoplePicker, out String CCPeoplePicker, out String UserName)
        {
            PeoplePicker = "";
            CCPeoplePicker = "";
            UserName = "";
            try
            {

                SPFieldUserValueCollection Users = new SPFieldUserValueCollection(mySiteWeb, Convert.ToString(mySourceListItem["Point of Contact"]));
                foreach (SPFieldUserValue User in Users)
                {
                    UserName = User.User.Name;
                    PeoplePicker = User.User.Email;
                }
                SPFieldUserValueCollection BCCUsers = new SPFieldUserValueCollection(mySiteWeb, Convert.ToString(mySourceListItem["Backup Point of Contact"]));
                foreach (SPFieldUserValue BCCUser in BCCUsers)
                {
                    CCPeoplePicker = BCCUser.User.Email;
                }
            }

            catch (Exception ex)
            {
                UlsLogs.LogErrorInULS(ex.InnerException.ToString(), TraceSeverity.High);
            }
        }

2 个答案:

答案 0 :(得分:1)

从Sharepoint People Picker获取用户名和电子邮件。

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

}

答案 1 :(得分:0)

我不确定你的问题是什么......以及你想用哪种语言?

使用JavaScript我使用this method执行与人员选取器相同的操作...