2024/02/26(月)02:46
VB.NETでSharePointリストから特定のリストアイテムを削除するコード
Imports System.Net.Http
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 ' 削除するアイテムのID
DeleteListItem(siteUrl, username, password, listName, itemId).Wait()
End Sub
Async Function DeleteListItem(siteUrl As String, username As String, password As String, listName As String, itemId As Integer) As Task
Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"))
Using client As New HttpClient()
client.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials)
Dim endpointUrl As String = $"{siteUrl}/_api/web/lists/getbytitle('{listName}')/items({itemId})"
Dim response As HttpResponseMessage = Await client.DeleteAsync(endpointUrl)
If response.IsSuccessStatusCode Then
Console.WriteLine($"Item with ID {itemId} deleted successfully from list '{listName}'.")
Else
Console.WriteLine($"Failed to delete item. StatusCode: {response.StatusCode}")
End If
End Using
End Function
End Module