Monday, 16 February 2015

"The File is locked for shared use by user in Sharepoint" Error Solution

When we write code in event receiver to add some custom column value on item added or item updated , sometimes we encounter error - "The File is locked for shared use by user". 

Here is the solution for this king of error.

Error code:

public override void ItemAdded(SPItemEventProperties properties)
        {
            try
            {
                base.ItemAdded(properties);
                base.EventFiringEnabled = false;
                SPListItem item = properties.ListItem;                
                item["ColName1"] = "Value";
                item["ColName1"] = "Value";
                item.Update(); // Here it throws error
                      
                base.EventFiringEnabled = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Solution : item.SystemUpdate();

public override void ItemAdded(SPItemEventProperties properties)
        {
            try
            {
                base.ItemAdded(properties);
                base.EventFiringEnabled = false;
                SPListItem item = properties.ListItem;                
                item["ColName1"] = "Value";
                item["ColName1"] = "Value";
                item.SystemUpdate();
                      
                base.EventFiringEnabled = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

No comments:

Post a Comment