本教學將逐步引導您建立一個自訂動作 Feature,在 SharePoint 網站範圍內為編輯通知表單的工具列新增一個自訂按鈕。透過這個範例,您將了解 Feature 的基本結構、元素定義以及部署與啟動的完整流程。
在本機磁碟的下列路徑建立一個名為 SimpleFormToolbarButton 的資料夾:
Local_Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\SimpleFormToolbarButton
若透過 Windows 檔案總管(右鍵 → 新增 → 資料夾)建立目錄,新資料夾不會繼承父目錄權限,導致 Feature 部署後在網站設定或列表檢視等頁面拋出例外。解決方法:
md SimpleFormToolbarButton 建立目錄,可自動繼承權限。在剛建立的資料夾中新增 Feature.xml,內容如下:
<Feature Title="New Simple Form Toolbar Button"
Scope="Web"
Id="GUID"
xmlns="https://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="Elements.xml" />
</ElementManifests>
</Feature>
注意: 必須將 GUID 佔位符替換為真實的 GUID。可執行 guidgen.exe(位於 Local_Drive:\Program Files\Microsoft Visual Studio 8\Common7\Tools)產生新的 GUID。
在同一資料夾建立 Elements.xml,定義一個出現在編輯表單工具列的按鈕:
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<CustomAction Title="Simple"
Sequence="10"
RegistrationType="List"
RegistrationId="104"
Location="EditFormToolbar"
Id="Simple Toolbar">
<UrlAction Url="/_layouts/SampleUrl.aspx" />
</CustomAction>
</Elements>
/_layouts/SampleUrl.aspx。在 TEMPLATE\LAYOUTS 目錄下建立 SampleUrl.aspx,內容僅輸出 "Hello, world!":
<HTML>
<head runat="server">
<title>Sample Page</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
開啟命令提示字元,依序執行下列 stsadm 指令:
stsadm -o installfeature -filename SimpleFormToolbarButton\Feature.xml
stsadm -o activatefeature -filename SimpleFormToolbarButton\Feature.xml -url https://Server/Site/Subsite
installfeature 將 Feature 註冊到伺服器陣列。activatefeature 在指定網站(Web 範圍)啟動 Feature。瀏覽至任一通知項目的編輯表單,您將在工具列看到名為 "Simple" 的新按鈕。點擊該按鈕即會導向 SampleUrl.aspx 並顯示 "Hello, world!"。