locked
Setting permissions using SPRoleType RRS feed

  • Question

  • When trying to set the permissions on a page for a group, I'm having trouble with the SPRoleType enumeration. It will break on certain types (None and Reader) saying these do not exist in the enumeration. They clearly do: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sproletype.aspx. Contributor and Guest work fine. Here's a sample of the code:

     

    private void SetUserGroupPermissions(SPSite spSite, SPWeb initialWeb)  
    {  
     using (SPWeb spWeb = initialWeb.OpenWeb())  
            {  
              string strGroupName = WebConfigurationManager.OpenWebConfiguration("/" + spSite.WebApplication.Name).AppSettings.Settings["UTDUSERGROUP_SAMPLE"].Value;  
           
     
                //Define the SP groups  
                SPGroup spGroup = spWeb.SiteGroups[deGroupName];  
     
                //Assign permissions for group  
                SPRoleAssignment sampleGroupRoleAssignement = new SPRoleAssignment(spGroup);  
                sampleGroupRoleAssignement.RoleDefinitionBindings.Add(spWeb.RoleDefinitions.GetByType(SPRoleType.None));  
    }  
     
    It fails to find SPRoleType of "None". Any ideas?

    Monday, November 24, 2008 4:51 PM

All replies

  • I am able to get SPRoleType.Reader to work. However, SPRoleType.None throws "value does not fall within expected range" error. This what I would expect. Why would I have the SPRoleType.None within my web's SPRoleDefinitionCollection?
    http://www.certdev.com
    • Proposed as answer by Elad.Gutman Monday, July 6, 2009 9:30 AM
    Monday, November 24, 2008 10:31 PM
  • Hi

    I had the same problem. Sharepoint can't get role definitions by using role types Guest and None as SPWeb.RoleDefinition.GetByType(SPRoleDefinition.Type), so instead of using:

    definition = web.RoleDefinitions.GetByType(roleDefinition.Type);
    roleAssignment.RoleDefinitionBindings.Add(definition);

    i used the SPRoleDefinition.Id like so:

    if (roleDefinition.Type != SPRoleType.None)
    {
         definition = web.RoleDefinitions.GetByType(roleDefinition.Type);
         roleAssignment.RoleDefinitionBindings.Add(definition);
    }
    else
    {
         definition = web.RoleDefinitions.GetById(roleDefinition.Id);
         roleAssignment.RoleDefinitionBindings.Add(definition);
    }


    For more sharepoints tips visit http://www.mosswat.com

    Monday, July 6, 2009 9:47 AM