将新增用户添加到指定用户组

在oa系统中,对于员工信息,设计的是让行政\人事的同事,自行维护一张名为“员工信息”的列表。后端通过制定程序,来达到行政\人事所需的要求。于是就产生了"将在员工信息列表中新增员工添加到指定组"的需求。(其实这个之前一直是手工输入,但觉得有空的时候,还是应该自动化下,所以。。)

关键代码如下:

//从域中获取该用户
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://domain", @"username", "password", AuthenticationTypes.Secure);
DirectorySearcher ds = new DirectorySearcher(directoryEntry);
ds.Filter = "(&(objectClass=User))";
SearchResultCollection collection = ds.FindAll();
                      
foreach (SearchResult sr in collection)
{
         System.DirectoryServices.DirectoryEntry det = sr.GetDirectoryEntry();
         System.DirectoryServices.PropertyCollection pc = det.Properties;
                        

         if (det.Properties["mail"].Value != null && det.Properties["displayName"].Value != null && det.Properties["userPrincipalName"].Value!=null)
         {
                   if (det.Properties["displayName"].Value.ToString().Equals(employeeName))
                   {
                           string tmpLoginName = det.Properties["userPrincipalName"].Value.ToString();
                           try 
                          {
                                string[] sArray = tmpLoginName.Split(new char[1] { '@' });
                                       
                                 try 
                                 {
                                       newUser = web.EnsureUser(@"domain\"+sArray[0]);
                                          
                                  }catch(Exception ex)
                                   {
                                           Console.WriteLine(ex.stacktrace);
                                   }
                                       

                                   }catch(Exception ex)
                                   {
                                       Console.WriteLine(ex.Message);
                                   }

                                   break;

                               }
                            }
                           
                       } 


           SPGroupCollection groups = web.Groups;
           foreach (SPGroup tmpgroup in groups)
           {
                 if (tmpgroup.Name.Equals(groupName))
                 {
                       destGroup = tmpgroup;
                       break;

                 }
            }

            //将新建员工加入指定组
            if (destGroup != null)
            {
                destGroup.AddUser(newUser.LoginName, newUser.Email, newUser.Name, newUser.Notes);
                        
             }

 需要引用dll: System.DirectoryServices。

 其中SPWeb.EnsureUser可以替换为SPWeb.AllUsers.Add(),直接用搜到的用户的域帐号信息来添加。

附上EnsureUser的代码:

 try
    {
        SPUser byLoginNoThrow = this.SiteUsers.GetByLoginNoThrow(logonName);
        if (byLoginNoThrow != null)
        {
            return byLoginNoThrow;
        }
    }
    catch
    {
    }
    if (!this.DoesUserHavePermissions(SPBasePermissions.BrowseUserInfo))
    {
        SPGlobal.HandleUnauthorizedAccessException(new UnauthorizedAccessException());
    }
    SPPrincipalInfo info = SPUtility.ResolvePrincipalInternal(this, null, null, logonName, SPPrincipalType.SecurityGroup | SPPrincipalType.User, SPPrincipalSource.All, null, false, true);
    if (info == null)
    {
        throw new SPException(SPResource.GetString("UserCouldNotBeFound", new object[] { logonName }));
    }
    if (info.PrincipalId < 0)
    {
        this.SiteUsers.Add(info.LoginName, info.Email, info.DisplayName, string.Empty);
    }
    return this.SiteUsers[info.LoginName];
 

再附上"在console程序里使用SPWeb.EnsureUser"无效的原因分析和解决方法:

http://blog.mastykarz.nl/inconvenient-programmatically-sharepoint-users-spweb-ensureuser/

猜你喜欢

转载自laintoday.iteye.com/blog/1664884
今日推荐