Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to work with privileges on elements of a list. I want the item to mantain the same privileges for all SPPrincipal I don't modify.

This is the code

    public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType) 
     { 
         if (!item.HasUniqueRoleAssignments) 
         { 
             item.BreakRoleInheritance(false, true); 
          } 

    SPRoleAssignment roleAssignment = new SPRoleAssignment(obj); 
    SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType); 
    roleAssignment.RoleDefinitionBindings.Add(roleDefinition); 

    item.RoleAssignments.Add(roleAssignment); 
} 

This code (proposed to me by Alexander), destroys all the permissions and add the permission specified as parameter..

To mantain the past privileges I must set

item.BreakRoleInheritance(true, true); 

which mantains the old privileges.

I tried to make a RemoveAll on the roleAssignment before the add of the new one, but it mantains also the old privileges, like RemoveAll didn't to anything!

This is the code:

 public static void AddPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
    {
        if (!item.HasUniqueRoleAssignments)
        {
            item.BreakRoleInheritance(true, true);
        }

        SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
        roleAssignment.RoleDefinitionBindings.RemoveAll();
        roleAssignment.Update();

        SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
        roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

        item.RoleAssignments.Add(roleAssignment);
    }

My code (simplyfied):

  [WebMethod]
    public ResponseObject InsertInAllReplyers(string username)
    {
        ResponseObject SPResultStatus = new ResponseObject();
        try
        {

                SPUserToken myToken = TokenManager.GetToken(username);
                //ATTENZIONE!! SOLO PER LA MACCHINA LOCALE DIEGO VA MANTENUTO LO SPAZIO!!!
                string fullsite = SPContext.Current.Web.Url + "/sites/Reply Corp";
                //ATTENZIONE!! SOLO PER LA MACCHINA LOCALE DIEGO VA MANTENUTO LO SPAZIO!!!
                using (SPSite site = new SPSite(fullsite, myToken))
                {
                    site.AllowUnsafeUpdates = true;
                    using (SPWeb currentWeb = site.OpenWeb("All Replyers"))
                    {

                        currentWeb.AllowUnsafeUpdates = true;
                        SPList targetList = currentWeb.Lists["Contenuti"];
                        SPListItem listItem = targetList.AddItem();
                        listItem["Title"] = "Prova :)";
                        listItem["tt_contentid"] = "TT_ID252";
                        listItem["Tag"] = "Tag2";
                        listItem.Update();

                        var allreplyers = currentWeb.Groups["All Replyers"];
                        AssignPermissionsToItem(listItem, (SPPrincipal)allreplyers, SPRoleType.Reader);
                        AssignPermissionsToItem(listItem, (SPPrincipal)currentWeb.AllUsers[username], SPRoleType.Contributor);

                        SPResultStatus.SPResultStatus = ResponseObjectSPResultStatus.SUCCESS;
                        SPResultStatus.SPResultUrl = listItem.Url;
                        SPResultStatus.SPResultId = listItem.ID.ToString();
                        SPResultStatus.SPResultType = ResponseObjectSPResultType.LIST;

                        }

                }
        }
        catch (Exception exx)
        {
            return new ResponseObject() { SPResultStatus = ResponseObjectSPResultStatus.FAILURE, Message = exx.Message, Stacktrace = exx.StackTrace, ExceptionType = exx.GetType().ToString() };
        }
        return SPResultStatus;
    }

    public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
    {
        if (!item.HasUniqueRoleAssignments)
        {
            item.BreakRoleInheritance(false, true);
        }
        item.RoleAssignments.Remove(obj); 
        item.SystemUpdate(); 

        SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
        SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);

        roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

        item.RoleAssignments.Add(roleAssignment);
    }
share|improve this question
1  
I've posted an answer but if it doesn't help could you post the code of your RemoveAll method? –  SpartanDonut Feb 2 '12 at 17:08
    
Just added the code... –  Ziba Leah Feb 2 '12 at 18:03

1 Answer 1

up vote 1 down vote accepted

Make sure you do an update to your list item before adding the role again.

oListItem.RoleAssignments.Remove(principal);
oListItem.SystemUpdate();
share|improve this answer
    
It tolds me i cannot do that operation with my privilege even if I'm in a Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges part... –  Ziba Leah Feb 2 '12 at 18:04
    
Odd - what if you use just an update intead of a system update? Can you post the code leading up to that point? Specifically how you get your item object? Also for the record, any time I've worked with permissions I've only ever done RoleAssignments.Remove() and not RoleDefinitionBindings.RemoveAll(). I'm not sure what route is necessarily better, that is just what has worked for me. –  SpartanDonut Feb 2 '12 at 19:46
1  
Looking at the example I have in front of me (code someone I know wrote) they also do an Update() right after BreakRoleInheritance() - Not sure if this is bad programming or a requirement. Worth trying though. –  SpartanDonut Feb 2 '12 at 19:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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