1

I am getting the below error while trying to set permission groups on folders in SharePoint 2010 list. The method is an extension method in helper dll and called from a feature receiver and set elevatedSite.AllowUnsafeUpdates = true; before the method call as well...

Please let me know if any one has solution for this?

Error:

Error occured while assigning permission to the Office Document Library: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

Code:

 public static void SetUniquePermissions(this SPListItem item, SPPrincipal principal, params string[] roleNames)
    {
        SPWeb web = item.Web;
        try
        {
            web.AllowUnsafeUpdates = true;
            item.BreakRoleInheritance(false);

            foreach (var roleName in roleNames)
            {
                Logger.LogInfo(string.Format("Granting {0} {1} permission on item {2} in list {3}", principal.LoginName, roleName, item.ID, item.ParentList.Title), "");
                SPRoleDefinition def = web.GetRoleDefinition(roleName);
                if (def != null)
                {
                    SPRoleAssignment ra = new SPRoleAssignment(principal);
                    ra.RoleDefinitionBindings.Add(def);
                    item.RoleAssignments.Add(ra);
                }
                else
                {
                    Logger.LogError("Unable to find permission " + roleName, "");
                }
            }
        }
        catch (Exception ex)
        {
            Logger.LogError(ex, "");
        }
        finally
        {
            web.AllowUnsafeUpdates = false;
        }

    }
6

BreakRoleInheritance() will reset AllowUnsafeUpdates automatically and you need to set it to True again.

This is probably the problem with your code. So try by setting AllowUnsafetUdates to True after you break the role inheritance and before updating the item.

4

Instead of using AllowUnsafeUpates I would refer to use ValidateDigestForm method in code. For more details please refer to

http://a2zdinesh.blogspot.in/2014/06/sharepoint-programming-dont-set-or-use.html

  • Please no link answers. Links die all the time. Please copy the content here & provide attribution. – Michael Blackburn Aug 31 '16 at 14:24

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.