MOSS点滴(1):如何开发和部署feature
Features 是MOSS 2007以開(kāi)箱即用的一套新功能,Features 存儲(chǔ)在SharePoint服務(wù)器的如下路徑下:C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES。每個(gè)Featrue在此路徑下有自己的子目錄,在每一個(gè)Feature子目錄下會(huì)發(fā)現(xiàn)名字為Feature.xml的文件,它存儲(chǔ)一些關(guān)于Featrue的metadata信息。
下面我就利用feature來(lái)實(shí)現(xiàn)一個(gè)小功能,在"網(wǎng)站操作"中添加自定義的菜單,首先使用VS2005建立一個(gè)HelloWorld的類庫(kù)項(xiàng)目,然后添加文件夾Helloworld,在文件夾中添加feature.xml文件代碼如下:
?
<Feature?Id="B2CB42E2-4F0A-4380-AABA-1EF9CD526F20"Title="Hello?World?Feature"Description="這是我的第一個(gè)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>
下面我們來(lái)說(shuō)明下包含在Featrue 元素中的metadata 信息。
ID: 一個(gè)GUID,用于唯一標(biāo)識(shí)這個(gè)Feature,這個(gè)可以使用GUID的生成工具得到;
Scope:其值可以是Web或Site,它指明了這個(gè)Feature是應(yīng)用于整個(gè)的Site Collection還是僅僅用于單獨(dú)的一個(gè)子站點(diǎn)。如果Scope="Web",則在[網(wǎng)站操作—網(wǎng)站設(shè)置—網(wǎng)站管理—網(wǎng)站功能]下激活,如果Scope="Site"則要在[網(wǎng)站操作—網(wǎng)站設(shè)置—網(wǎng)站管理—網(wǎng)站集功能]下激活。
Hidden:值可以是True或False.該設(shè)置指定了這個(gè)Feature是否在Site Feature頁(yè)面上顯示。
DefaultResourceFile: 資源文件名字,Feature依賴它提供其它附加的配置信息。
<ElementManifests>元素:這個(gè)元素包含了另一個(gè)XML文件的位置,而這個(gè)文件包含的<Elemnets>的內(nèi)容是Feature要實(shí)現(xiàn)的。
然后我們?cè)谔砑?strong>elements.xml文件,代碼如下:
?
<Elements?xmlns="http://schemas.microsoft.com/sharepoint/"><CustomAction?Id="SiteActionsToolbar"GroupId="SiteActions"Location="Microsoft.SharePoint.StandardMenu"Sequence="100"Title="Hello?World"Description="使用feature方式自定義菜單"ImageUrl="_layouts/images/crtsite.gif">
<UrlAction?Url="http://msdn.microsoft.com"/>
</CustomAction>
</Elements>
這個(gè)就是我們自定義的菜單項(xiàng)了。
在增加一個(gè)類文件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 類中定義當(dāng)安裝、激活、停用或卸載 Web 部件 Feature 時(shí),MOSS會(huì)觸發(fā)這些事件,在此我們要設(shè)置feature.xml中的ReceiverAssembly 和 ReceiverClass 的屬性。 這些屬性指向一個(gè)功能接收器的托管類。PublicKeyToken是HelloWorld的key可以在VS2005命令行下使用"sn -t HelloWorld"來(lái)得到。
基本上我們的任務(wù)就完成了,現(xiàn)在我們就要開(kāi)始部署了,需要通過(guò)以下步驟
1.將HelloWorl文件夾(其中包含feature.xml和elements.xml文件)拷貝到C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES文件夾下。
2.STSADM -o InstallFeature -filename HelloWorld\feature.xml -force
3.使用"gacutil -if 程序集名"將HelloWorld.dll注冊(cè)到GAC中
4.重啟IIS:iisreset
現(xiàn)在我們?nèi)ゾW(wǎng)站集功能中查看,你可以激活該feature,激活的時(shí)候會(huì)執(zhí)行FeatureActivated中的代碼
當(dāng)然這一部署過(guò)程我們可以使用一個(gè)批處理來(lái)完成,注意路徑的更改:
@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 http://www.cnblogs.com/carysun/archive/2008/04/16/feature.html
轉(zhuǎn)載于:https://www.cnblogs.com/Areas/archive/2012/05/22/2513648.html
總結(jié)
以上是生活随笔為你收集整理的MOSS点滴(1):如何开发和部署feature的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Bob的烦恼II 逃离迷宫
- 下一篇: 双机热备工作原理图