0

I need to copy a selected file in the same document library in sharepoint online using csom. I have used below code and it work

// get the file
                File file = sourceItem.File;
                clientContext.Load(file);
                clientContext.ExecuteQuery();

                char[] chars = { '.' };
                var fileName = file.Name.Split(chars);
                // build destination url
                string locationUrl = web.ServerRelativeUrl.TrimEnd('/') + "/" + destinationList.Title + "/" + fileName[0] + "-Copy." + fileName[1];

                // read the file, copy the content to new file at new location
                file.CopyTo(locationUrl, true);
                clientContext.ExecuteQuery();`

It copies all the fields in the new item generated. How do I apply condition and skip specific fields so as to not update and have them blank.

Any suggestions please?

0

Rather than copy the file, you can add the new document to the Document Library with the existing one by disabling the Overwrite property.

    //Prepare to upload
    Boolean replaceExisting = false;
    String fileName = file.Url;
    FileStream fileStream = File.OpenRead(fileToUpload);

    // Upload document
    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExisting);

myLibrary.Update();

| improve this answer | |
  • Seems the above code doesn't work for sharepoint online. I need csom code. – MJaiswal Mar 22 '16 at 10:19
  • As I told, Please try to add the document rather than copying it. Refer the link below link – Mohamed Arif Mar 22 '16 at 10:31
  • What error did you got? Please let me know – Mohamed Arif Mar 22 '16 at 10:32
  • it seems for server side. – MJaiswal Mar 22 '16 at 12:07
  • Change the server side to client side. It is very simple. Replace SPFile to FIle. Thats it – Mohamed Arif Mar 23 '16 at 5:07

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.