I have set my web permission to Inherit Permissions as well as Lists.and programmatically i have add SPUSER to web with assigned 'Restricted Read' access Role.

and also associate SPUSER to List1 list with Full control role.

my requirement is Users will be get access Role (Restricted Read) to Site and Role(Full control) to List1.

Here is my code:

 web.AllowUnsafeUpdates = true;
 SPUser user = web.SiteUsers["domain\administrator"];
 if (user != null)
           SPRoleDefinitionCollection WebRoles = web.RoleDefinitions;
           SPRoleAssignment roleassignweb = new SPRoleAssignment(user);
           roleassignweb.RoleDefinitionBindings.Add(WebRoles["Restricted Read"]);

           SPRoleAssignment roleAssignment = new SPRoleAssignment(user);
           roleAssignment.RoleDefinitionBindings.Add(WebRoles["Full Control"]);
            #region Assign Role To Requirement List.
                if (IsListExists(web, "List1"))
                      SPList oRequirement = web.Lists["List1"];
                      if (!oRequirement.HasUniqueRoleAssignments)
                           oRequirement.BreakRoleInheritance(true); // Ensure we don't inherit permissions from parent


My code run perfect. but user could not got Full Control access to List1. any idea?

  • can you debug and check if your code is going through this code line, "oRequirement.BreakRoleInheritance(true); " Dec 28 '12 at 11:53

As far as I know I never seen "Full control" premission level has been assigned to anyone other then System Account, however I think it will be better if you give user "Contribute" permissions as it will let them do most of things they need to do or you can create your own custom permission level.

We normally use SPRoleType to enumerate default permission levels and then add them to user of choice as shown below,

// Assuming you already have SPWeb and SPList objects
SPRoleAssignment roleAssignment = new SPRoleAssignment("dom\\user", "user@dom", "user", "some notes");
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
if (!myList.HasUniqueRoleAssignments)
    myList.BreakRoleInheritance(true); // Ensure we don't inherit permissions from parent

For More Information check this Link out

Hope it helps.

  • Hi,my List1 inherit permission with parent web. but when my code run. List1 break inherit permission.why this cause? bcz of BreakRoleInheritance(true) Dec 28 '12 at 18:15
  • yup, thats true, by default all objects inherit permissions but when you break inheritance, it doesn't inherit anymore from parent web Dec 28 '12 at 18:36
  • Its sound like.. Should i try use BreakRoleInheritance(false,false)? Dec 28 '12 at 18:46
  • I don't think if that will going to make any major difference, can you try putting your code in Elevated privilages might help, msdn.microsoft.com/en-us/library/bb466220(v=office.12).aspx Dec 28 '12 at 18:48
  • no luck.i have use my code in RunwithElevatedPrivilages :( Dec 28 '12 at 18:53

i dont know if timetothine has solved your issue or not as it should! but if it hasnt then you can do this! it will 100% work for you using impersonation! I used it for annonymous user who has no rights to anything appart for the public site. I need to write to a list that they dont have access to and so I came up with this:

SharePoint -access to path is denied

Just to clarifiy, runwithelevatedprivlages is not impersonation:

a good example is an anonymous user who you want to edit (add to) a sharepoint list that they dont have access to programmaticaly.

runwithelevatedprivlages will not work as anonymous users wouldnt have anything to elevate, this is where you need to impersonate an account that does have access. runwithelevatedprivlages works well with logged in users who dont have the right permissions.

as an example I will show you impersonation first:

to impersonate you need to get the system token first and make sharepoint think that your that person, anything within the spsite that your going to impersonate will be under that user and not the current user your using, the method below is getting the system account token with elevated privlages (for anonymous users):

/// <summary>
/// get the system token from the list of users to elevate spsite
/// </summary>
/// <param name="spSite"></param>
/// <returns></returns>
protected static SPUserToken GetSystemToken(SPSite spSite) 
    SPUserToken res = null;     
    bool oldCatchAccessDeniedException = spSite.CatchAccessDeniedException;
        spSite.CatchAccessDeniedException = false;
        res = spSite.SystemAccount.UserToken;
    catch (UnauthorizedAccessException)
            using (SPSite elevatedSPSite = new SPSite(spSite.ID))
                res = elevatedSPSite.SystemAccount.UserToken;
            // (***)         
        spSite.CatchAccessDeniedException = oldCatchAccessDeniedException;     
    return res; 

to use this method you would need to do the following:

        SPUserToken sysToken = null;
        using (SPSite spSite = new SPSite(SPContext.Current.Site.ID))
            //get the system token from the method GetSystemToken and passing the spSite that you want to run under
            sysToken = GetSystemToken(spSite);

            using (SPSite impersonatedSite = new SPSite(siteUrl, sysToken))
                //we are impersonating the [@"SHAREPOINT\SYSTEM"] account
                using (SPWeb web = impersonatedSite.OpenWeb())
                     //do your code here as the impersonated account like updating a list

this will work where RunWithElevatedPrivileges does not work :) as you can see there is suttle differences between the two, impersonation is not using the application pool account but rather any account that you want to impersonate. RunWithElevatedPrivileges only uses the application pool account and has some limitation to what access it has

msdn RunWithElevatedPrivileges explained


msdn Impersonation explained



for the op, I would suggest as others and use RunWithElevatedPrivileges as it should be more than enough :)

hope this helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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