在MOSS中開發一個模塊化的feature

 

moss中的feature功能很強大,本文主要看一下如何開發一個模塊化的feature
比如可以把一個學生管理功能(包括aspx頁面等)開發成一個feature,然後可以在不同的moss網站中有選擇的激活這個feature,激活後就把對應的鏈接加入此網站的首頁上,以此實現功能的動態加載。
首先我們在sharepoint designer中定製兩個aspx頁面:StudentList.aspxUserEdit.aspx
  (
要保證這兩個頁面在moss站點中是能夠正常訪問的)
  
我這裏只是演示feature的功能,就兩個頁面的代碼就不列出了。
然後就是feature配置文件的寫法
對於MOSS中的feature我們一般都要寫兩個配置文件:
<Feature
Id="4292625E-5811-47a4-9B88-58A206C53515"
Title="
學生管理-測試"
Description="
學生管理-測試。"
Scope="Web"
Hidden="false"
ReceiverAssembly="Feature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=48fca413ed4dd464"
ReceiverClass="Feature.StudentFeature"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml"/>
</ElementManifests>
</Feature>
  
其中ReceiverAssemblyReceiverClass是指定feature激活等操作時對應的代碼文件的,後面會提到
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Path
指的是Template/Features下的實際路徑 Url指的是MOSS站點的虛擬路徑 -->
<Module Path="Student" Url="Student" >
<!--
一覽頁面 -->
<File Url="StudentList.aspx"  Type="Ghostable" />
<!--
編輯頁面 -->
<File Url="UserEdit.aspx" Type="Ghostable" />
</Module>
</Elements>
  
特別要注意的是我文件中的註釋
然後就是Feature對應的Receiver代碼
主要作用是在feature激活時把鏈接加到網站首頁上,在停止時把feature對應的aspx頁面從網站中刪除(激活feature時會根據配置自動把文件複製到MOSS網站裏)
  public class StudentFeature : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// get a hold off current site in context of feature activation
            SPWeb site = (SPWeb)properties.Feature.Parent;
SPNavigationNodeCollection topNav = site.Navigation.QuickLaunch;
// create dropdown menu for custom site pages
            topNav[0].Children.AddAsLast(new SPNavigationNode("
學生管理", "Student/StudentList.aspx"));
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb site = (SPWeb)properties.Feature.Parent;
// delete folder of site pages provisioned during activation
            SPFolder sitePagesFolder = site.GetFolder("Student");
sitePagesFolder.Delete();
SPNavigationNodeCollection topNav = site.Navigation.QuickLaunch;
for (int i = topNav[0].Children.Count - 1; i >= 0; i--)
{
if (topNav[0].Children.Title == "
學生管理")
{
// delete node
                    topNav[0].Children.Delete();
}
}
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
}

代碼沒什麼特殊的地方,就是使用了MOSSobject model進行操作
最後就是進行部署了
1@SET TEMPLATEDIR="c:/program files/common files/microsoft shared/web server extensions/12/Template/Features"
2@SET STSADM="c:/program files/common files/microsoft shared/web server extensions/12/bin/stsadm"
3@SET GACUTIL="c:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Bin/gacutil.exe"
4
5Echo Installing CustomSitePages.dll in GAC
6%GACUTIL% -if Feature.dll
7
8Echo Copying files to TEMPLATE directory
9xcopy /e /y TEMPLATE/* %TEMPLATEDIR%
10
11Echo Installing feature
12%STSADM% -o installfeature -filename  Student/feature.xml -force
13
14IISRESET
15REM cscript c:/windows/system32/iisapp.vbs /a "SharePointDefaultAppPool" /r
  
安裝好之後在需要的網站上激活這個feature就可以了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章