3

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();
        }
1
  • 1
    Setting permissions at the item level will quickly become a management nightmare. I strongly urge you to re-evaluate the solution design. Commented Mar 6, 2013 at 21:21

3 Answers 3

3

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.

3
  • what is happening when I do not the update? Because in visual studio debug mode everthing is working fine. The permissions are setted.
    – Ola
    Commented Mar 7, 2013 at 7:23
  • @KHA Can you post rest of code, as you might be updating web after adding role assignments to list items. Commented Mar 7, 2013 at 16:47
  • see comments above for the code..
    – Ola
    Commented Mar 11, 2013 at 11:16
2

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

0

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.

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.