2

When I check in as a major version or minor version, it creates the new version as a major. Below is the code:

private static void UploadFiles(string sourceFileUrl, Folder targetFolder, string fileName,ClientContext clientContext,ClientContext destinationContext, bool bolMajorVersion) { FileCreationInformation targetFileVersionCreationInfo = new FileCreationInformation(); targetFileVersionCreationInfo.Overwrite = true; try { WebRequest request = HttpWebRequest.Create(sourceFileUrl); request.Credentials = clientContext.Credentials; using (WebResponse response = request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { byte[] verBuffer = new byte[32768]; using (MemoryStream versionMS = new MemoryStream()) { int read; while ((read = stream.Read(verBuffer, 0, verBuffer.Length)) > 0) { versionMS.Write(verBuffer, 0, read); } versionMS.Seek(0, SeekOrigin.Begin); targetFileVersionCreationInfo.ContentStream = versionMS; destinationContext.RequestTimeout = System.Threading.Timeout.Infinite; targetFileVersionCreationInfo.Url = targetFolder.ServerRelativeUrl+ "/" + fileName; Microsoft.SharePoint.Client.File targetVersionFile = targetFolder.Files.Add(targetFileVersionCreationInfo); Microsoft.SharePoint.Client.File file = targetVersionFile; //Checkout if checked in destinationContext.Load(file); destinationContext.ExecuteQuery(); if (file.CheckOutType == CheckOutType.None) { file.CheckOut(); } //ListItem lstItem = file.ListItemAllFields; //clientContext.Load(lstItem); //clientContext.ExecuteQuery(); if (bolMajorVersion) { file.CheckIn("Uploaded through portal", CheckinType.MajorCheckIn); } else { //Overwrite to avoid creating new version file.CheckIn("Uploaded through portal", CheckinType.MinorCheckIn); } destinationContext.ExecuteQuery(); // destinationContext.ExecuteQuery(); } } } } catch (Exception ex) { //handle exception } } 

The file initially upload with version 0.1 after check in as a major version, it creates new version 1.0 and also keep version 0.1. I need to overwrite this 0.1 version file.

Any help should be appreciated.

2 Answers 2

0

The targetFolder.Files.Add is checking the file out too. So before you are going to overwrite the file, first retrieve itwith a try catch around it (in case it doesn't exist) and check it out. After that add the file with targetFolder.Files.Add(), do something with metadata if you like and check it in.

1
  • Got to rectify this a little. This does not work when the file doesn't exist. In that case you might indeed want to use SystemUpdate, like @GVIrish mentioned, but in my case that doesn't work as I still got to support SP2013 for the application I'm working on. Commented Mar 29, 2018 at 13:40
0

You should be able to use SystemUpdate() on the SPListItem attached to your file before you call ExecuteQuery(). So it should be something like

file.Item.SystemUpdate(); destinationContext.Load(file); destinationContext.ExecuteQuery(); 

You may have to play with the order of the statements a bit, but I think SystemUpdate() is the function you're looking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.