技术开发 频道

在MOSS中开发一个模块化的feature


【IT168技术文档】

  moss中的feature功能很强大,本文主要看一下如何开发一个模块化的feature。
  比如可以把一个学生管理功能(包括aspx页面等)开发成一个feature,然后可以在不同的moss网站中有选择的激活这个feature,激活后就把对应的链接加入此网站的首页上,以此实现功能的动态加载。

  首先我们在sharepoint designer中定制两个aspx页面:StudentList.aspx和UserEdit.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>
  其中ReceiverAssembly和ReceiverClass是指定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[i].Title == "学生管理") { // delete node topNav[0].Children[i].Delete(); } } } public override void FeatureInstalled(SPFeatureReceiverProperties properties) { } public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { } }
0
相关文章