如何将文件复制到共享点站点

How to copy a file to a sharepoint site

我必须在共享点网站上复制一个文件。 我已经看到唯一有效的身份验证是使用 AuthenticationManager。 所以这有效:

var authManager = new AuthenticationManager();
var ctx = authManager.GetWebLoginClientContext(strHexagon);
Web web = ctx.Web;
User user = web.CurrentUser;
ctx.Load(web);
ctx.Load(user);
ctx.ExecuteQuery();

lbxInfo.Items.Add(web.Title);
lbxInfo.Items.Add(user.LoginName);

现在,在通过身份验证后,我需要将文件复制到共享点站点。 我已经看到有 ctx.Web.SaveFileToLocal 但是如果我必须将 从本地复制到共享点 怎么办?

谢谢

您可以使用 OfficeDevPnP.Core 库

string str1_Url=...                         <--- sharepoint site
string str2_FileSource_Full= @"C:\temp\A.txt";
string str3_FileDestination_NameExt="B.txt";
string str4_TopDestination_Folder=...       <--- sharepoint site title folder
string str5_TopDestination_SubFolder=...    <--- folder e.g. Production
string str6_TopDestination_AllSubFolders=...<--- subfolder e.g. Test 

// AuthenticationManager -> ByPasses Multi-Factor Authentication
var authManager = new AuthenticationManager();
var ctx = authManager.GetWebLoginClientContext(str1_Url);

// Web & User definitions
Web web = ctx.Web;
User user = web.CurrentUser;

FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(str2_FileSource_Full);

// Rename the destination file
newFile.Url = str3_FileDestination_NameExt;

Microsoft.SharePoint.Client.List docs = web.Lists.GetByTitle(str4_TopDestination_Folder);

// Selects a Folder inside the root one
Microsoft.SharePoint.Client.Folder folder = docs.RootFolder.Folders.GetByUrl(str5_TopDestination_SubFolder);

folder.Folders.Add(str6_TopDestination_AllSubFolders);
var targetFolder = folder.Folders.GetByUrl(str6_TopDestination_AllSubFolders);

// Uploads a file to the targetFolder
newFile.Overwrite = true;
Microsoft.SharePoint.Client.File uploadFile = targetFolder.Files.Add(newFile);

// Executes query
ctx.Load(docs);
ctx.Load(uploadFile);
ctx.Load(web);
ctx.Load(user);
ctx.ExecuteQuery();