天天看點

MOSS點滴(1):如何開發和部署feature

Features 是MOSS 2007以開箱即用的一套新功能,Features 存儲在SharePoint伺服器的如下路徑下:C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES。每個Featrue在此路徑下有自己的子目錄,在每一個Feature子目錄下會發現名字為Feature.xml的檔案,它存儲一些關于Featrue的metadata資訊。

下面我就利用feature來實作一個小功能,在"網站操作"中添加自定義的菜單,首先使用VS2005建立一個HelloWorld的類庫項目,然後添加檔案夾Helloworld,在檔案夾中添加feature.xml檔案代碼如下:

<Feature Id="B2CB42E2-4F0A-4380-AABA-1EF9CD526F20" Title="Hello World Feature" Description="這是我的第一個Feature" Scope="Web" Hidden="FALSE" ImageUrl="TPG\canteen.gif" ReceiverAssembly="HelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b38a04419cc857d9" ReceiverClass="HelloWorld.FeatureReceiver" xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests>

<ElementManifest Location="elements.xml" />

</ElementManifests>

</Feature>

下面我們來說明下包含在Featrue 元素中的metadata 資訊。

ID: 一個GUID,用于唯一辨別這個Feature,這個可以使用GUID的生成工具得到;

Scope:其值可以是Web或Site,它指明了這個Feature是應用于整個的Site Collection還是僅僅用于單獨的一個子站點。如果Scope="Web",則在[網站操作—網站設定—網站管理—網站功能]下激活,如果Scope="Site"則要在[網站操作—網站設定—網站管理—網站集功能]下激活。

Hidden:值可以是True或False.該設定指定了這個Feature是否在Site Feature頁面上顯示。

DefaultResourceFile: 資源檔案名字,Feature依賴它提供其它附加的配置資訊。

<ElementManifests>元素:這個元素包含了另一個XML檔案的位置,而這個檔案包含的<Elemnets>的内容是Feature要實作的。

然後我們在添加<b>elements.xml</b>檔案,代碼如下:

&lt;Elements xmlns="http://schemas.microsoft.com/sharepoint/"&gt;

&lt;CustomAction Id="SiteActionsToolbar" GroupId="SiteActions" Location="Microsoft.SharePoint.StandardMenu" Sequence="100" Title="Hello World" Description="使用feature方式自定義菜單" ImageUrl="_layouts/images/crtsite.gif"&gt;

&lt;UrlAction Url="http://msdn.microsoft.com" /&gt;

&lt;/CustomAction&gt;

&lt;/Elements&gt;

這個就是我們自定義的菜單項了。

在增加一個類檔案FeatureReceiver.cs,代碼如下:

   using System; 

using Microsoft.SharePoint; 

namespace HelloWorld 

  public class FeatureReceiver : SPFeatureReceiver 

   { 

       public override void FeatureInstalled(SPFeatureReceiverProperties properties) { } 

       public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { } 

       public override void FeatureActivated(SPFeatureReceiverProperties properties) 

      { 

           SPWeb site = (SPWeb)properties.Feature.Parent; 

           site.Properties["OriginalTitle"] = site.Title; 

           site.Properties.Update(); 

           site.Title = "Hello World Modify"; 

           site.Update(); 

      } 

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 

   { 

       SPWeb site = (SPWeb)properties.Feature.Parent; 

       site.Title = site.Properties["OriginalTitle"]; 

       site.Update(); 

    } 

  } 

}

SPFeatureReceiver 類中定義當安裝、激活、停用或解除安裝 Web 部件 Feature 時,MOSS會觸發這些事件,在此我們要設定feature.xml中的ReceiverAssembly 和 ReceiverClass 的屬性。 這些屬性指向一個功能接收器的托管類。PublicKeyToken是HelloWorld的key可以在VS2005指令行下使用"sn -t HelloWorld"來得到。

基本上我們的任務就完成了,現在我們就要開始部署了,需要通過以下步驟

1.将HelloWorl檔案夾(其中包含feature.xml和<b>elements.xml</b>檔案)拷貝到C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES檔案夾下。

2.STSADM -o InstallFeature -filename HelloWorld\feature.xml -force

<a href="http://www.cnblogs.com/images/cnblogs_com/carysun/WindowsLiveWriter/MOSS1feature_11EC0/feature1.jpg"></a>

3.使用"gacutil -if 程式集名"将HelloWorld.dll注冊到GAC中

<a href="http://www.cnblogs.com/images/cnblogs_com/carysun/WindowsLiveWriter/MOSS1feature_11EC0/feature3.jpg"></a>

4.重新開機IIS:iisreset

現在我們去網站集功能中檢視,你可以激活該feature,激活的時候會執行FeatureActivated中的代碼

<a href="http://www.cnblogs.com/images/cnblogs_com/carysun/WindowsLiveWriter/MOSS1feature_11EC0/feature2.jpg"></a>

當然這一部署過程我們可以使用一個批處理來完成,注意路徑的更改:

@SET TEMPLATEDIR="c:\program files\common files\microsoft shared\web server extensions\12\Template" 

@SET STSADM="c:\program files\common files\microsoft shared\web server extensions\12\bin\stsadm" 

@SET GACUTIL="d:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" 

Echo Installing HelloWorld.dll in GAC 

%GACUTIL% -if bin\debug\HelloWorld.dll 

Echo Copying files to TEMPLATE directory 

xcopy /e /y TEMPLATE\* %TEMPLATEDIR% 

Echo Installing feature 

%STSADM% -o installfeature -filename  HelloWorld\feature.xml -force 

IISRESET 

REM cscript c:\windows\system32\iisapp.vbs /a "SharePointDefaultAppPool" /r

下面就是我們最終要實作的效果了

<a href="http://www.cnblogs.com/images/cnblogs_com/carysun/WindowsLiveWriter/MOSS1feature_11EC0/feature4.jpg"></a>

本文轉自生魚片部落格園部落格,原文連結:http://www.cnblogs.com/carysun/archive/2008/04/16/feature.html,如需轉載請自行聯系原作者