Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal static void DownloadFilesFromSharePoint(string siteUrl, string folderPath, string tempLocation)
- {
- string username = "my.username@company.com";
- string passwordFromForm = "password.from.form";
- var securedPassword = new SecureString();
- foreach (var c in passwordFromForm.ToCharArray()) securedPassword.AppendChar(c);
- ClientContext ctx = new ClientContext(siteUrl);
- ctx.Credentials = new SharePointOnlineCredentials(username, securedPassword);
- FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(folderPath).Files;
- ctx.Load(files);
- ctx.ExecuteQuery(); //Error here
- foreach (Microsoft.SharePoint.Client.File file in files)
- {
- FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
- ctx.ExecuteQuery();
- var filePath = tempLocation + file.Name;
- using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
- {
- fileInfo.Stream.CopyTo(fileStream);
- }
- }
- }
Add Comment
Please, Sign In to add comment