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

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

2024.02.26
XML
カテゴリ: VB.NET


Imports System.IO

Imports System.Net.Http

Imports System.Net.Http.Headers

Imports System.Text

Imports System.Threading.Tasks


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 listName As String = "YourListName"

        Dim itemId As Integer = 10

        Dim filePath As String = "path\to\your\file.txt" ' 添付するファイルのパス


        AttachFileToListItem(siteUrl, username, password, listName, itemId, filePath).Wait()

    End Sub


    Async Function AttachFileToListItem(siteUrl As String, username As String, password As String, listName As String, itemId As Integer, filePath As String) As Task

        Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"))


        Using client As New HttpClient()

            client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Basic", credentials)


            Dim fileContent As New StreamContent(File.OpenRead(filePath))

            fileContent.Headers.ContentDisposition = New ContentDispositionHeaderValue("form-data") With {

                .Name = "file",

                .FileName = Path.GetFileName(filePath)

            }


            Dim multipartContent As New MultipartFormDataContent()

            multipartContent.Add(fileContent)


            Dim endpointUrl As String = $"{siteUrl}/_api/web/lists/getbytitle('{listName}')/items({itemId})/AttachmentFiles/add(FileName='{Path.GetFileName(filePath)}')"


            Dim response As HttpResponseMessage = Await client.PostAsync(endpointUrl, multipartContent)


            If response.IsSuccessStatusCode Then

                Console.WriteLine($"File '{Path.GetFileName(filePath)}' attached to item with ID {itemId} successfully.")

            Else

                Console.WriteLine($"Failed to attach file. StatusCode: {response.StatusCode}")

            End If

        End Using

    End Function

End Module







お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.02.26 02:44:39


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: