我有一个库(“文档”),我想在其中添加一个事件接收器,它将删除添加的项目的权限。我已经尝试了多种方法来做到这一点,但我没有那么实验,我不知道我错在哪里。使用以下代码,我设法 BreakInheritance,但没有设法删除所有assignmets 并添加一个新的。
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
if (properties.ListTitle.Equals("Documents"))
I really need help on this one.{
using (SPSite site = new SPSite(properties.WebUrl))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb())
{
SPUser user = web.CurrentUser;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
web.AllowUnsafeUpdates = true;
SPListItem li = properties.ListItem;
SPFile lf = web.GetFile(li.Url);
SPRoleType read = SPRoleType.Guest;
lf.Item.BreakRoleInheritance(true);
while (lf.Item.RoleAssignments.Count > 0)
{
lf.Item.RoleAssignments.Remove(0);
}
AssignPermissionsToItem(lf.Item, user, read);
web.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
}
});
}
}
}
}
public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
{
if (!item.HasUniqueRoleAssignments)
{
item.BreakRoleInheritance(true);
}
SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
item.RoleAssignments.Add(roleAssignment);
}