SharePoint Central Administration Feature开发要点
在SharePoint开发中,我们有时需要在Central Administration中部署Feature,这样的Feature(以下简称CA Feature)通常都是完成一些全局的管理操作,比如说公司开发出多个SharePoint可重用的组件,准备以产品的形式对外销售,这个时候就得通过部署一些CA Feature去对整个Farm中已经安装的SharePoint组件授权进行管理。
作为部署到Central Administration的Feature,与一般的Feature有些不同,现在总结如下:
1. Feature自动激活及隐藏
因为CA Feature用于管理,所以最好在部署的时候自动将其激活,同时将该Feature隐藏,不让它在Feature管理列表中显示。这样SharePoint Admin也省去了一些不必要的麻烦,同时也不让他有机会在别的站点上激活CA Feature。如下feature.xml示例:
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
AutoActivateInCentralAdmin="TRUE"
Hidden="TRUE"
Scope="WebApplication"
ReceiverAssembly="Demo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0ae3210f1d183ecb"
ReceiverClass="Demo.CentralAdminOnlyFeatureReceiver"
Title="Demo"
ActivateOnDefault="FALSE"
Id="3e624731-3917-49ea-916a-292c3689f188">
<ElementManifests>
<ElementManifest Location="Elements.xml" />
</ElementManifests>
</Feature>
注意:对于Farm Scope的Feature,AutoActivateInCentralAdmin这个属性是无效的,所以这里把它设置成WebApplication,不然CA Feature无法在Central Administration自动激活。
2. SPFeatureReceiver
前面我们把Feature Scope设置成WebApplication,如果不想让CA Feature在非Central Administration的Web Application上激活,那么我们需要在CA Feature激活的时候作一下检查,限制其激活,在上面的feature.xml中已将其添加进来,下面给出该Feature Receiver的定义。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace Demo
{
public class CentralAdminOnlyFeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
if (!webApp.IsAdministrationWebApplication)
{
Guid featureId = properties.Feature.DefinitionId;
webApp.Features.Remove(featureId, true);
webApp.Update();
throw new SPException(string.Format("You can activate the feature with ID {0} on the Central Administration Web Application only!", featureId));
}
}
}
}
这样当用户在非Central Administration的Web Application上激活CA Feature的时候,会立即将这个已激活的Feature移除,并且显示相应的错误信息。