SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Any document library where the list has EnableModeration set to true causes an exception to be thrown in a powershell cmdlet that uploads a file.

The checkout, file upload, checkin, and publishing work. However, when I get to activeFile.Approve("") an exception of type SPException and message of "The file doclib/file has been modified by {FarmAccount} on {date}".

This error message is correct, the file has been modified but it has also been checked in and published. Just not approved. I have way too many files to go in and approve them all by hand in multiple places, and while I can write a console app to approve all of these after the fact, id rather nip this in the bud.

Here is the code of the segmet performing this action:

public void foo2(SPFolder folder, SPFile activeFile, string name, string filepath, ResultCollection resultsCollection)
        folder.ParentWeb.AllowUnsafeUpdates = true;
        DebugLog("Replacing existing file " + name + " in folder " + folder.Url);

            // Force CheckinpeopleChangesPage.ListItem.File.CheckOutStatus != SPFile.SPCheckOutStatus.None
            if (activeFile.CheckOutType != SPFile.SPCheckOutType.None && activeFile.Item.ListItems.List.EnableVersioning)
                activeFile.CheckIn("Forced Check-in");

            if (activeFile.Item.ListItems.List.EnableVersioning)


            if (activeFile.Item.ListItems.List.EnableVersioning)
                activeFile.CheckIn("File uploaded via api");
            if (folder.ParentWeb.Lists[folder.ParentListId].EnableMinorVersions)
                activeFile.Publish("Published automatically via api");
            if (activeFile.Item.ListItems.List.EnableModeration)
                activeFile.Approve("Approved via api");

            resultsCollection.Add(new Result(true,
                                             "Replaced existing file " + name + " in folder " +
                                             folder.ParentWeb.Url + "/" + folder.Url));

        catch (Exception ex)
            DebugLog("Failing file replacement: " + ex.StackTrace);
            resultsCollection.Add(new Result(false,
                                             "Failed to replace " + name + " in folder " +
                                             folder.ParentWeb.Url + "/" + folder.Url + ": " +
            folder.ParentWeb.AllowUnsafeUpdates = false;

I had to extract it from a delegate that was going to run with elevated privileges, so forgive the name.

share|improve this question
up vote 2 down vote accepted

I believe that this is because calling .Update() essentially invalidates your handle to the file object as backend operations now can modify it outside of your control (workflows, approvals, event receivers, etc..) Specifically, the underlying file object has changed but your handle has not. In the past, I've had to refetch the SPFile object after calling .Update() in order to do further work with it.

share|improve this answer
Ill give that a shot and let you know how it works. – John S Apr 17 '12 at 15:01
I set activeFile = null then refetched it but am still getting the same error. – John S Apr 17 '12 at 15:35
How are you refetching the file? – Dave Wise Apr 17 '12 at 15:38
activeFile = null; activeFile = folder.Files[name]; – John S Apr 17 '12 at 15:51
Just tried SPListItem item = activeFile.Item; activeFile = item.File; and it worked. You definitely pointed me in the right direction. THanks! – John S Apr 17 '12 at 16:01

Your Answer


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.