網(wǎng)上一有一篇文章,來(lái)很仔細(xì)的描述如何增加自定義字段的.
以下是內(nèi)容,我復(fù)制過(guò)來(lái),大家可以先試試,然后再做基于TREEVIEW的OU組織結(jié)構(gòu)菜單
--------------------------
MOSS2007的自定義字段類型是一個(gè)非常有用的功能,但在網(wǎng)上相關(guān)的實(shí)例介紹很少,所以下面就
一步一步地介紹怎樣來(lái)創(chuàng)建一個(gè)自定義字段類型,我們的目標(biāo)是:實(shí)現(xiàn)一個(gè)具有可配置性的下拉列表框,
其選擇項(xiàng)目是讀取XML文件獲得。
實(shí)現(xiàn)一個(gè)自定義字段類型主要需要完成三種文件的編制:1)定義類型的XML文件;2)定義展現(xiàn)模板
*.ascx文件;3)定義后臺(tái)代碼程序集。
這里因?yàn)槲覀兿胍獙?shí)現(xiàn)一個(gè)可讀取XML文件的下拉列表框,所以我們首先還要定義一個(gè)配置文件
SelectItemFromXMLConfig.xml。
XML代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<SelectItems>
<Item>選項(xiàng)一</Item>
<Item>選項(xiàng)二</Item>
<Item>選項(xiàng)三</Item>
<Item>選項(xiàng)四</Item>
<Item>選項(xiàng)五</Item>
</SelectItems>
然后,我們可以打開(kāi)Visual Studio2005,然后添加一個(gè)類庫(kù),如果你已經(jīng)安裝了擴(kuò)展模板也可以直接選擇
創(chuàng)建“Field Control”類型的項(xiàng)目。建議下載微軟的SharePoint工具包:Windows SharePoint Services 3.0 Tools: Visual Studio 2005 Extensions.
這里我們新建一個(gè)名叫SelectItemFromXMLField的類庫(kù),其中包含兩個(gè)類文件:SelectItemFromXML.Field.cs
和SelectItemFromXML.FieldControl.cs。
SelectItemFromXML.Field.cs完整代碼如下:
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Security;
namespace SelectItemFromXML
{
[CLSCompliant(false)]
[Guid("92f76873-537c-4c13-abaf-2d47bdc7e2be")]
public class SelectItemFromXMLField : SPFieldChoice
{
public SelectItemFromXMLField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public SelectItemFromXMLField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
}
public override BaseFieldControl FieldRenderingControl
{
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
get
{
BaseFieldControl fieldControl = new SelectItemFromXMLFieldControl();
fieldControl.FieldName = this.InternalName;
return fieldControl;
}
}
}
}
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;
namespace SelectItemFromXML
{
[CLSCompliant(false)]
[Guid("eb269bd9-0d3e-4aa2-b88f-37fbccb0d7d8")]
public class SelectItemFromXMLFieldControl : BaseFieldControl
{
protected DropDownList cboList;
protected override string DefaultTemplateName
{
get
{
return "SelectItemFromXMLFieldControl";
}
}
public override object Value
{
get
{
EnsureChildControls();
return cboList.SelectedValue.ToString();
}
set
{
EnsureChildControls();
cboList.SelectedValue = (string)this.ItemFieldValue;
}
}
public override void Focus()
{
EnsureChildControls();
cboList.Focus();
}
protected override void CreateChildControls()
{
if (Field == null) return;
base.CreateChildControls();
if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display)
return;
cboList = (DropDownList)TemplateContainer.FindControl("cboList");
if (cboList == null)
{
throw new ArgumentException("cboList is null. Corrupted SelectItemFromXMLFieldControl.ascx file.");
}
if (!Page.IsPostBack)
{
string configFile = Environment.CurrentDirectory + "\\SelectItemFromXMLConfig.xml";
if (!File.Exists(configFile))
{
cboList.Items.Add(new ListItem("Config file not found!","Error"));
cboList.ToolTip = configFile + " not found!";
}
else
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(configFile);
XmlNodeList nodelist = xmlDoc.DocumentElement.ChildNodes;
foreach (XmlNode node in nodelist)
{
cboList.Items.Add(new ListItem(node.InnerText, node.InnerText));
}
}
}
}
}
}
如果你使用模板創(chuàng)建項(xiàng)目,則上面的代碼大部分會(huì)被自動(dòng)生成。
值得注意的幾個(gè)地方是:
?。保㏒electItemFromXMLField類將繼承SPFieldChoice基類,并重寫(xiě)FieldRenderingControl方法。
?。玻㏒electItemFromXMLFieldControl 類將繼承BaseFieldControl基類。
?。常〤reateChildControls方法中讀取XML配置文件,并把選項(xiàng)綁定到下拉列表框(DropDownList)子控件上。
?。矗┥厦娲a中使用了Environment.CurrentDirectory語(yǔ)句,后面部署時(shí)要把配置文件放到
代碼編寫(xiě)完成之后,要對(duì)程序集進(jìn)行強(qiáng)名稱設(shè)置,并編譯。
接下來(lái),我們來(lái)制作展現(xiàn)模板。你可以在當(dāng)前解決方案中添加一個(gè)文本文件“SelectItemFromXMLFieldControl.ascx”。
然后打該文件編寫(xiě)代碼如下:
<% @ 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:DropDownList runat="server" ID="cboList"/>
</Template>
</SharePoint:RenderingTemplate>
值得注意的地方是:上面代碼中DropDownList的ID是"cboList",這個(gè)名字曾經(jīng)被后臺(tái)類SelectItemFromXMLFieldControl使用
TemplateContainer.FindControl方法尋找過(guò),所以不要寫(xiě)錯(cuò)。
OK,最后,我們可以在當(dāng)前解決方案中添加一個(gè)XML文件“FLDTYPES_SelectItemFromXmlField.xml”。
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">SelectItemFromXML</Field>
<Field Name="ParentType">Choice</Field>
<Field Name="TypeDisplayName">Select Item From XML</Field>
<Field Name="TypeShortDescription">Select Item From XML</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowInListCreate">TRUE</Field>
<Field Name="ShowInSurveyCreate">TRUE</Field>
<Field Name="ShowInDocumentLibraryCreate">TRUE</Field>
<Field Name="ShowInColumnTemplateCreate">TRUE</Field>
<Field Name="FieldTypeClass">SelectItemFromXML.SelectItemFromXMLField,SelectItemFromXMLField,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=ad167643f06d0c9c</Field>
</FieldType>
</FieldTypes>
值得注意的地方是:
?。保┰摱x文件的文件名一定要以“FLDTYPES”開(kāi)頭。
?。玻苅eldTypeClass項(xiàng)中的值是類的全名稱和程序集的全名稱,可以使用Reflector獲得。
到這里,我們需要編寫(xiě)的所有文件都已經(jīng)編寫(xiě)完成了,只要做簡(jiǎn)單的部署就可以了。
步驟如下:
?。保┌褟?qiáng)名稱編譯好的SelectItemFromXMLField程序集加入到GAC。
?。玻┌杨愋投x文件FLDTYPES_SelectItemFromXmlField.xml拷貝到c:\program...\12\TEMPLATE\XML目錄下。
3)把模板文件SelectItemFromXMLFieldControl.ascx拷貝到c:\program...\12\TEMPLATE\CONTROLTEMPLATES目錄下。
4)重啟動(dòng)IIS。(可以使用命令行iisreset)
5)把該控件的配置文件SelectItemFromXMLConfig.xml拷貝到C:\Windows\System32\inetsrv目錄下。
值得注意的地方是:
前四步是部署自定義字段類型的一般步驟。第五步是針對(duì)這個(gè)實(shí)例的特殊步驟。你其實(shí)可以把配置文件放在你想放的任何位置,只要在前面編寫(xiě)SelectItemFromXMLFieldControl類的CreateChildControls方法時(shí)指定你想放的位置即可。
OK,到此自定義字段類型的過(guò)程已經(jīng)結(jié)束。你可以在創(chuàng)建MOSS的某個(gè)列表欄時(shí),發(fā)現(xiàn)一個(gè)新的類型“Select Item From XML”。選擇該類型創(chuàng)建一個(gè)新欄后,當(dāng)往該列表中添加項(xiàng)目時(shí),會(huì)發(fā)現(xiàn)添加頁(yè)面中對(duì)應(yīng)新欄的輸入方式是一個(gè)下拉列表框,其中的選項(xiàng)就是我們?cè)冢兀停膛渲梦募卸x的選項(xiàng)了!
----------------------------------------
接下來(lái),我來(lái)告訴大家如何做TREEVIEW的樹(shù)型菜單.
首先修改.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 = "泉州移動(dòng)";
TreeView1.Nodes.Add(node2);
GetOu("OU=泉州移動(dòng)", 10,node2);
}
}
protected void TreeView1_SelectNodeChanged(object sender, EventArgs e)
{
}
public void GetOu(string ouname, int amstr,TreeNode nodetmp)
{
//獲取AD的對(duì)象
//搜索組織單位
DirectoryEntry entry = new DirectoryEntry("LDAP://SHAREPOINT2007.QZIT.COM", "administrator", "你的密碼");
//獲取子結(jié)點(diǎn)
System.DirectoryServices.DirectoryEntry subentry = entry.Children.Find(ouname, "organizationalUnit");
//對(duì)子對(duì)象進(jìn)行循環(huán)
foreach (DirectoryEntry res in subentry.Children)
{
if (res.Name.Substring(0, 3) == "OU=")//判斷是否為OU
{
//增加子結(jié)點(diǎn)
TreeNode node2 = new TreeNode();
node2.Text = res.Name.ToString().Replace("OU=", "") ;
nodetmp.ChildNodes.Add(node2);
//遞歸調(diào)用
GetOu(res.Name.ToString()+","+ouname, 10,node2);
}
}
}
}
}
其中必須確定在AD根結(jié)點(diǎn)下建立一個(gè)泉州移動(dòng)的組織單位(也可修改其中代碼)