SharePoint Onlineから指定したURLのファイルを指定したフォルダにダウンロードする
2020-07-15 22:48:41
var mail = "ログインメアド"; var pass = "ログインパスワード"; // ▽以下、ダウンロードしたいファイルのあるURLに置き換え。 var siteUrl = "https://nyaoudev.sharepoint.com/sites/nyaouDev/"; var relativeUrl = "/sites/nyaouDev/Shared Documents/General/"; var downloadFileName = "111.xlsx"; var restUrl = @"/_api/web/GetFileByServerRelativeUrl('" + relativeUrl + downloadFileName + "')/$value"; Uri endpointUri = new Uri(siteUrl + restUrl); var downloadLocalFolder = @"ダウンロード先のローカルPCのパス"; SecureString secureString = new SecureString(); foreach (char c in pass.ToCharArray()) { secureString.AppendChar(c); } ICredentials credentials = new SharePointOnlineCredentials(mail, secureString); using (WebClient client = new WebClient()) { client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); client.Credentials = credentials; byte[] data = client.DownloadData(endpointUri); FileStream outputStream = new FileStream(downloadLocalFolder + downloadFileName, FileMode.Create, FileAccess.Write, FileShare.None); outputStream.Write(data, 0, data.Length); outputStream.Flush(true); outputStream.Close(); }