1

I need to get details of a document and its version details as well from one library and create its last major version in another library using CSOM.

I'm struck at the part where I should create the document with specific version. Is it possible that I can create a new document whose version is 5.0 in the target?

Shen Prabu
  • 155
  • 10
  • there are several posts about how to get a specific version. then you can upload that version to the new list. I suggest you search a bit, https://sharepoint.stackexchange.com/questions/156135/access-old-version-file-objects-with-csom https://stackoverflow.com/questions/18367014/document-version-history-via-csom – Tiago Duarte May 02 '18 at 15:35
  • @TiagoDuarte I'm able to get the version history from the source document as mentioned in your links. But I want to create only a specific version in the target with the same version as in source. – Shen Prabu May 03 '18 at 05:52

1 Answers1

0

Here's a working version

//params
string domain = "http://contoso.com";
string siteRelativeUrl = "/sites/aaa";
string listRelativeUrl = "/Accounting";
string documentName = "Demo Document 001.docx";
string localPath = "c:\\temp\\tests\\" + documentName;
int majorVersion = 1;
int minorVersion = 0;
int versionLabel = majorVersion * 512 + minorVersion;

string destinationListTitle = "Administrative";

//setup context
context = Helper.GetContextImpersonated(domain + siteRelativeUrl, UserName, Password, Domain);
Web web = context.Web;
context.Load(web, w => w.ServerRelativeUrl);
context.ExecuteQuery();

//format: /sites/aaa/_vti_history/512/Accounting/Demo Document 001.docx
string fileRelativeUrl = web.ServerRelativeUrl.TrimEnd('/') + "/_vti_history/" + versionLabel + listRelativeUrl + "/" + documentName;

Console.WriteLine("Downloading file: " + fileRelativeUrl);

using (var wc = new System.Net.WebClient())
{
    wc.Credentials = new System.Net.NetworkCredential(UserName, Password, Domain);
    wc.DownloadFile(domain + fileRelativeUrl, localPath);
}    

Console.WriteLine("Uploading file: " + localPath);

FileCreationInformation oCreateFile = new FileCreationInformation();
oCreateFile.Content = System.IO.File.ReadAllBytes(localPath);
oCreateFile.Url = documentName;
oCreateFile.Overwrite = false;

List spList = web.Lists.GetByTitle(destinationListTitle);
Microsoft.SharePoint.Client.File addedFile = spList.RootFolder.Files.Add(oCreateFile);
context.Load(addedFile);
context.ExecuteQuery();    

Console.WriteLine("File uploaded!");

I suggest that you check the below link for details on versioning urls,

How to download the historical file version content using CSOM

Tiago Duarte
  • 5,477
  • 2
  • 21
  • 43
  • spList.RootFolder.Files.Add(oCreateFile); will create a new file with default version settings of the document library. I want to manually set the version of the newly created document. Is that possible ? – Shen Prabu May 03 '18 at 13:58
  • you can upload versions 1, 2, 3, 4, and 5 from each of the source versions but you can't create a single file and expect it to be version 5 – Tiago Duarte May 03 '18 at 14:09
  • Then If I want to create new document as version 5 means, I have to create each versions and delete the versions from 1 to 4 ? – Shen Prabu May 03 '18 at 14:14
  • 1
    you have to upload all the 5 versions or upload it 5 times so it goes from version 1 to version 5. I don't think you can upload it as version 5 without going through the others. and yes you can delete the 4 versions after you are done, although I don't see why you would want to do that – Tiago Duarte May 03 '18 at 14:29
  • Ok, I will iterate and create the required version. How can I update the version details like version comment, created by and stuffs ? – Shen Prabu May 03 '18 at 15:03
  • created and modified by is the user you authenticate with in the network credentials. comments you can use by checking out and checking in with parameters, e.g. .CheckIn("my comments here") – Tiago Duarte May 03 '18 at 15:16