Active Directory基于自定义字段的外观 [英] Active Directory look based on custom field

查看:66
本文介绍了Active Directory基于自定义字段的外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,让我从设置场景开始.

我正在创建一个InfoPath表单用于购买软件.表单根据当前登录的用户将数据从Active Directory自动填充到多个字段中.

现在,当表单打开时,它将检索用户的全名,电子邮件,部门,电话号码和员工ID号码.

我还希望自动填充用户经理,以将批准电子邮件发送到.

我们的Active Directory中有一个小规定,我们不使用经理名称".用户属性中的字段.员工ID也不保留在员工ID字段中.员工的ID号存储在自定义属性"字段1中.用户经理将其员工ID添加到自定义属性5"中.我的表单当前从自定义属性中检索这两个员工ID号.想要做的是从经理属性中获取经理的雇员ID号,然后使用该号码在``自定义属性1''字段中搜索Active Directory并找到匹配项,然后使用匹配的``全名''填充字段.

无论如何我都不是程序员,只是从MDSN中获取了示例代码,并对其进行了调整,以使其走到现在,所以如果有人可以帮助我弄清楚这最后一点,我将不胜感激.

到目前为止,这是我的代码:

Ok let me start by setting up the scenario.

I'm creating an InfoPath form for software purchasing.  The form auto-populates data into several fields from Active Directory based on the current logged in user.

Right now when the form opens it retrieves the users full name, email, department, phone number and employee ID number.

I want to auto populate the users manager as well for an approval email to be sent to.

One minor stipulation is in our Active Directory we do not use the "Managers Name" field in the users properties.  The employees ID is also not kept in the Employee ID field.  The Employee's ID number is stored the Custom Attribute field 1.  The users manager has his/her Employee ID added to Custom Attribute 5.  My form currently retrieves both of these Employee ID numbers from the custom attributes.

What i would like to be able to do is to take the managers Employee ID number (from the users properties) and then use that number to search Active Directory in the Custom Attribute 1 field and find the match and then populate a field with the matching Full Name.

I'm not a programmer by any means, and have just snagged sample code from MDSN and tweaked with it to get it this far, so if anyone could help me figure out this last bit i would appreciate it.

Here is my code thus far:

