问题描述
我试图找到一个简单的示例,该示例如何为列表项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",
"[email protected]",
"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
,而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权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!