Creating SharePoint 2010 permissions levels

fix

Tuesday, March 2nd, 2010

If you’re building custom solutions for SharePoint you probably have various types of users that are accessing the prtal with all sorts of permissions on different containers such as document libraries and lists etc. Quite often this results in you needing to create custom level of access to the group and define what funcitons users are allowed to access such as item creation, editing etc.

Here is how to assing all of those settings programatically so that you can call them from the feature receiver when creating a site from your site template:

1. First we create custom permission level with the name and description and set of permissions you can find here.

SPRoleDefinition role = new SPRoleDefinition();
                role.BasePermissions = SPBasePermissions.OpenItems | SPBasePermissions.EditListItems | SPBasePermissions.ViewListItems | SPBasePermissions.ViewPages | SPBasePermissions.Open | SPBasePermissions.ViewFormPages;
                role.Name = “My Role Name”;
                role.Description = “My Role Description”;
                rootWeb.RoleDefinitions.Add(role);

Then we assign a set of permissions to an existing group in this case called MyGroup

                SPRoleAssignment roleAssignment = new SPRoleAssignment(rootWeb.SiteGroups["MyGroup"]);
                roleAssignment.RoleDefinitionBindings.Add(role);
                rootWeb.RoleAssignments.Add(roleAssignment);

If you’re reading this you probably spent about 10 minutes trying to find this particular code. Here is something I found very usefull if you’re developing for MOSS or SharePoint 2010: a great set of SharePoint snippents from CodePlex.

This isn’t just a one line of code type of snippets (well some are), but mostly those are really handy pieces of functionality and it’s really good to know it’s out there when you need it.

Cheers!

news

Any information posted on this blog does not reflect views of respective product vendors unless explicitely stated.

featured