【Pleasanter】APIで複数レコードを取得する処理をVB.NETで書いてみた

# サンプルデータとして登録されていた「顧客名簿」のデータを外部アプリから参照する処理は以下のとおりです。

```vb:VB.NET
Imports System.Net.Http
Imports System.Text
Imports System.Threading.Tasks
Imports Newtonsoft.Json

Public Class Form1
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim jsonStr = Await getRecords()

        'Json文字列をJson形式データに復元する
        Dim jsonObj = JsonConvert.DeserializeObject(jsonStr)

        'データ格納用のDatatableを作成する。
        Dim dt As New DataTable
        makeSchema(dt)

        For Each js In jsonObj("Response")("Data")
            Dim row As DataRow
            row = dt.NewRow
            row("UpdatedTime") = js("UpdatedTime")
            row("ClassB") = js("ClassHash")("ClassB")
            row("ClassE") = js("ClassHash")("ClassE")
            dt.Rows.Add(row)
        Next

        'DataGridViewへ表示する。
        dgv.DataSource = dt
        Me.lblCnt.Text = dt.Rows.Count
    End Sub

    Private Async Function getRecords() As Task(Of String)
        Dim uri As String = "http://{サーバ指定}/api/items/{サイトID}/get"
        Dim param As Dictionary(Of String, String) = New Dictionary(Of String, String)() From {
            {"ApiVersion", "1.1"},
            {"ApiKey", "ApiKeyをコピペ"}
        }
        Using httpClient As HttpClient = New HttpClient()
            Dim json As String = JsonConvert.SerializeObject(param, Formatting.None, New JsonSerializerSettings With {.NullValueHandling = NullValueHandling.Ignore})
            Dim content As HttpContent = New StringContent(json, Encoding.UTF8, "application/json")
            Dim response As HttpResponseMessage = Await httpClient.PostAsync(uri, content)
            Dim result As String = Await response.Content.ReadAsStringAsync()
            Return result
        End Using
    End Function

    Private Sub makeSchema(ByRef dt As DataTable)
        dt.Columns.Add("UpdatedTime", Type.GetType("System.String"))
        dt.Columns.Add("ClassB", Type.GetType("System.String"))
        dt.Columns.Add("ClassE", Type.GetType("System.String"))
    End Sub

End Class

```
# レコード追加の例
```vb:VB.NET
    ''' <summary>
    ''' PleasanterAPIでレコードを追加する。
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Async Function insertRecords() As Task(Of String)

        Dim uri As String = serverUrl & "/api/items/1/create"

        Dim str As String = "{""ApiVersion"": 1.1, ""ApiKey"": """ & ApiKey & """,""ClassHash"": { ""ClassB"": ""山田太郎"" , ""ClassE"": ""01203334444"" , ""ClassR"": ""vb.netでapiテスト"" }}"
        Dim param As Dictionary(Of String, Object) = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(str)

        Using httpClient As HttpClient = New HttpClient()
            Dim json As String = JsonConvert.SerializeObject(param, Formatting.None, New JsonSerializerSettings With {.NullValueHandling = NullValueHandling.Ignore})

            Dim content As HttpContent = New StringContent(json, Encoding.UTF8, "application/json")

            Dim response As HttpResponseMessage = Await httpClient.PostAsync(uri, content)
            Dim result As String = Await response.Content.ReadAsStringAsync()
            Return result
        End Using

    End Function
```

# 残っている課題
1. 「変更履歴」レコードの取り出し方が不明
1. 「コメント」レコードの取り出し方が不明


# PleasanterのApi説明についての詳細は以下
https://pleasanter.org/manual/api-record-get-multi

https://pleasanter.org/manual/api-record-create