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 the following problem with this code:

    public override void ItemUpdating(SPItemEventProperties properties)
    {
        base.ItemUpdating(properties);

        const string fieldInternalNameWrt = "UserWrt";
        const string fieldInternalNameRdr = "UserRdr";

        SPListItem item = properties.ListItem;
        SPUser userWrt = null;
        SPUser userRdr = null;

        if (properties.AfterProperties[fieldInternalNameWrt] != null)
        {
            string userValueWrt = properties.AfterProperties[fieldInternalWrt].ToString();
            userWrt = GetSPUserFromId(properties.ListTitle, userValueWrt, fieldInternalWrt, properties);
        }

        if (properties.AfterProperties[fieldInternalNameRdr] != null)
        {
            string userValueRdr = properties.AfterProperties[fieldInternalNameRdr].ToString();
            userRdr = GetSPUserFromId(properties.ListTitle, userValueRdr, fieldInternalNameRdr, properties);
        }

        if (userWrt == null && userRdr == null)
            return;

        SPRoleAssignment roleAssignmentWrt = null;
        if (userWrt != null)
        {
            roleAssignmentWrt = new SPRoleAssignment(userWrt);
            var roleDefinition = properties.Web.RoleDefinitions.GetByType(SPRoleType.Contributor);
            roleAssignmentWrt.RoleDefinitionBindings.Add(roleDefinition);
        }

        SPRoleAssignment roleAssignmentRdr = null;
        if (userRdr != null)
        {
            roleAssignmentRdr = new SPRoleAssignment(userRdr);
            var roleDefinition = properties.Web.RoleDefinitions.GetByType(SPRoleType.Reader);
            roleAssignmentRdr.RoleDefinitionBindings.Add(roleDefinition);
        }

        try
        {
            this.EventFiringEnabled = false;

            lock (_thisLock)
            {
                SPSecurity.RunWithElevatedPrivileges(() =>
                {
                    item.BreakRoleInheritance(false);

                    foreach (SPRoleAssignment assignment in item.RoleAssignments)
                    {
                        assignment.RoleDefinitionBindings.RemoveAll();
                        assignment.Update();
                    }

                    if (roleAssignmentWrt != null)
                        item.RoleAssignments.Add(roleAssignmentWrt);

                    if (roleAssignmentRdr != null)
                        item.RoleAssignments.Add(roleAssignmentRdr);

                    item.Update();
                });
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            this.EventFiringEnabled = true;
        }
    }

    public static SPUser GetSPUserFromId(string listName, string currentValue, string nameField, SPItemEventProperties properties)
    {
        SPFieldUserValue fieldValue;

        using (SPWeb web = properties.OpenWeb())
        {
            var userField = (SPFieldUser)web.Lists[listName].Fields.GetField(nameField);
            fieldValue = (SPFieldUserValue)userField.GetFieldValue(currentValue);
        }

        return fieldValue.User;
    }

the item's permissions was changed but I receive the following error!

http://i.stack.imgur.com/l1lgn.jpg1

anyone knows why?

share|improve this question
1  
it is impossible to read the attached screenshot. Please fix by adding the text as formattet code. Also remember to have EventFiringEnabled in a try/finally or you wont get it enabled again if your code breaks. Also please try to describe what you are trying to do, what works and what doesnt. In other words: help us help you :-) Reading our faq might help you in formulating your question in a way that allows us to help you in the best way possible How To ask –  Anders Rask Oct 19 '12 at 13:34
    
You get this, when specyfic item from the list isn't exists. Somthing doesn't exist on which You calling. –  Grzegorz Z Oct 19 '12 at 13:44
add comment

3 Answers

A couple of comments:

  • As @AndersRask mentioned in the comment put the restore of EventFiringEnabled in a finally block
  • Your current use of SPSecurity.RunWithElevatedPrivileges isn't doing anything except wasting resources. In order for it to allow more than the current user you need to allocate a new SPSite + SPWeb + SPList + SPListItem
  • My guess is that the lock also is completely useless
  • You should use SPListItem.SystemUpdate when doing changes behind the users back
  • If you don't want us to see the name of the document and user then you should also strike it out in the blue band
share|improve this answer
    
ok I expanded my code and I changed the image :) –  Gaetanu Oct 20 '12 at 4:07
    
have you guys others suggestion? –  Gaetanu Oct 21 '12 at 6:53
    
Have you tried to switch to SystemUpdate instead of Update? And if you're not using lock and RunWithElevatedPrivileges for anything remove them. –  Per Jakobsen Oct 21 '12 at 8:31
add comment

I had similar issue with ItemUpdating recently. When trying to update field in an updating item I had the same exception as yours. My solution was to change event receiver from ItemUpdating to ItemUpdated. If you need strictly ItemUpdating event then I suggest to run code which refers to item modification in a separate thread.

Of course such solution have less control over item behavior and may lead to concurrent update issues.

share|improve this answer
add comment

How Per wrote you, you don't need the SPSecurity.RunWithElevatedPrivileges and a lock on you code. I think you don't need a item.Update or SystemUpdate because you are in updateing. If the document it's new you have the error because sharepoint doesn't find it.

Try to remove all the code that the guys are suggest to you and move the base.ItemUpdating(properties); at the end of function an remove item.Update() method.

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.