using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace SPSiteHanlder.Features.WebFeature
{
/// <summary>
/// 此类用于处理在激活、停用、安装、卸载和升级功能的过程中引发的事件。
/// </summary>
/// <remarks>
/// 附加到此类的 GUID 可能会在打包期间使用,不应进行修改。
/// </remarks>
[Guid("aa46c131-e224-492a-a623-db98662fafcc")]
public class WebFeatureEventReceiver : SPFeatureReceiver
{
// 取消对以下方法的注释,以便处理激活某个功能后引发的事件。
private const string WebConfigModificatinOwner = "SPSiteHanlder.LogonModule";
private readonly SPWebConfigModification[] Modifications ={
new SPWebConfigModification(){
Owner=WebConfigModificatinOwner,
Name="add[@name=‘LogonModule‘]",
Type=SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Path="configuration/system.webServer/modules",
Sequence=0,
Value=string.Format("<add name=‘LogonModule‘ type=‘SPSiteHanlder.LogonModule,{0}‘ />",Assembly.GetExecutingAssembly().FullName)
}
};
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
if (webApp != null)
{
AddWebConfig(webApp, Modifications);
}
}
// 取消对以下方法的注释,以便处理在停用某个功能前引发的事件。
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
if (webApp != null)
{
RemoveWebConfig(webApp, "SPSiteHanlder.LogonModule");
}
}
/// <summary>
/// 添加webconfig
/// </summary>
/// <param name="webApp"></param>
/// <param name="modifications"></param>
protected void AddWebConfig(SPWebApplication webApp,IEnumerable<SPWebConfigModification> modifications)
{
foreach (SPWebConfigModification a in modifications)
{
webApp.WebConfigModifications.Add(a);
}
webApp.Update();
webApp.WebService.ApplyWebConfigModifications();
}
protected void RemoveWebConfig(SPWebApplication webApp, string owner)
{
Collection<SPWebConfigModification> modifications = webApp.WebConfigModifications;
Collection<SPWebConfigModification> removeModifications = new Collection<SPWebConfigModification>();
int count = modifications.Count;
for (int i = 0; i < count; i++)
{
var a = modifications[i];
if (a.Owner == owner)
{
removeModifications.Add(a);
}
}
if (removeModifications.Count > 0)
{
foreach (SPWebConfigModification b in removeModifications)
{
webApp.WebConfigModifications.Remove(b);
}
webApp.Update();
webApp.WebService.ApplyWebConfigModifications();
}
}
// 取消对以下方法的注释,以便处理在安装某个功能后引发的事件。
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
//{
//}
// 取消对以下方法的注释,以便处理在卸载某个功能前引发的事件。
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
//{
//}
// 取消对以下方法的注释,以便处理在升级某个功能时引发的事件。
//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
//{
//}
}
}