C# 以编程方式将用户权限添加到 Sharepoint 中的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/358647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:24:41  来源:igfitidea点击:

Programmatically add user permission to a list in Sharepoint

c#sharepointpermissionsmoss

提问by Adyt

How do I programmatically add user permissions to a list in Sharepoint? I want to add the permission "Contribute" to a user or group for a certain list. I'm using C#.

如何以编程方式将用户权限添加到 Sharepoint 中的列表?我想为某个列表的用户或组添加“贡献”权限。我正在使用 C#。

采纳答案by Paul Nearney

You can do this using the SPRoleAssignmentobject, e.g.

您可以使用SPRoleAssignment对象执行此操作,例如

// Assuming you already have SPWeb and SPList objects
...
SPRoleAssignment roleAssignment = new SPRoleAssignment("dom\user", "user@dom", "user", "some notes");
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
if (!myList.HasUniqueRoleAssignments)
{
    myList.BreakRoleInheritance(true); // Ensure we don't inherit permissions from parent
} 
myList.RoleAssignments.Add(roleAssignment);
myList.Update();