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 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))
        file.UndoCheckOut();
    file.CheckOut();
    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);
    file.Context.ExecuteQuery();
    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?

share|improve this question
add comment

1 Answer

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.)

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.