Monday, February 2, 2009

Creating custom permission level in moss

Hi All,

Today we are going to create a custom Permission level for the site.

It is very simple process of understanding the SPRoleDefinition.

Assume that currentWeb is an instance of SPWeb and where in you want to create custom permission level, then write down this code.

currentWeb.AllowUnsafeUpdates = true;

SPRoleDefinition roleDefinition = new SPRoleDefinition();

roleDefinition.Name = "Custom Permission";

And then start giving all permisions that you want to give.

roleDefinition.BasePermissions =
SPBasePermissions.ManageLists
| SPBasePermissions.AddListItems
| SPBasePermissions.EditListItems
| SPBasePermissions.DeleteListItems
| SPBasePermissions.ViewListItems
| SPBasePermissions.ApproveItems
| SPBasePermissions.OpenItems
| SPBasePermissions.ViewVersions
| SPBasePermissions.DeleteVersions
| SPBasePermissions.CreateAlerts
| SPBasePermissions.ViewFormPages
| SPBasePermissions.ViewPages
| SPBasePermissions.ManagePermissions
| SPBasePermissions.EnumeratePermissions;

These permission are same as that we find in Site Settings->Permissions->Permission levels which is at _layouts/role.aspx location of the site.

You can get each and every permission enumeration here in SPBasePermissions. Every permission that you see on _layouts/role.aspx page is there in SPBasePermissions enumeration.


Once you do that all that remains is just add this SProleDenifition to the web.

currentWeb.RoleDefinitions.Add(roleDefinition);

currentWeb.AllowUnsafeUpdates = false;

That's it. you are done. When you open this permission set named "Custom Permission", you can see those options are by default selected for which you have given permission from code.

4 comments:

Kalaravi said...

Thanks alot this is a good post

raj said...

very neat and clear explination.

thanks a lot.

pandit said...

Helpful code..
how to select default permissions programatically. for eg:- if i set "delete items" permission the code should select "view items"

SharePoint Kings said...

pandit,

if you remove all permission to user and give only delete permission then by default it should take view item.

this is how default permission works so this what we guess.

try it out




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com