Guest User

Untitled

a guest
Mar 22nd, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. internal static void DownloadFilesFromSharePoint(string siteUrl, string folderPath, string tempLocation)
  2. {
  3. string username = "my.username@company.com";
  4. string passwordFromForm = "password.from.form";
  5.  
  6. var securedPassword = new SecureString();
  7. foreach (var c in passwordFromForm.ToCharArray()) securedPassword.AppendChar(c);
  8. ClientContext ctx = new ClientContext(siteUrl);
  9. ctx.Credentials = new SharePointOnlineCredentials(username, securedPassword);
  10.  
  11. FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(folderPath).Files;
  12.  
  13. ctx.Load(files);
  14. ctx.ExecuteQuery(); //Error here
  15.  
  16. foreach (Microsoft.SharePoint.Client.File file in files)
  17. {
  18. FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
  19. ctx.ExecuteQuery();
  20.  
  21. var filePath = tempLocation + file.Name;
  22. using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
  23. {
  24. fileInfo.Stream.CopyTo(fileStream);
  25. }
  26. }
  27. }
Add Comment
Please, Sign In to add comment