本文整理汇总了C#中SPListItem.BreakRoleInheritance方法的典型用法代码示例。如果您正苦于以下问题:C# SPListItem.BreakRoleInheritance方法的具体用法?C# SPListItem.BreakRoleInheritance怎么用?C# SPListItem.BreakRoleInheritance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SPListItem
的用法示例。
在下文中一共展示了SPListItem.BreakRoleInheritance方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetTrainingCourseItemPermission
private void SetTrainingCourseItemPermission(SPListItem tcItem)
{
tcItem.BreakRoleInheritance(false);
foreach (PickerEntity entity in peAttendees.ResolvedEntities) {
SPPrincipal principal = MOSSPrincipal.FindUserOrSiteGroup(entity.Key);
SPRoleAssignment roleAssignment = MOSSPermission.GetRoleAssignment(principal, SPRoleType.Reader);
tcItem.RoleAssignments.Add(roleAssignment);
}
SPPrincipal hrPrincipal = MOSSPrincipal.FindUserOrSiteGroup(AnalogicConstant.HR);
SPRoleAssignment hrAssign = MOSSPermission.GetRoleAssignment(hrPrincipal, SPRoleType.Contributor);
tcItem.RoleAssignments.Add(hrAssign);
}
示例2: UpdateUserPermission
public static void UpdateUserPermission(SPWeb web, bool isBreakRole, SPListItem item, string LoginName, string RoleDefinition)
{
web.AllowUnsafeUpdates = true;
if (isBreakRole)
{
item.ResetRoleInheritance();
item.Update();
if (!item.HasUniqueRoleAssignments)
item.BreakRoleInheritance(false);
}
SPRoleDefinition SPRoleDefinitionData = item.Web.RoleDefinitions[RoleDefinition];
string[] split = LoginName.Split(';');
foreach (string s in split)
{
SPRoleAssignment RoleAssignment = new SPRoleAssignment(s, string.Empty, string.Empty, string.Empty);
RoleAssignment.RoleDefinitionBindings.Add(SPRoleDefinitionData);
item.RoleAssignments.Add(RoleAssignment);
}
item.Update();
}
示例3: UpdateGroupPermission
public static void UpdateGroupPermission(SPWeb web, bool isBreakRole, SPListItem item, string Group, string RoleDefinition)
{
web.AllowUnsafeUpdates = true;
if (isBreakRole)
{
item.ResetRoleInheritance();
item.Update();
if (!item.HasUniqueRoleAssignments)
item.BreakRoleInheritance(false);
}
SPRoleDefinition SPRoleDefinitionData = item.Web.RoleDefinitions[RoleDefinition];
string[] GroupSplit = Group.Split(';');
foreach (string i in GroupSplit)
{
SPGroup MembersGroup = item.Web.SiteGroups[i];
SPRoleAssignment MembersRoleAssignment = new SPRoleAssignment(MembersGroup);
MembersRoleAssignment.RoleDefinitionBindings.Add(SPRoleDefinitionData);
item.RoleAssignments.Add(MembersRoleAssignment);
}
item.Update();
}
示例4: AddRoleAssignmentToListItem
/// <summary>
/// 为某个SPPrincipal赋于针对某个SPListItem的权限. 权限由rd来决定.
/// </summary>
/// <param name="item">某个需要赋于权限的SPListItem</param>
/// <param name="spPrincipalName">用户登录名称或者是MOSS User Group, 如果是EMail地址, 则略过</param>
/// <param name="rd">需要赋于的权限级别名称</param>
public static void AddRoleAssignmentToListItem(SPListItem item, string spPrincipalName, SPRoleDefinition rd)
{
// 表明这个LoginName是电子邮件地址类型:SmtpAddress
if (spPrincipalName.IndexOf("@") >= 0)
return;
SPRoleAssignment ra = null;
// 如果spPrincipalName里面包含\, 则表示是一个AccountId, 否则是一个SPGroup
if (spPrincipalName.IndexOf("\\") >= 0)
ra = new SPRoleAssignment(spPrincipalName, string.Empty, string.Empty, string.Empty);
else
{
try
{
SPGroup group = item.Web.Groups[spPrincipalName];
if (group != null)
{
ra = new SPRoleAssignment(group);
}
}
catch { }
}
if (ra == null)
return;
SPRoleDefinition rd_temp = null;
// 特别处理的地方:rd在创建并添加到Web.RoleDefinitions之后, 还不能直接使用rd这个变量,可能是复制了一个副本,然后再添加到Collection中去的,所以这里需要从Collection中查找副本, 然后再对其进行引用
// 不可以直接写为: ra.RoleDefinitionBindings.Add(rd) ; 会报错称不能使用Web中未定义的RD
// 这个问题的本质原因在于创建类RoleDefinition之后, 前面拿到的SPWeb不会同步更新, 重新打开SPWeb就可以解决问题
for (int index = 0; index < item.Web.RoleDefinitions.Count; index++)
if (item.Web.RoleDefinitions[index].Name.ToLower() == rd.Name.ToLower())
rd_temp = item.Web.Site.RootWeb.RoleDefinitions[index];
ra.RoleDefinitionBindings.Add(rd_temp);
item.BreakRoleInheritance(false);
item.RoleAssignments.Add(ra);
}
示例5: BreakAndClearRoleInheritance
/// <summary>
/// 取消某个SPListItem从父节点继承的权限
/// </summary>
/// <param name="item"></param>
public static void BreakAndClearRoleInheritance(SPListItem item)
{
//item.ParentList.ParentWeb.AllowUnsafeUpdates = true;
SPWeb web = item.ParentList.ParentWeb;
web.AllowUnsafeUpdates = true ;
try
{
item.BreakRoleInheritance(true);
}
catch { }
web.AllowUnsafeUpdates = true;
int count_ra = item.RoleAssignments.Count;
// 如果某个RoleAssignment的Member为系统管理员, 则不要删除之
for (int index = count_ra - 1; index >= 0; index--)
if (item.RoleAssignments[index].Member.Name.ToLower() != "SHAREPOINT\\system".ToLower())
item.RoleAssignments.Remove(index);
}
示例6: CopyListItemRoleAssignments
public static void CopyListItemRoleAssignments(SPListItem sourceListItem, SPListItem destinationListItem)
{
//First check if the Source List has Unique permissions
if (sourceListItem.HasUniqueRoleAssignments) {
//Break List permission inheritance first
destinationListItem.BreakRoleInheritance(true);
destinationListItem.Update();
//Remove current role assignemnts
while (destinationListItem.RoleAssignments.Count > 0) {
destinationListItem.RoleAssignments.Remove(0);
}
destinationListItem.Update();
//Copy Role Assignments from source to destination list.
foreach (SPRoleAssignment sourceRoleAsg in sourceListItem.RoleAssignments) {
SPRoleAssignment destinationRoleAsg = null;
//Get the source member object
SPPrincipal member = sourceRoleAsg.Member;
//Check if the member is a user
try {
SPUser sourceUser = (SPUser)member;
SPUser destinationUser = destinationListItem.ParentList.ParentWeb.AllUsers[sourceUser.LoginName];
if (destinationUser != null) {
destinationRoleAsg = new SPRoleAssignment(destinationUser);
}
}
catch { }
//Not a user, try check if the member is a Group
if (destinationRoleAsg == null) {
//Check if the member is a group
try {
SPGroup sourceGroup = (SPGroup)member;
SPGroup destinationGroup = destinationListItem.ParentList.ParentWeb.SiteGroups[sourceGroup.Name];
if (destinationGroup != null) {
destinationRoleAsg = new SPRoleAssignment(destinationGroup);
}
}
catch { }
}
//At this state we should have the role assignment established either by user or group
if (destinationRoleAsg != null) {
foreach (SPRoleDefinition sourceRoleDefinition in sourceRoleAsg.RoleDefinitionBindings) {
try { destinationRoleAsg.RoleDefinitionBindings.Add(destinationListItem.ParentList.ParentWeb.RoleDefinitions[sourceRoleDefinition.Name]); }
catch { }
}
if (destinationRoleAsg.RoleDefinitionBindings.Count > 0) {
//handle additon of an existing permission assignment error
try { destinationListItem.RoleAssignments.Add(destinationRoleAsg); }
catch (ArgumentException) { }
}
}
}
//Ensure item update metadata is not affected.
destinationListItem.SystemUpdate(false);
}
else
//No need to assign permissions
return;
}
示例7: ShareItem
/// <summary>
/// This method marks the current Item as shared
/// </summary>
/// <param name="site">SPWeb</param>
/// <param name="item">SPListItem</param>
/// <param name="Shared">bool</param>
public static void ShareItem(SPWeb site, SPListItem item, bool Shared)
{
item.BreakRoleInheritance(true);
site.AllowUnsafeUpdates = true;
SPFieldUrlValue value = new SPFieldUrlValue();
//Remove all existing RoleAssignments
for (int i = item.RoleAssignments.Count - 1; i >= 0; i--)
{
item.RoleAssignments.Remove(item.RoleAssignments[i].Member);
}
if (Shared == true)
{
SPList siteList = site.Lists["Shared Documents"];
for (int i = siteList.RoleAssignments.Count - 1; i >= 0; i--)
{
item.RoleAssignments.Add(siteList.RoleAssignments[i]);
}
value.Description = "Shared";
value.Url = SPHelper.GetRootUrl(SPContext.Current.Web.Url) + "_layouts/IMAGES/sharedoc.gif";
}
else
{
value.Description = "Private";
value.Url = SPHelper.GetRootUrl(SPContext.Current.Web.Url) + "_layouts/IMAGES/bizdataactionicon.gif";
}
item["Access Level"] = value;
item.Update();
site.AllowUnsafeUpdates = false;
site.Update();
}
示例8: AssignPermission
public static void AssignPermission(SPListItem Item, string LoginName, string UserEmail, string UserNote, string UserName, SPWeb Web)
{
Web.AllowUnsafeUpdates = true;
//Remove all existing RoleAssignments
for (int i = Item.RoleAssignments.Count - 1; i >= 0; i--)
{
Item.RoleAssignments.Remove(Item.RoleAssignments[i].Member);
}
SPRoleAssignment roleAssignment = new SPRoleAssignment(LoginName, UserEmail, UserName, UserNote);
SPRoleDefinition roleDefinition = Web.RoleDefinitions.GetByType(SPRoleType.Administrator);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
if (!Item.HasUniqueRoleAssignments)
{
// Ensure we don't inherit permissions from parent
Item.BreakRoleInheritance(true);
}
Item.RoleAssignments.Add(roleAssignment);
Item.SystemUpdate(false);
Web.Update();
Web.AllowUnsafeUpdates = false;
}
示例9: AssignApproverReadPermission
/// <summary>
/// This method assigns user or group with read permission on the current item/file.
/// </summary>
/// <param name="Item">SPListItem</param>
/// <param name="Approver">string</param>
/// <param name="Web">SPWeb</param>
public static void AssignApproverReadPermission(SPListItem Item,SPUser Approver,SPWeb Web)
{
bool _roleAssignmentExist = false;
//Checking if role already exit for the current user or group.
foreach(SPRoleAssignment rs in Item.RoleAssignments)
{
if (rs.Member == Approver)
{
_roleAssignmentExist = true;
}
}
//Add user or group role assignment.
if (_roleAssignmentExist == false)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPRoleAssignment roleAssignment = new SPRoleAssignment(Approver.LoginName, Approver.Email, Approver.Name, "Approver's file access permission.");
SPRoleDefinition roleDefinition = Web.RoleDefinitions.GetByType(SPRoleType.Administrator);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
if (!Item.HasUniqueRoleAssignments)
{
// Ensure we don't inherit permissions from parent
Item.BreakRoleInheritance(true);
}
Item.Web.AllowUnsafeUpdates = true;
Item.RoleAssignments.Add(roleAssignment);
Item.SystemUpdate(false);
Item.Web.AllowUnsafeUpdates = false;
});
}
}
示例10: SetItemPermissions
/// <summary>
/// adds a specified role for a given user on a list item
/// </summary>
/// <param name="web">ref to SPWeb</param>
/// <param name="listItem">list item in question</param>
/// <param name="role">role to add for user e.g. Contribute, View or any custom permission level</param>
/// <param name="loginName">login name of user or sharepoint group</param>
/// <returns></returns>
internal static SPListItem SetItemPermissions(SPWeb web, SPListItem listItem, string role, string loginName)
{
if (!listItem.HasUniqueRoleAssignments)
{
listItem.BreakRoleInheritance(true);
}
SPRoleDefinition RoleDefinition = web.RoleDefinitions[role];
SPPrincipal myUser = FindUserOrSiteGroup(web.Site, loginName);
if (myUser == null)
{
throw new ArgumentException(string.Format("user:{0} is not a valid sharepoint user!"));
}
SPRoleAssignment RoleAssignment = new SPRoleAssignment(myUser);
RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
listItem.RoleAssignments.Add(RoleAssignment);
return listItem;
}
示例11: RemoveListItemPermissionEntry
/// <summary>
/// removes role assigments for principal, breaking inheritance if specified
/// </summary>
/// <param name="item"></param>
/// <param name="principal"></param>
/// <param name="breakRoleInheritance"></param>
internal static void RemoveListItemPermissionEntry(SPListItem item, string principalName, bool breakRoleInheritance)
{
List<int> usersToRemove = new List<int>();
if (!item.HasUniqueRoleAssignments && breakRoleInheritance)
item.BreakRoleInheritance(true);
else if (!item.HasUniqueRoleAssignments)
return;
SPPrincipal myPrincipal = FindUserOrSiteGroup(item.Web.Site, principalName);
foreach (SPRoleAssignment ra in item.RoleAssignments)
{
if (ra.Member.ID == myPrincipal.ID)
{
usersToRemove.Add(ra.Member.ID);
}
}
foreach (int id in usersToRemove)
{
item.RoleAssignments.RemoveById(id);
}
}
示例12: ModifyItemPermissions
private void ModifyItemPermissions(SPListItem listItem)
{
// Modifying the permissions for the list to restrict the students for direct list items operations
try
{
if (!listItem.HasUniqueRoleAssignments)
{
var web = listItem.ParentList.ParentWeb;
listItem.BreakRoleInheritance(false, false);
var group = web.SiteGroups[Utilities.FacultyGroupName];
var roleAssignment = new SPRoleAssignment(group);
var spRole = web.RoleDefinitions["Full Control"];
roleAssignment.RoleDefinitionBindings.Add(spRole);
listItem.RoleAssignments.Add(roleAssignment);
roleAssignment = new SPRoleAssignment(SPContext.Current.Web.CurrentUser);
spRole = web.RoleDefinitions["Read"];
roleAssignment.RoleDefinitionBindings.Add(spRole);
listItem.RoleAssignments.Add(roleAssignment);
listItem.Update();
}
}
catch (Exception ex)
{
SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("BethesdaSkillLab", TraceSeverity.Monitorable, EventSeverity.Error), TraceSeverity.Monitorable, ex.Message, new object[] { ex.StackTrace });
}
}