欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

MOSS小技巧入门(1):轻松打造与部署特性功能步骤详解

最编程 2024-07-23 16:27:09
...

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要实现的。

然后我们在添加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 >

这个就是我们自定义的菜单项了。

在增加一个类文件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和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注册到GAC中

4.重启IIS:iisreset

现在我们去网站集功能中查看,你可以激活该feature,激活的时候会执行FeatureActivated中的代码

当然这一部署过程我们可以使用一个批处理来完成,注意路径的更改:

@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

下面就是我们最终要实现的效果了