namespace Microsoft.SharePoint
{
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint; public static class PermissionManager
{ //SPGroup group = web.Groups[0];
//SPUser user = web.Users[0];
//SPUser user2 = web.EnsureUser("mangaldas.mano");
//SPUser user3 = web.EnsureUser("Domain Users");
//SPPrincipal[] principals = { group, user, user2, user3 }; #region Adding Permissions to an item /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="user">SPUser</param>
/// <param name="roleType">SPRoleType</param>
public static void SetPermissions(SPListItem item, SPUser user, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
SetPermissions(item, (SPPrincipal)user, roleDefinition);
}
}
public static void SetPermissions(SPListItem item, SPUser user, string rolename)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[rolename];
if (roleDefinition != null)
SetPermissions(item, (SPPrincipal)user, roleDefinition);
}
}
public static void SetPermissions(SPListItem item, string groupName, string rolename)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[rolename];
SPGroup group = item.Web.Groups[groupName];
if (roleDefinition != null && group != null)
SetPermissions(item, (SPPrincipal)group, roleDefinition);
}
}
public static void SetPermissions(SPListItem item, string groupName, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
SPGroup group = item.Web.Groups[groupName];
if (group != null)
SetPermissions(item, (SPPrincipal)group, roleDefinition);
}
} /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principals">IEnumerable</param>
/// <param name="roleType">SPRoleType</param>
public static void SetPermissions(SPListItem item, IEnumerable<SPPrincipal> principals, SPRoleType roleType)
{
if (item != null)
{
foreach (SPPrincipal principal in principals)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
SetPermissions(item, principal, roleDefinition);
}
}
} /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleType">SPRoleType</param>
public static void SetPermissions(SPListItem item, SPPrincipal principal, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
SetPermissions(item, principal, roleDefinition);
}
} /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="user">SPUser</param>
/// <param name="roleDefinition">SPRoleDefinition</param>
public static void SetPermissions(SPListItem item, SPUser user, SPRoleDefinition roleDefinition)
{
if (item != null)
{
SetPermissions(item, (SPPrincipal)user, roleDefinition);
}
} /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleDefinition">SPRoleDefinition</param>
public static void SetPermissions(SPListItem item, SPPrincipal principal, SPRoleDefinition roleDefinition)
{
if (item != null)
{
BreakRoleSPListItem(item);
SPRoleAssignment roleAssignment = new SPRoleAssignment(principal);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
item.RoleAssignments.Add(roleAssignment);
}
} #endregion #region Deleting all user Permissions from an item /// <summary>
/// remove permisions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="user">SPUser</param>
public static void RemovePermissions(SPListItem item, SPUser user)
{
if (item != null)
{
RemovePermissions(item, user as SPPrincipal);
}
} public static void RemovePermissions(SPListItem item, string groupName)
{
if (item != null)
{
SPGroup group = item.Web.Groups[groupName];
if (group != null)
RemovePermissions(item, group as SPPrincipal);
}
} /// <summary>
/// remove permisions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
public static void RemovePermissions(SPListItem item, SPPrincipal principal)
{
if (item != null)
{
BreakRoleSPListItem(item);
item.RoleAssignments.Remove(principal);
// item.SystemUpdate();
}
} #endregion #region Removing specific roles from an item /// <summary>
/// remove permission
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleDefinition">SPRoleDefinition</param>
public static void RemovePermissionsSpecificRole(SPListItem item, SPPrincipal principal, SPRoleDefinition roleDefinition)
{
if (item != null)
{
SPRoleAssignment roleAssignment = item.RoleAssignments.GetAssignmentByPrincipal(principal);
if (roleAssignment != null)
{
if (roleAssignment.RoleDefinitionBindings.Contains(roleDefinition))
{
roleAssignment.RoleDefinitionBindings.Remove(roleDefinition);
roleAssignment.Update();
}
}
}
} /// <summary>
/// remove permission
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleType">SPRoleType</param>
public static void RemovePermissionsSpecificRole(SPListItem item, SPPrincipal principal, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
RemovePermissionsSpecificRole(item, principal, roleDefinition);
}
}
public static void RemovePermissionsSpecificRole(SPListItem item, SPUser user, string roleName)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[roleName];
if (roleDefinition != null)
RemovePermissionsSpecificRole(item, user as SPPrincipal, roleDefinition);
}
}
public static void RemovePermissionsSpecificRole(SPListItem item, string groupName, string roleName)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[roleName];
SPGroup group = item.Web.Groups[groupName];
if (group != null && roleDefinition != null)
RemovePermissionsSpecificRole(item, group as SPPrincipal, roleDefinition);
}
}
#endregion #region Updating or Modifying Permissions on an item /// <summary>
/// modify Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleType">SPRoleType</param>
public static void ChangePermissions(SPListItem item, SPPrincipal principal, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
ChangePermissions(item, principal, roleDefinition);
}
}
public static void ChangePermissions(SPListItem item, SPUser user, string roleName)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[roleName];
if (roleDefinition != null)
ChangePermissions(item, user as SPPrincipal, roleDefinition);
}
} public static void ChangePermissions(SPListItem item, string groupName, string roleName)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[roleName];
SPGroup group = item.Web.Groups[groupName];
if (roleDefinition != null)
ChangePermissions(item, group as SPPrincipal, roleDefinition);
}
}
/// <summary>
/// modify Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleDefinition">SPRoleDefinition</param>
public static void ChangePermissions(SPListItem item, SPPrincipal principal, SPRoleDefinition roleDefinition)
{
//SPRoleAssignment roleAssignment = item.RoleAssignments.GetAssignmentByPrincipal(principal); //if (roleAssignment != null)
//{
// roleAssignment.RoleDefinitionBindings.RemoveAll();
// roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
// roleAssignment.Update();
//}
RemovePermissions(item, principal);
SetPermissions(item, principal, roleDefinition);
} #endregion public static void BreakRoleSPListItem(SPListItem item)
{
if (item.HasUniqueRoleAssignments)
{
item.ResetRoleInheritance();
}
item.BreakRoleInheritance(false);
} public static SPUser GetSPUserFromSPListItemByFieldName(SPListItem spItem, string fieldName)
{
SPFieldUser field = spItem.Fields[fieldName] as SPFieldUser;
if (field != null && spItem[fieldName] != null)
{
SPFieldUserValue fieldValue = field.GetFieldValue(spItem[fieldName].ToString()) as SPFieldUserValue;
if (fieldValue != null)
{
return fieldValue.User;
}
}
return null;
}
class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
{
public DisabledItemEventsScope()
{
this.EventFiringEnabled = false;
}
#region IDisposable Members
public void Dispose()
{
this.EventFiringEnabled = true;
}
#endregion
}
public static void SaveListItem(SPListItem item)
{
using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
{
item.SystemUpdate();
}
}
}
}

