0

我正在尝试从活动目录中获取用户的经理名称,我创建了一个控制台程序来测试它并且一切正常,当我在我的 Sharepoint 2010 程序中尝试代码时,它只是给了我一个例外“一个发生操作错误”。

进一步调试后,我发现主体上下文出错,引发异常“为了执行此操作,必须在连接上完成成功绑定。” 代码如下(注意:try catch 中的代码与我在控制台应用程序中的代码完全相同。):

   public override void ItemAdding(SPItemEventProperties properties)
   {
       base.ItemAdding(properties);

       try 
       {
               // set up domain context
               PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

               // find a user
               UserPrincipal user = UserPrincipal.FindByIdentity(ctx, Environment.UserName);
               string samAccountName = "";


               if (user != null)
               {
                   // do something here....     
                   samAccountName = user.SamAccountName;
               }


               //Get the manager name from the active directory
               var domain = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;

               using (DirectoryEntry dir = new DirectoryEntry("LDAP://" + domain))
               {
                   using (DirectorySearcher ds = new DirectorySearcher(dir, "samAccountName=" + samAccountName))
                   {

                       SearchResult result = ds.FindOne();

                       string managerName = result.Properties["manager"][0].ToString();
                   }
               }



       }
       catch(Exception ex)
       {
           var message = ex.Message;

       }
   }
4

1 回答 1

0

尝试改变:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAINNAME");
于 2013-03-25T15:56:35.660 回答