「東雲 忠太郎」の平凡な日常のできごと

2024/02/26(月)03:13

SharePointRestAPIで共有フォルダ内のファイルを移動するコード

VB.NET(26)

Imports System.Net.Http Imports System.Text Imports System.Threading.Tasks Imports System.IO Module Module1     Sub Main()         Dim siteUrl As String = "https://your-sharepoint-site-url"         Dim username As String = "your-username"         Dim password As String = "your-password"         Dim sourceFolderUrl As String = "/sites/your-site/shared documents/source-folder/" ' 元のフォルダのURL         Dim destinationFolderUrl As String = "/sites/your-site/shared documents/destination-folder/" ' 移動先のフォルダのURL         Dim fileName As String = "file.txt" ' 移動するファイルの名前         MoveFileInSharePoint(siteUrl, username, password, sourceFolderUrl, destinationFolderUrl, fileName).Wait()     End Sub     Async Function MoveFileInSharePoint(siteUrl As String, username As String, password As String, sourceFolderUrl As String, destinationFolderUrl As String, fileName As String) As Task         Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"))         ' 移動元のファイルのパスを取得         Dim sourceFileUrl As String = $"{siteUrl}/_api/web/getfilebyserverrelativeurl('{sourceFolderUrl}/{fileName}')"                  ' 移動先のフォルダにファイルをアップロードするためのエンドポイントURLを作成         Dim destinationFileUrl As String = $"{siteUrl}/_api/web/getfolderbyserverrelativeurl('{destinationFolderUrl}')/files/add(url='{fileName}',overwrite=true)"         Using client As New HttpClient()             client.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials)             client.DefaultRequestHeaders.Accept.Add(New System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))             ' 移動元のファイルを読み込む             Dim response As HttpResponseMessage = Await client.GetAsync(sourceFileUrl)             If response.IsSuccessStatusCode Then                 ' ファイルの内容を取得                 Dim fileBytes As Byte() = Await response.Content.ReadAsByteArrayAsync()                 Dim fileContent As New ByteArrayContent(fileBytes)                 ' 移動先にファイルをアップロード                 Dim uploadResponse As HttpResponseMessage = Await client.PostAsync(destinationFileUrl, fileContent)                 If uploadResponse.IsSuccessStatusCode Then                     Console.WriteLine($"File '{fileName}' moved successfully from '{sourceFolderUrl}' to '{destinationFolderUrl}'.")                                          ' 元のファイルを削除                     Dim deleteResponse As HttpResponseMessage = Await client.DeleteAsync(sourceFileUrl)                     If deleteResponse.IsSuccessStatusCode Then                         Console.WriteLine($"File '{fileName}' deleted successfully from '{sourceFolderUrl}'.")                     Else                         Console.WriteLine($"Failed to delete file '{fileName}' from '{sourceFolderUrl}'. StatusCode: {deleteResponse.StatusCode}")                     End If                 Else                     Console.WriteLine($"Failed to move file '{fileName}' to '{destinationFolderUrl}'. StatusCode: {uploadResponse.StatusCode}")                 End If             Else                 Console.WriteLine($"Failed to retrieve file '{fileName}' from '{sourceFolderUrl}'. StatusCode: {response.StatusCode}")             End If         End Using     End Function End Module

続きを読む

このブログでよく読まれている記事

もっと見る

総合記事ランキング

もっと見る