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();
        }
Ola
  • 4,405
  • 26
  • 129
  • 231
  • 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 06 '13 at 21:21

3 Answers3

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.

Muhammad Raja
  • 9,250
  • 7
  • 44
  • 85
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

Dan Fox
  • 76
  • 2
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.

Andreas
  • 619
  • 3
  • 17