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.