Sharepoint 权限

Sharepoint权限中的关系:权限——角色 ——对象——关联

//为角色赋予权限
public void RoleAssignment(SPListItem item, SPPrincipal groupOrUser, SPRoleDefinition role)
        {
            item.Web.AllowUnsafeUpdates = true;
            if (!item.HasUniqueRoleAssignments)
            {
                item.BreakRoleInheritance(true);//true则断开并继承原有权限,false则断开不继承原有权限
            }
            SPRoleAssignment assignment = new SPRoleAssignment(groupOrUser);
            assignment.RoleDefinitionBindings.Add(role);
            item.RoleAssignments.Add(assignment);
            item.Update();
            item.Web.AllowUnsafeUpdates = false;
        }

//为权限级别分配权限基类:SPBasePermissions
        SPWeb oWebsite = SPContext.Current.Web
        SPRoleDefinitionCollection collRoles = oWebsite.RoleDefinitions;        //获取当前网站的权限集
                SPRoleDefinition oRoleDefinition = collRoles["名称"];                  //权限集中的权限级别“名称”
                                                                                        //权限级别为网站操作中预先设定
                oRoleDefinition.BasePermissions = SPBasePermissions.AddListItems |      //为权限级别分配权限积累
                    SPBasePermissions.BrowseDirectories |
                    SPBasePermissions.EditListItems |
                    SPBasePermissions.DeleteListItems |
                    SPBasePermissions.AddDelPrivateWebParts; ;
                oWebsite.AllowUnsafeUpdates = true;
                oRoleDefinition.Update();                                               //更新网站权限
                oWebsite.AllowUnsafeUpdates = false;

//完整分配权限方法
 public void ListItem_Permission(object sender, ClickedEventArgs e)
        {
            using (SPWeb oWebsite = SPContext.Current.Web)
            {
                SPRoleDefinitionCollection collRoles = oWebsite.RoleDefinitions;

                SPRoleDefinition oRoleDefinition = collRoles["test2"];
                oRoleDefinition.BasePermissions = SPBasePermissions.AddListItems |
                    SPBasePermissions.BrowseDirectories |
                    SPBasePermissions.EditListItems |
                    SPBasePermissions.DeleteListItems |
                    SPBasePermissions.AddDelPrivateWebParts; ;
                oWebsite.AllowUnsafeUpdates = true;
                oRoleDefinition.Update();
                oWebsite.AllowUnsafeUpdates = false;
                SPPrincipal User = oWebsite.SiteGroups["权限名称"];                      //权限名称
                RoleAssignment(oWebsite.Lists["列表名"].Items["Item名称"].Fields[""], User, oRoleDefinition);
            }
        }
posted @ 2012-09-14 13:18  任泽华Ryan  阅读(397)  评论(0编辑  收藏  举报