How to give an SPGroup permissions for an SPItem?

前端 未结 1 1883
盖世英雄少女心
盖世英雄少女心 2020-12-18 08:12

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

相关标签:
1条回答
  • 2020-12-18 08:41

    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();
    
    0 讨论(0)
提交回复
热议问题