如何为 SPItem 授予 SPGroup 权限? [英] How to give an SPGroup permissions for an SPItem?

查看:22
本文介绍了如何为 SPItem 授予 SPGroup 权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个简单的示例,如何为列表项 Z 指定某个 Sharepoint 组 X、权限级别 Y - 但找不到示例代码.

I tried to find a simple example how to give a certain Sharepoint group X, a permission level Y, for list item Z - but can't find example code.

我可以想出的为项目分配特定用户权限(在本例中为读者")的最短代码如下:

The shortest code I could come up with for assigning a specific user permissions ("Reader" in this case) for the item is the following:

SPRoleDefinition spRole = spWeb.RoleDefinitions["Reader"];    
SPRoleAssignment roleAssignment= new SPRoleAssignment("//myDomain/myUser",
                                                      "none@example.org",
                                                      "Name", "Notes");    
roleAssignment.RoleDefinitionBindings.Add(spRole);

SPListItem listItem = spWeb.GetListItem("http://<URL to item somewhere on the Site>");
listItem.BreakRoleInheritance(true);
listItem.RoleAssegnments.Add(roleAssignment);
listItem.Update();

我知道 SPRoleAssignment.Add 也可以采用 SPPrincipal 反过来 是一个组 - 我只是不知道怎么写.

I know that SPRoleAssignment.Add can also take an SPPrincipal which in turn is a group - I just don't know how do write it.

请给我一些示例代码,说明如何将具有读者"权限级别的现有 Sharepoint 组(例如MyGroup")添加到我的项目.

Please give me some example code for how to add an existing Sharepoint group (e.g. "MyGroup") with the "Reader" permission level to my item.

推荐答案

实际上这很容易 - 而不是 SPRoleAssignment("user","name"...) 我可以添加一个 SPGroup 到角色分配,它起作用了!完整代码如下:

Actually it was rather easy - instead of the SPRoleAssignment("user","name"...) I could just add an SPGroup to the role assignment and it worked! Full code following:

//note: using SiteGroups is "safer",
//because also groups which don't yet have any permissions are included
SPGroup spGroup = spWeb.SiteGroups["MyGroup"];
SPRoleDefinition spRole = spWeb.RoleDefinitions["Read"]; 

SPRoleAssignment roleAssignment= new SPRoleAssignment(spGroup);
roleAssignment.RoleDefinitionBindings.Add(spRole);

SPListItem listItem = spWeb.GetListItem("http://<URL to item somewhere on the Site>");
listItem.BreakRoleInheritance(true);
listItem.RoleAssignments.Add(roleAssignment);
listItem.Update();

这篇关于如何为 SPItem 授予 SPGroup 权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