在 SharePoint 網站部署時,常需預先放置特定檔案(如母版頁、預設頁面、圖片等)。WSS 3.0 提供 Feature 中的 <Module> 元素,讓開發者能以宣告式方式將檔案佈署至文件庫或任意路徑。本文詳細說明 Module 元素用法、GhostableInLibrary 與 Ghostable 差異、以及實際範例。
<File> 子元素,定義要佈署的檔案集合。GhostableInLibrary:佈署至文件庫時使用,會在庫中建立對應的列表項目。Ghostable:佈署至文件庫以外路徑(如網站根目錄)時使用。<Feature Id="947F6C32-E898-45f5-A595-C46F07DA6BF7"
Title="New Master Pages" Scope="Web"
xmlns="https://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="newmasterpages.xml" />
</ElementManifests>
</Feature>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<Module Name="MPages" List="116" Url="_catalogs/masterpage">
<File Url="Minimal.master" Type="GhostableInLibrary" />
<File Url="ContosoRed.master" Type="GhostableInLibrary" />
</Module>
</Elements>
List="116" 指定目標為母版頁庫 (Master Page Gallery),Url="_catalogs/masterpage" 為庫的相對路徑。
此範例展示如何在網站定義中佈署首頁,包含列表視圖 Web Part、一般 Web Part (AllUsersWebPart) 及導覽列設定:
<Module Name="DWS" Url="" Path="dws">
<File Url="default.aspx">
<View List="104" BaseViewID="3" WebPartZoneID="Top"/>
<View List="103" BaseViewID="3" WebPartZoneID="Right" WebPartOrder="2"/>
<View List="101" BaseViewID="6" WebPartZoneID="Left">
<![CDATA[
<WebPart xmlns="https://schemas.microsoft.com/WebPart/v2">
<Title>Members</Title>
</WebPart>
]]>
</View>
<View List="107" BaseViewID="7" WebPartZoneID="Left" WebPartOrder="2"/>
<AllUsersWebPart WebPartZoneID="Right" WebPartOrder="1">
<![CDATA[
<WebPart xmlns="https://schemas.microsoft.com/WebPart/v2">
<Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.MembersWebPart</TypeName>
<Title>Members</Title>
<Description>Use the Members Web Part to see a list of the site members and their online status.</Description>
<FrameType>Standard</FrameType>
<IsVisible>true</IsVisible>
</WebPart>
]]>
</AllUsersWebPart>
<NavBarPage Name="Home" ID="1002" Position="Start"> </NavBarPage>
<NavBarPage Name="Home" ID="0" Position="Start"> </NavBarPage>
</File>
</Module>
說明:Path="dws" 表示來源檔案位於網站定義目錄下的 dws 子資料夾,但佈署目標 Url="" 為網站根目錄。
透過 Module 的 Url 屬性可直接建立資料夾結構:
<Module Name="MyImage" Url="MyImages" Path="">
<File Url="MyImage.jpg" Type="GhostableInLibrary" />
</Module>
此例會在網站中建立 MyImages 資料夾,並放入 MyImage.jpg。
Module 元素是 SharePoint 檔案佈署的基石,善用 GhostableInLibrary / Ghostable、結合 View 與 AllUsersWebPart,即可在 Feature 或網站定義中完成複雜的頁面與檔案預置需求。