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 have an event receiver on itemAdded. I set some permissions like below.

SPRoleAssignment role = new SPRoleAssignment(spPrincipal);
    role.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Contributor));
    listItem.RoleAssignments.Add(role);

Do I have to execute also the Update method to save the changes? Like this:

listItem.Update();

UPDATE:

This is the code:

public static void SetMemberShipToListItem(List<SPPrincipal> userAndGroups, SPListItem listItem, SPWeb web)
        {
            if (listItem.HasUniqueRoleAssignments)
            {
                listItem.ResetRoleInheritance();
            }

            listItem.BreakRoleInheritance(false);

            foreach (SPPrincipal spPrincipal in userAndGroups)
            {
                // set permission for current user (creator of this listitem)
                if (spPrincipal != null)
                {
                    SPRoleAssignment role = new SPRoleAssignment(spPrincipal);
                    role.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Contributor));
                    listItem.RoleAssignments.Add(role);
                }
            }
            //listItem.Update();
        }
share|improve this question
1  
Setting permissions at the item level will quickly become a management nightmare. I strongly urge you to re-evaluate the solution design. –  Paul Schaeflein Mar 6 '13 at 21:21
add comment

3 Answers

First thing first, Yes you will have to call listItem.Update() reason is because in itemAdded event Item has been added to list already and now you are about to update it.

Second, as Paul said you setting item level permissions can be nightmare as they actually effect page's performance while loading it.

Third, you might would like to look at this article, where you need to allowUnsafeUpdates to the web where list resides to update RoleAssignments.

share|improve this answer
    
what is happening when I do not the update? Because in visual studio debug mode everthing is working fine. The permissions are setted. –  KHA Mar 7 '13 at 7:23
    
@KHA Can you post rest of code, as you might be updating web after adding role assignments to list items. –  Muhammad Raja Mar 7 '13 at 16:47
    
see comments above for the code.. –  KHA Mar 11 '13 at 11:16
add comment

I agree with Andreas - you don't have to call Update() or SystemUpdate(). We have code in production that changes permissions and doesn't call Upate afterwards. But almost every example on the internet calls Update() after changing the role assignments.

Here's one blog post that doesn't call it: http://howtosharepoint.blogspot.co.uk/2010/10/programmatically-add-delete-modify-item.html

But even this old msdn blog calls Update() afterwards: http://blogs.msdn.com/b/ryanrogers/archive/2006/08/11/setting-list-item-permissions-programatically-in-wss-3-0-moss-2007.aspx

Update: the example in this other thread doesn't include it either: How to change programmatically privileges/permissions on SPListItem

share|improve this answer
add comment

I don't agree that you're required to update the SPSecurableObject (like SPListItem or SPList) if you change the RoleAssignments. I couldn't find the code using Reflector to prove my statement, but as KHA stated it works without any problems.

share|improve this answer
add comment

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.