Uploading file version - Client Object Model
-
16-10-2019 - |
Question
I have a library with versioned files. I have versioned files prepared for uploading (1 version = 1 file). How do I create new version? Do I just set file's Content property and Update it? Please share your experinces, thanks
edit: I have something like this:
File1|v1
File1|v2
File1|v4
File2|v2
File2|v5
So I have missing versions. Is it possible to set file version when uploading (item's field?)? If not, I will fill missing version with previous/next versions, for example File2|v1 will be filled with File2|v2.
Solution
I don't know how to programmatically assign the version number, but if it is OK to let SharePoint assign the version number...
With the server version of the method:
SPFileCollection.Add(urlOfFile, fileContent, overwrite);
if you pass in overwrite=true and the document library has versioning turned on, I think it will create a new version of the document. (That's the behavior through the browser.) I assume the Client Object Model version of the method would behave the same way.
FileCreationInformation fci = new FileCreationInformation();
fci.Url = urlOfFile;
fci.Content = fileContent;
fci.Overwrite = true;
folder.files.Add(fci);
So, as long as you upload the files in the correct order of their versions, SharePoint should create each version for you.
OTHER TIPS
Here is a little code sample that will simulate versioning of a file
public File changeDocumentVersion(string version,File file){
string [] versions = version.Split(new char[]{'.'});
int minor = Int32.Parse(versions[1]);
int major = Int32.Parse(versions[0]);
for (int i = 1; i < major; i++)
{
file.CheckOut();
file.CheckIn("versioning", CheckinType.MajorCheckIn);
}
for (int i = 0; i < minor; i++)
{
file.CheckOut();
file.CheckIn("versioning", CheckinType.MinorCheckIn);
file.Context.Load(file);
file.Context.ExecuteQuery();
}
return file;
}
As long as you have enabled versioning (found under list/library settings, then versioning) enabled SP will take care of the versioning for you.