Feature是MOSS中一个重要的东西,Feature 使得基于sharepoint的用户功能定制变得简单和方便。我们使用Feature来实现网站的MasterPage的更换。写一个方法,继承SPFeatureReceiver这个抽象类,实现抽象方法即可。四个方法:FeatureActivated,FeatureDeactivating,FeatureInstalled,FeatureUninstalling,分别是激活反激活,安装反安装对应的事件。我们实现FeatureActivated和FeatureDeactivating的方法。代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
到这里代码部分就写好了。加强签名,然后扔到GAC里。然后进行下一步,建一个Feature.xml。我们可以将Feature定义在几个范围上:Farm(服务器场),WebApplication(Web应用),Site(网站集),Web(网站),但通常定义在网站集或网站范围上。我们的Feature.xml内容如下:
<Feature
xmlns="http://schemas.microsoft.com/sharepoint/"
Id="3b387ef0-01c7-4980-bfa2-e6d37b986bac"
Title="change my masterpage"
Description="change my masterpage"
Scope="Site"
ReceiverAssembly="MySiteFeatureManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7c96adf2aa593794"
ReceiverClass="MySiteFeatureManager.MySiteFeature"
>
<ElementManifests>
<ElementFile Location="NewMasterPage.master"/>
<ElementManifest Location="element.xml"/>
</ElementManifests>
<Properties>
<Property Key="NewMasterPage" Value="NewMasterPage.master"/>
</Properties>
</Feature>
属性介绍:
Id:Guid格式,这个可以使用VS2005或者别的工具来生成一个。
Title:Feature的标题。
Description:Feature的内容描述。
Scope:Feature的功能范围(Farm,WebApplication,Site,Web)
ReceiverAssembly:引用的程序集
ReceiverClass:引用程序集的类
ElementManifests这里我们可以看到<ElementFile Location="NewMasterPage.master"/>,所以我们在同一文件夹放一个NewMasterPage.master,masterpage注意问题请查阅相关文章;还有<ElementManifest Location="element.xml"/>,所以还要在同一文件夹放一element.xml文件,这个文件的内容如下:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="NewMasterPage" RootWebOnly="true" Url="_catalogs/masterpage">
<File Url="NewMasterPage.master" Type="GhostableInLibrary" />
</Module>
</Elements>
这里的意思是告诉它,我们的NewMasterPage.master上传到了哪个目录。
这里<ElementManifest Location="element.xml"/>的element.xml文件名可以随便自己写,只要有这个对应xml文件即可,但是Feature.xml,名称是不可改变的。
下面安装和激活这个Feature:
在目录C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES中新建一个文件夹,如:MySiteFeature。将这两个xml放入这个文件夹。
使用如下命令:
安装:stsadm –o installfeature –filename MySiteFeature\feature.xml
激活到网站集:stsadm –o activatefeature –filename MySiteFeature\feature.xml -url http://moss:81
这样Feature就安装好了。现在我们已经做好了可以更换MasterPage的Feature,在网站集的管理中的网站集功能中可以看到我们的Feature,可以随时激活或者停止我们的Feature的功能。在上面代码的基础上,可以扩展,比如根据用户组来判断是用哪个MasterPage,MasterPage的定义在xml里定义好就行了。
![](/images/cnblogs_com/ycc/omygod.gif)