通过创建MOSS自定义字段类来实现和基于TREEVIEW的OU菜单

网上一有一篇文章,来很仔细的描述如何增加自定义字段的.可能大家都有见过。这里就不在详细讲如何创建自定义字段了。

有兴趣的话可以查看我BLOG的另外一篇文章http://blog.lickies.cn/Lists/Posts/ViewPost.aspx?ID=17


----------------------------------------
接下来,我来告诉大家如何做TREEVIEW的树型菜单.

首先修改.ASCX文件

<% @ Control Language="C#" Debug="true"%>
<% @ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<% @ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0,

 Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls"  %>

<SharePoint:RenderingTemplate ID="SelectItemFromXMLFieldControl" runat="server">
    <Template>
   
        <asp:TextBox ID="txt_Ou" runat="server"></asp:TextBox>
     <asp:TreeView ID="TreeView1" runat="server" ShowLines="True">
</asp:TreeView>
    </Template>
</SharePoint:RenderingTemplate>
修改SelectItemFromXML.FieldControl.cs文件
using System;
using System.Runtime.InteropServices;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.DirectoryServices;


namespace SelectItemFromXML
{
    [CLSCompliant(false)]
    [Guid("eb269bd9-0d3e-4aa2-b88f-37fbccb0d7d8")]
    public class SelectItemFromXMLFieldControl : BaseFieldControl
    {

        protected TreeView TreeView1;
        protected TextBox txt_Ou;
        protected override string DefaultTemplateName
        {
            get
            {
                return "SelectItemFromXMLFieldControl";
            }
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                return txt_Ou.Text;
            }
            set
            {
                EnsureChildControls();
                txt_Ou.Text = (string)this.ItemFieldValue;
            }
        }

        public override void Focus()
        {
            EnsureChildControls();
            txt_Ou.Focus();
        }

        protected override void CreateChildControls()
        {
            if (Field == null) return;
            base.CreateChildControls();

            if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display)
                return;

            txt_Ou = (TextBox)TemplateContainer.FindControl("txt_Ou");
            TreeView1 = (TreeView)TemplateContainer.FindControl("TreeView1");
          
            if (!Page.IsPostBack)
            {             
               
                TreeNode node2 = new TreeNode();
                node2.Text = "泉州移动";
                TreeView1.Nodes.Add(node2);
                GetOu("OU=泉州移动", 10,node2);
               
            }

        }
        protected void TreeView1_SelectNodeChanged(object sender, EventArgs e)
        {

        }
        public void GetOu(string ouname, int amstr,TreeNode nodetmp)
        {
            //获取AD的对象
            //搜索组织单位
            DirectoryEntry entry = new DirectoryEntry("LDAP://SHAREPOINT2007.QZIT.COM", "administrator", "你的密码");
            //获取子结点
            System.DirectoryServices.DirectoryEntry subentry = entry.Children.Find(ouname, "organizationalUnit");
           

            //对子对象进行循环
            foreach (DirectoryEntry res in subentry.Children)
            {
                if (res.Name.Substring(0, 3) == "OU=")//判断是否为OU
                {
                    //增加子结点
                    TreeNode node2 = new TreeNode();
                    node2.Text = res.Name.ToString().Replace("OU=", "") ;
                    nodetmp.ChildNodes.Add(node2);
                    //递归调用
                    GetOu(res.Name.ToString()+","+ouname, 10,node2);
                  
                }
            }

        }
    }
}
其中必须确定在AD根结点下建立一个泉州移动的组织单位(也可修改其中代码)

posted @ 2009-01-22 09:55  雷君  阅读(420)  评论(0编辑  收藏  举报