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?

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

  • 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 3 '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 3 '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 3 '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 3 '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 3 '18 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.