sharepoint 2013 文档库eventhandle权限控制

时间:2022-02-07 04:03:30

记录一下如何在sharepoint server 2013文档库中,使用eventhandle控制文档库document library的条目item权限。

///<summary>

///已添加项.

///</summary>

public override void ItemAdded(SPItemEventProperties properties)

{

base.ItemAdded(properties);

SPSecurity.RunWithElevatedPrivileges(delegate()

{

if(properties.ListTitle =="共享文档1")

{

SPSite site =newSPSite(properties.SiteId);

SPWeb web =site.OpenWeb();

web.AllowUnsafeUpdates = true;

SPList list =web.Lists[properties.ListId];

SPFile file =list.GetItemById(properties.ListItemId).File;

SPListItem item = file.Item;

//权限断开,并且移除掉其它权限

if(!item.HasUniqueRoleAssignments)

{

item.BreakRoleInheritance(false);

}

//添加用户权限

SPUser CurrentUser =web.EnsureUser(properties.UserLoginName);

SPRoleAssignment MyRoleAssignment =newSPRoleAssignment(CurrentUser);

MyRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.WebDesigner));

item.RoleAssignments.Add(MyRoleAssignment);

CurrentUser =web.EnsureUser("kw\\cxx");

MyRoleAssignment = newSPRoleAssignment(CurrentUser);

MyRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.WebDesigner));

item.RoleAssignments.Add(MyRoleAssignment);

item.Update();

}

});

}