In C# with CSOM, How can I copy a file from a known location to a known Folder?
-
18-02-2021 - |
문제
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:
- The filename,
- The file location, and
- 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;
해결책
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();
제휴하지 않습니다 sharepoint.stackexchange