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);
try
{
// 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)
{
activeFile.CheckOut();
}
activeFile.SaveBinary(File.ReadAllBytes(filepath));
activeFile.Update();
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 + ": " +
ex.Message));
}
finally
{
folder.ParentWeb.AllowUnsafeUpdates = false;
}
}
I had to extract it from a delegate that was going to run with elevated privileges, so forgive the name.