0

任何人都可以尝试在 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);
            }
        }
4

2 回答 2

1

从 Sharepoint 人员选择器获取用户名和电子邮件。

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

}
于 2013-07-03T18:22:43.957 回答
0

我不确定你的问题……你想用哪种语言?

使用 JavaScript,我使用这种方法来做与 People Picker 相同的事情......

于 2013-07-02T17:56:40.847 回答