SharePoint 的委派控制項 (Delegate Control) 允許開發者以 Feature 方式替換頁面上的特定控制項 (如搜尋框、導覽列等),而不需修改母版頁。本文示範如何建立 Feature 取代預設的 SmallSearchInputBox 搜尋框圖示。
複製 \TEMPLATE\FEATURES\ContentLightup 資料夾至:
Local_Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\NewSearchBox
更新 GUID (使用 guidgen.exe 產生)、標題、描述、範圍設為 WebApplication,並指向自訂控制項定義檔:
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="{NEW_GUID}"
Title="New Search Image"
Description="Points at which third parties light up in content pages"
Version="12.0.0.0"
Scope="WebApplication"
xmlns="https://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="controls\searcharea.xml" />
</ElementManifests>
</Feature>
指定要替換的委派控制項 ID (SmallSearchInputBox)、順序 (Sequence) 以及自訂使用者控制項 (.ascx) 路徑:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<Control
Id="SmallSearchInputBox"
Sequence="100"
ControlSrc="~/_controltemplates/mySearchArea.ascx">
<Property Name="SearchTextToolTip">Search this site</Property>
<Property Name="SearchImageToolTip">Go</Property>
</Control>
</Elements>
\TEMPLATE\CONTROLTEMPLATES\SearchArea.ascx 至同目錄下,重新命名為 mySearchArea.ascx。mySearchArea.ascx,尋找 <img> 標籤,將 src 屬性指向新圖片。測試時可使用內建圖片 (如 /_layouts/images/FOLDER.GIF) 替換原本的 gosearch.gif。關鍵片段:
<img border='0' src="/_layouts/images/FOLDER.GIF" style='vertical-align: 1' ... />
stsadm -o installfeature -filename NewSearchBox\feature.xml
stsadm -o activatefeature -filename NewSearchBox\feature.xml -url https://Server/Site/Subsite
瀏覽該子網站,搜尋框右側的「前往」圖示即已替換為自訂圖片。
<Control> 元素的 Id、Sequence、ControlSrc 三屬性控制。Sequence 數值越小優先權越高,若多個 Feature 針對同一控制項,最小者生效。_controltemplates 目錄,採用 ~/_controltemplates/ 相對路徑引用。