<身体> 启用浏览器的表单不支持
使用 Microsoft.Office. InfoPath;
使用 系统;
使用 System.Xml;
使用 System.Xml.XPath;
使用 System.DirectoryServices;
使用 System.Windows.Forms;
命名空间 AD_Connection
{
public 部分 <字体样式="color:blue">类 <字体样式="font-size:11px"> FormCode
.{
//成员变量.
//而是从FormState
//词典使用如下代码: //
//private object_memberVariable
//{
//得到
//{
//返回FormState ["_ memberVariable"];
//}
//set
//{
//FormName ["_ memberVariable"] =值;
//}
//}
//注意:Microsoft Office InfoPath要求执行以下过程.
//可以使用Microsoft Office InfoPath对其进行修改.
public 无效 内部启动()
.{
EventManager.FormEvents.Loading + = LoadingEventHandler(FormEvents_Loading);
public void FormEvents_Loading( object 发送者, LoadingEventArgs e)
.{
//获取当前用户的用户名.
字符串 strUserName = .Application.User.UserName;
//使用用户名
//作为LDAP搜索过滤器.如果使用其他目录
//而不是Exchange,使用sAMAccountName代替mailNickname.
DirectorySearchersearcher = DirectorySearcher( /font>
"(mailNickname =" + strUserName + ")" );
//搜索指定的用户.
.SearchResult结果= searcher.FindOne();
//确保已找到用户.
如果 (结果== )
MessageBox.Show( "错误查找用户:" + strUserName);
else
11px>
//用户的属性(属性).
DirectoryEntry雇员= result.GetDirectoryEntry();
//将指定的属性分配给字符串变量.
字符串 strFirstName = employee.Properties [ ].Value.ToString();
string strLastName = employee.Properties [ " sn" ].Value.ToString();
字符串 strMail = employee.Properties [ ].Value.ToString();
string strLocation = employee.Properties [ " physicalDeliveryOfficeName" ].Value.ToString();
字符串 strPhone = employee.Properties [ ].Value.ToString();
string strDepartment = employee.Properties [ "部门; string strUserSID = employee.Properties [ ].Value.ToString();
string strMgrSID = employee.Properties [ &; extensionAttribute5""
//创建一个XPathNavigator来遍历主要数据源 td>
//表单.
,XPathNavigator xnMyForm = this .CreateNavigator();
XmlNamespaceManager ns = this .NamespaceManager;
//在表格中设置字段.
)
SetValue(strFirstName);
的xnMyForm.SelectSingleNode( "/my:myFields/my:LastName" ,ns )
SetValue(strLastName);
xnMyForm.SelectSingleNode( "/my:myFields/my:Alias" ,ns )
SetValue(strUserName);
)
SetValue(strMail);
``xnMyForm.SelectSingleNode( ''/my:myFields/my:Location'' ,ns )
SetValue(strLocation);
xnMyForm.SelectSingleNode( "/my:myFields/my:TelephoneNumber" ,ns )
SetValue(strPhone);
)
SetValue(strDepartment);
)
SetValue(strUserSID)​​;
xnMyForm.SelectSingleNode( "/my:myFields/my:mgrSID"" SetValue(strMgrSID)​​;
//清理.
;
``searcher.Dispose();
;
employee.Close();
}
}
using Microsoft.Office.InfoPath;  
using System;  
using System.Xml;  
using System.Xml.XPath;  
using System.DirectoryServices;  
using System.Windows.Forms;  
 
namespace AD_Connection  
{  
    public partial class FormCode  
    {  
        // Member variables are not supported in browser-enabled forms.  
        // Instead, write and read these values from the FormState  
        // dictionary using code such as the following:  
        //  
        // private object _memberVariable  
        // {  
        //     get  
        //     {  
        //         return FormState["_memberVariable"];  
        //     }  
        //     set  
        //     {  
        //         FormState["_memberVariable"] = value;  
        //     }  
        // }  
 
        // NOTE: The following procedure is required by Microsoft Office InfoPath.  
        // It can be modified using Microsoft Office InfoPath.  
        public void InternalStartup()  
        {  
            EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);  
        }  
 
        public void FormEvents_Loading(object sender, LoadingEventArgs e)  
              
        {  
                // Get the user name of the current user.  
                string strUserName = this.Application.User.UserName;  
 
                // Create a DirectorySearcher object using the user name   
                // as the LDAP search filter. If using a directory other  
                // than Exchange, use sAMAccountName instead of mailNickname.  
                DirectorySearcher searcher = new DirectorySearcher(  
                   "(mailNickname=" + strUserName + ")");  
 
                // Search for the specified user.  
                SearchResult result = searcher.FindOne();  
              
                // Make sure the user was found.  
                if (result == null)  
                {  
                MessageBox.Show("Error finding user: " + strUserName);  
                }  
                else 
                {  
                      
                    // Create a DirectoryEntry object to retrieve the collection  
                    // of attributes (properties) for the user.  
                    DirectoryEntry employee = result.GetDirectoryEntry();  
 
                    // Assign the specified properties to string variables.  
                    string strFirstName = employee.Properties["givenName"].Value.ToString();  
                    string strLastName = employee.Properties["sn"].Value.ToString();  
                    string strMail = employee.Properties["mail"].Value.ToString();  
                    string strLocation = employee.Properties["physicalDeliveryOfficeName"].Value.ToString();  
                    string strPhone = employee.Properties["telephoneNumber"].Value.ToString();  
                    string strDepartment = employee.Properties["department"].Value.ToString();  
                    string strUserSID = employee.Properties["extensionAttribute1"].Value.ToString();  
                    string strMgrSID = employee.Properties["extensionAttribute5"].Value.ToString();  
 
                                          
                        // Create an XPathNavigator to walk the main data source  
                        // of the form.  
                        XPathNavigator xnMyForm = this.CreateNavigator();  
                        XmlNamespaceManager ns = this.NamespaceManager;  
 
                        // Set the fields in the form.  
                        xnMyForm.SelectSingleNode("/my:myFields/my:FirstName", ns)  
                           .SetValue(strFirstName);  
                        xnMyForm.SelectSingleNode("/my:myFields/my:LastName", ns)  
                           .SetValue(strLastName);  
                        xnMyForm.SelectSingleNode("/my:myFields/my:Alias", ns)  
                           .SetValue(strUserName);  
                        xnMyForm.SelectSingleNode("/my:myFields/my:Email", ns)  
                           .SetValue(strMail);  
                        xnMyForm.SelectSingleNode("/my:myFields/my:Location", ns)  
                           .SetValue(strLocation);  
                        xnMyForm.SelectSingleNode("/my:myFields/my:TelephoneNumber", ns)  
                           .SetValue(strPhone);  
                        xnMyForm.SelectSingleNode("/my:myFields/my:Department", ns)  
                           .SetValue(strDepartment);  
                        xnMyForm.SelectSingleNode("/my:myFields/my:userSID", ns)  
                           .SetValue(strUserSID);  
                        xnMyForm.SelectSingleNode("/my:myFields/my:mgrSID", ns)  
                            .SetValue(strMgrSID);  
 
                      
                        // Clean up.  
                        xnMyForm = null;  
                        searcher.Dispose();  
                        result = null;  
                        employee.Close();  
                    }  
                }  
 
    }  

推荐答案

我相信在这一点上,您已经获得了经理的EmployeeID:

Hi,

I believe at this point, you already got the Manager's EmployeeID:

字符串 strMgrSID = employee.Properties ["extensionAttribute5"].Value.ToString();

string strMgrSID = employee.Properties["extensionAttribute5"].Value.ToString();

所有您需要做的就是使用存储员工ID的属性进行搜索:


="Courier New"> DirectorySearcher searcher = new DirectorySearcher( (extensionAttribute1 =" + strMgrSID +)");

////搜索指定的用户.
.SearchResult结果= searcher.FindOne();
if(结果!= null)
{
DirectoryEntry manager = result.GetDirectoryEntry();

All you have to do is to do a search using the property where employeeID's are stored:


DirectorySearcher searcher = new DirectorySearcher(  
                   "(extensionAttribute1=" + strMgrSID + ")");  
 
                // Search for the specified user.  
                SearchResult result = searcher.FindOne();  
           
if (result != null)
{
 DirectoryEntry manager = result.GetDirectoryEntry(); 

字符串strManagerFirstName = manager.Properties ["givenName"].Value.ToString();
//在此处获取其余所需的属性...
}


希望这会有所帮助!


这篇关于Active Directory基于自定义字段的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