博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编程给用户设置权限
阅读量:6481 次
发布时间:2019-06-23

本文共 1756 字,大约阅读时间需要 5 分钟。

This is some example code I wrote the for the MSDN forums to showcase how you can programmatically set permissions. This code will work for splists or spwebs (in the example it’s SPList permissions, but just changing those calls to go off of Web will switch it). In addition, this shows how to set permissions automatically at feature activation using a SPFeatureReceiver. Double bonus! ;)

#region feature event handler
// actions to perform when feature is activated/removed
class FeatureReceiver
: SPFeatureReceiver
{
#region Activate and install feature
public
override
void FeatureActivated
(SPFeatureReceiverProperties properties
)
{
SPWeb web
=
(SPWeb
)properties
.
Feature
.
Parent
;
SPList list
= web
.
Lists
[
"ListName"
]
;

 

//get users and groups so we can add them to the list

SPUserCollection users = web.Users;
SPGroupCollection groups = web.Groups;

//get current permissions and permission definitions so we can apply permissions

SPRoleAssignmentCollection rolesCol = list.RoleAssignments;
SPRoleDefinitionCollection roleDefs = web.RoleDefinitions;

//get a specific user or ad security group by username

SPUser myUser = web.EnsureUser(@”DOMAIN\MySecurityGroup”);
SPRoleAssignment addUser = SPRoleAssignment(myUser);

//create new roles binding for user or group

SPRoleDefinitionBindingCollection bindUser = addUser.RoleDefinitionBindings;
//give contribute rights to user or group, this can be the friendly name of any role definition
bindUser.Add(roleDefs["Contribute"]);
//add the user to the existing role assignment collection for the object :)
rolesCol.Add(addUser);
//and finally we update to apply our changes
list.Update();
}
}

  • Related posts:

转载于:https://www.cnblogs.com/ahghy/archive/2012/12/06/2804955.html

你可能感兴趣的文章
ORACLE 的游标
查看>>
虚拟机安装的UBUNTU全屏的方法:
查看>>
java虚拟机类加载器
查看>>
ASP.NET状态管理之八(会话Session)
查看>>
background
查看>>
转载:大型网站架构演变和知识体系
查看>>
set集合
查看>>
SVN服务器的搭建和使用
查看>>
mvc中枚举的使用和绑定枚举值到DropDownListFor
查看>>
多目标跟踪的评价指标
查看>>
python 生成器
查看>>
HTTPS(SSL)详解以及PHP调用方法
查看>>
突发小事件,USB接口问题
查看>>
Nginx负载均衡配置实例详解
查看>>
L1-009. N个数求和
查看>>
实参传递不当导致的运行时错误
查看>>
sqlserver 批量删除存储过程(转)
查看>>
自建型呼叫中心
查看>>
Inno setup中定制安装路径
查看>>
要懂得对你的老板好一点!
查看>>