I would like to programatically update a SharePoint file by checkout/checkin.

I have written code that looks like it should do what I want it to do:

static void HandleFile(Microsoft.SharePoint.Client.File file, byte[] bytes, Logger<bool> Log)
    if ((file.CheckOutType != CheckOutType.None) && Log(true, "Undoing previous Check Out for file: {0}", file.Name))
    Log(true, "Successfully Checked out file: {0}", file.Name);
    file.SaveBinary(new FileSaveBinaryInformation { Content = bytes });
    file.CheckIn("Comment", CheckinType.MajorCheckIn);
    var versions = file.Context.LoadQuery(file.Versions);
    Log(true, "Successfully Checked in file: {0} (versions: {1})", file.Name, versions.Count());

However, you can probably guess that it does not work as I anticipate.

The code will update the File with the new data (bytes), but will not create a new version of the File (rather, it overwrites it), as my final logging line and a look at the SharePoint Library in my browser will confirm.

I've tried adding calls to file.ListItemAllFields.Update() at various places. If I add it before the SaveBinary call, I will see the same result I mentioned. If I add it after the SaveBinary call, the code will throw an Exception:

Microsoft.SharePoint.Client.ServerException: Version conflict.

To me, at least it acknowledges the version in that case.

But, how do I programatically update a file and create an entry in the version history with the SharePoint Client Object Model?

2 Answers 2


In addition to the code, the List/Document Library needs to have enabled the versioning setting(s) as well.

In the SharePoint ribbon, in the Library tab, select the Library Settings button in the Settings area. In the settings page, under the General Settings header, you'll see Versioning settings. Here, you can select settings for versioning, and if you configure the Library to support versioning the code will work.

Exercise caution with the CheckInType value in the File.CheckIn function. If your Library supports Major and Minor versions, trying to pass CheckInType.MajorCheckIn to the CheckIn function will cause an error if you don't also handle some items regarding "publishing". (I have only successfully used CheckInType.MinorCheckIn with Major and Minor versioning.)


in case some other stumbles upon the same problem, this it what worked for me. (I updated a field value instead of the file though)

item["Sprakkode"] = theValue; //UPDATE OPERATION GOES HERE
item.File.CheckIn("", CheckinType.MajorCheckIn);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.