1

I have created one group Name: TestGroup and one permission level Name: Custompermisionlevel

How can I assign the permission level(Custompermisionlevel) to my group programmatically?

I'm using this code:

public void SetPermissionsToGroup()
    {
        using (var site = new SPSite("http://sp2010:8888"))
        {
            using (var web = site.OpenWeb())
            {
                //Get group and group roles
                var group = web.SiteGroups["TestGroup"];
                var roles = new SPRoleAssignment(group);                   

                //Add another role
                roles.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Custompermisionlevel));
                roles.Update();
            }
        }
    }

I am not getting my Custompermisionlevel in SPRoleType. Does anyone know what I am doing wrong?

getting this error:Cannot update a permission level assignment that is not part of a permission level assignment collection.

2 Answers 2

5

Aishwarya,

You can get Role Definition object by:

SPRoleDefinition myRoleDefinition = oWeb.RoleDefinitions["CustomRoleDefinitionDisplayName"];

Or you can get custom Role Definition by using Linq:

SPRoleDefinition myRoleDefinition = oWeb.RoleDefinitions.Cast<SPRoleDefinition>().Where(x => x.Name == "CustomRoleDefinitionName").FirstOrDefault();

Plese make sure you add using System.Linq; at the top.. and replace CustomRoleDefinitionName with your role definition name

Once you get the Role Definition, you can use it as following:

var roles = new SPRoleAssignment(group);                   

//Add another role
roles.RoleDefinitionBindings.Add(myRoleDefinition);
roles.Update();
3

You can get your custom permission level as below:

roleDefinition = web.RoleDefinitions["YourCustomPermLevelName"];

Your error is about something else I think. On what securable object do you want to give your custom permission level to the group? I think you can't and don't have to call roles.Update() like that.

You can only call the Update method for an SPRoleAssignment object that is part of an SPRoleAssignmentCollection that in turn is bound to a Microsoft SharePoint Foundation object that implements the ISecurableObject interface (for example, SPWeb).

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sproleassignment.update.aspx

Let's say you want to give your custom permission level to the group for the site (can be a list, item,..). Then you can do the following:

public void SetPermissionsToGroup()
{
    using (var site = new SPSite("http://sp2010:8888"))
    {
        using (var web = site.OpenWeb())
        {
            var group = web.SiteGroups["TestGroup"];
            SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
            SPRoleDefinition roleDefinition = new SPRoleDefinition();
            roleDefinition = web.RoleDefinitions["YourCustomPermLevelName"];
            SPRoleDefinitionBindingCollection roleDefinitionBindingCollection = new SPRoleDefinitionBindingCollection();
            roleDefinitionBindingCollection.Add(roleDefinition);
            roleAssignment.ImportRoleDefinitionBindings(roleDefinitionBindingCollection);
            web.AllowUnsafeUpdates = true;
            web.RoleAssignments.Add(roleAssignment);
            web.AllowUnsafeUpdates = false;             
        }
    }
}

http://naimmurati.wordpress.com/2011/02/27/set-permissions-on-sharepoint-list-and-items-programmatically/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.