SharePoint的EventHandler主要有Web Level,List Level,List Item Level,Email几种。SharePoint的event handler主要是继承SPWebEventReceiver, SPEmailEventReceiver, SPListEventReceiver和SPItemEventReceiver类去实现其中相应的方法来完成我们的需求。
ModifiedPermissions:
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using System.IO; namespace Anson.EventReceiver { public class ModifiedPermission:SPItemEventReceiver { string strGroupName = "Home Owners"; public override void ItemAdded(SPItemEventProperties properties) { SPWeb myWeb = null; SPSite mySite = null; try { SPSecurity.RunWithElevatedPrivileges(delegate() { mySite = new SPSite(properties.ListItem.Web.Site.ID); myWeb = mySite.AllWebs[properties.ListItem.Web.ID]; string currentUser = properties.ListItem.Web.CurrentUser.LoginName; SPListItem myListItem = myWeb.Lists[properties.ListId].GetItemById(properties.ListItemId); myWeb.AllowUnsafeUpdates = true; myListItem.BreakRoleInheritance(true); //Clear new listItem all the RoleAssignment while (myListItem.RoleAssignments.Count>0) { myListItem.RoleAssignments.Remove(0); } //Add Home Owner Group RoleAssignment SPGroup oOwnerGroup=myWeb.SiteGroups[strGroupName]; if (oOwnerGroup!=null) { SPRoleAssignment oRoleGroup = new SPRoleAssignment(oOwnerGroup); oRoleGroup.RoleDefinitionBindings.Add(myWeb.RoleDefinitions["Full Control"]); myListItem.RoleAssignments.Add(oRoleGroup); } //Add Author RoleAssignment SPRoleAssignment authorRoleAssignment = new SPRoleAssignment(myWeb.SiteUsers[currentUser]); authorRoleAssignment.RoleDefinitionBindings.Add(myWeb.RoleDefinitions["Full Control"]); myListItem.RoleAssignments.Add(authorRoleAssignment); myWeb.Update(); myWeb.AllowUnsafeUpdates = false; }); } catch (Exception ex) { log(ex.Message); } } public override void ItemDeleting(SPItemEventProperties properties) { try { string oCurrentUser = properties.ListItem.Web.CurrentUser.Name; string authorName = properties.ListItem["Created By"].ToString(); authorName = authorName.Substring(authorName.IndexOf(";#") + 2); if (oCurrentUser != authorName) { properties.Cancel = true; properties.ErrorMessage = "You have not enough permission to delete!"; } } catch (Exception ex) { log("ItemDeleting Method Error Message:"+ex.Message+DateTime.Now.ToString()); } } private void log(string ErrorMessage) { FileStream FS = null; StreamWriter SW = null; try { SPSecurity.RunWithElevatedPrivileges ( delegate { FS = File.Open("c:\\Error.txt", FileMode.Append); SW = new StreamWriter(FS); SW.Write(ErrorMessage+"\r\n"); SW.Close(); SW.Dispose(); FS.Close(); FS.Dispose(); } ); } catch { } finally { SW = null; FS = null; } } } }
EventReceiveManger.cs ---这个是添加移动EventReceiver的管理类
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
namespace Anson.EventReceiver
{
class EventReceiverManager
{
public static bool SetEventReceiver(SPList list,Type t,params SPEventReceiverType[] eventType)
{
try
{
string assembly = t.Assembly.FullName;
string className = t.FullName;
SPEventReceiverDefinitionCollection ERCollection = list.EventReceivers;
SPEventReceiverDefinition def = null;
for (int i = 0; i < ERCollection.Count; i++)
{
def = ERCollection[i];
if (def.Class==className)
{
def.Delete();
}
}
foreach (SPEventReceiverType ET in eventType)
{
list.EventReceivers.Add(ET, assembly, className);
}
list.Update();
return true;
}
catch (Exception ex)
{
log("SetEventReceiverError:" + ex.Message + "\r\n");
return false;
}
}
public static bool RemoveEventReceivers(SPList list ,Type t)
{
try
{
string assembly = t.Assembly.FullName;
string className = t.FullName;
SPEventReceiverDefinitionCollection ERCollection = list.EventReceivers;
SPEventReceiverDefinition def = null;
for (int i = 0; i < ERCollection.Count; i++)
{
def = ERCollection[i];
if (def.Class==className)
{
def.Delete();
}
}
list.Update();
return true;
}
catch (Exception ex)
{
log("RemoveEventReceivrs Method Error:"+ex.Message+"\r\n");
return false;
}
}
#region Log Method
public static void log(string ErrorMessage)
{
FileStream FS = null;
StreamWriter SW = null;
try
{
SPSecurity.RunWithElevatedPrivileges
(
delegate
{
FS = File.Open("c:\\Error.txt", FileMode.Append);
SW = new StreamWriter(FS);
SW.Write(ErrorMessage + "\r\n");
SW.Close();
SW.Dispose();
FS.Close();
FS.Dispose();
}
);
}
catch
{
}
finally
{
SW = null;
FS = null;
}
}
#endregion
}
}
EventReceiverInstall.cs --这个是安装Feature的类
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; namespace Anson.EventReceiver { class EventReceiverInstall:SPFeatureReceiver { string CustomListName = "AnsonList"; public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite mySite = properties.Feature.Parent as SPSite; SPList myList = mySite.RootWeb.Lists[CustomListName]; SPEventReceiverType[] types={SPEventReceiverType.ItemDeleting,SPEventReceiverType.ItemAdded}; if (myList!=null) { EventReceiverManager.SetEventReceiver(myList, typeof(ModifiedPermission), types); } else { return; } } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPSite mySite = properties.Feature.Parent as SPSite; SPList myList = mySite.RootWeb.Lists[CustomListName]; if (myList != null) { EventReceiverManager.RemoveEventReceivers(myList, typeof(ModifiedPermission)); } else { return; } } public override void FeatureInstalled(SPFeatureReceiverProperties properties) { } public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { } } }
Feature.xml -- 这个是Feature类似的结构,也有其他的结构写法,我习惯用这种
<?xml version="1.0" encoding="utf-8"?> <Feature Scope="Site" Title="Anson.EventReceiver ModifedPermission" Id="615208F2-85DD-42c5-9590-1F9B0BC725B0" Description="It is Created by Ansom ,Use in the list of AnsonList" xmlns="http://schemas.microsoft.com/sharepoint/" Hidden="FALSE" Version="1.0.0.0" ReceiverAssembly="Anson.EventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5abfd538ff01cc7c" ReceiverClass="Anson.EventReceiver.EventReceiverInstall"> </Feature>
部署:
1. 强命名。
2. DLL文件放到GAC
3.重启IIS, command: iisreset
3. 把含 Feature.xml文件的文件夹DistributePermission放到 12\TEMPLATE\FEATURES文件件里
4.安装Feature, Stsadm Command: stsadm -o installfeature -name DistributePermission
5.激活Feature