http://blog.csdn.net/minglelui/article/details/6731824

SharePoint SPListItem 权限设置的更多相关文章

  1. sharepoint 调查问卷权限设置

    参考网址:http://www.cnblogs.com/mybi/archive/2011/04/18/2019935.html 按文章设置后发现访问时提示没有权限. 于是把新权限(问卷回复)的权限组 ...

  2. SharePoint 列表权限控制

    一提到sharepoint 我相信权限控制一定会是一个很重要的话题,尤其是对列表的权限控制,创建.修改.查看权限.其实网上这方面的资料已经很多了,尤其是以下2篇文章,后来索性把CodeArt_Perm ...

  3. Atitit  godaddy 文件权限 root权限设置

    Atitit  godaddy 文件权限 root权限设置 1. ubuntu需要先登录,再su切换到root1 2. sudo 授权许可使用的su,也是受限制的su1 3. ubuntu默认吗roo ...

  4. Linux 权限设置chmod

    Linux中设置权限,一般用chmod命令 1.介绍 权限设置chmod 功能:改变权限命令.常用参数: 1=x(执行权execute) 2=w(写权write) 4=r(读权Read) setuid ...

  5. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(23)-设置角色遗留问题和为权限设置角色以及EasyUI Tabs的使用

    ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    (2):数据库访问层的设计Demo    (3):面向接口编程   (4 ):业务逻辑层的封装    ...

  6. MAC OS X的ACL扩展权限设置

    在WEB开发时,网站是以_www的用户运行的,而我在本地是以liuwencan的用户编辑的.这就带来一个问题:如果所有文件属于liuwencan,那么网站运行需要写文件时就因无权限而失败:如果所有文件 ...

  7. C#-WinForm-MDI窗体容器、权限设置

    MDI窗体容器 - 放窗体的容器 窗体时顶级控件,是不允许放到其他的控件或窗体中的 (李献策lxc) 窗体属性中有一个属性:IsMdiContainer - 确定该窗体是否是MDI容器 在窗体中放一个 ...

  8. ACL权限设置命令setfacl和getfacl命令

    ACL权限设置命令setfacl和getfacl命令 setfacl命令是用来在命令行里设置ACL(访问控制列表).在命令行里,一系列的命令跟随以一系列的文件名. [TOC] 选项 |参数|说明|   ...

  9. MDI窗体容器、权限设置

    一.MDI窗体容器: 1.功能: 它可以让其它窗体在它的内部打开,无法超出它的范围 将某个窗体的属性:IsMdiContainer设置为true - 窗口样式 2.问题: (1)如何将其它窗体在它的内 ...

随机推荐

  1. InnoDB的锁机制浅析(All in One)

    目录 InnoDB的锁机制浅析 1. 前言 2. 锁基本概念 2.1 共享锁和排它锁 2.2 意向锁-Intention Locks 2.3 锁的兼容性 3. InnoDB中的锁 3.1 准备工作 3 ...

  2. Django(request和response)

    原文链接: https://blog.csdn.net/weixin_31449201/article/details/81043326 Django中的请求与响应 一.请求request djang ...

  3. hdu 5821 Ball 贪心

    Ball 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...

  4. C# 高级编程9 介绍篇

    对等网络 在日常软件环境中,解决了以下问题: 不断增加的客户端通讯负载放在服务器上,服务器必须与每个客户端进行通讯,导致站点崩溃.大流量消耗.服务器无法响应等问题. 因此产生了P2B网络技术. 使用P ...

  5. Mac应用

    App Store 安装: AnappyApp:   截图软件 Snap:  Dock快捷键启动 izip Unarchiver: rar解压 Dr.Cleaner:内存清理.资源监控 下载安装: C ...

  6. ios 从工程中删除Cocoapods

    删除工程文件夹下的Podfile.Podfile.lock及Pods文件夹 2. 删除xcworkspace文件 3. 使用xcodeproj文件打开工程,删除Frameworks组下的Pods.xc ...

  7. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  8. STM32F4 Timer Internal Trigger Connection

    The Timers can be cascaded to make more complex timing relationships, or longer periods. Internally ...

  9. Go 语言中的 new() 和 make()的区别

    本文是看了文章之后的心得. 在此感谢. 概述 Go 语言中的 new 和 make 一直是新手比较容易混淆的东西,咋一看很相似.不过解释两者之间的不同也非常容易. 他们所做的事情,和应用的类型也不相同 ...

  10. 详细分析Memcached缓存与Mongodb数据库的优点与作用

    http://www.mini188.com/showtopic-1604.aspx 本文详细讲下Memcached和Mongodb一些看法,以及结合应用有什么好处,希望看到大家的意见和补充. Mem ...