0

I have one function which uses SearchExecutor finds to find the path of a specified file, which returns a string like.

https://example.sharepoint.com/sites/DevelopementTestingSite/Shared Documents/Forms/DispForm.aspx?ID=43

I have another function which creates and returns a Microsoft.SharePoint.Client.Folder.

Give:

  1. The filename,
  2. The file location, and
  3. The Folder.

How can I copy the file to the folder?

If it helps, the ServerRelativePath has been initialized on the Folder with:

            Folder itemFolder = folderItem.Folder;
            context.Load(itemFolder, folder => folder.ServerRelativePath);
            context.ExecuteQuery();
            return itemFolder;
1

In the string https://example.sharepoint.com/sites/DevelopementTestingSite/Shared Documents/Forms/DispForm.aspx?ID=43, you could get the file by the id. Then copy to the folder.

My test demo code :

        Web web = ctx.Web;
        List list = web.GetListByTitle("Documents");
       // ctx.Load(list);
        //ctx.ExecuteQuery();
        ListItem item = list.GetItemById(2);
        File file = item.File;
        ctx.Load(file);
        ctx.ExecuteQuery();
        item.File.CopyTo("/sites/test/Shared%20Documents/folder1/"+item.File.Name, true);
        ctx.ExecuteQuery();
2
  • cheers for the info. I'm trying to play with it currently. Do I correctly understand that I can disregard most of the file location other than the id, as long as I access the "Documents" List? Nov 24 '20 at 10:52
  • 1
    Yes. You only need to use the listname and the id, that will be enough. Nov 25 '20 at 9:42

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.