Wednesday, March 8, 2023

VB.Net code to create Doc ID and Doc Content using Documentum REST API Model



Model 1:

Imports System.Net.Http Imports System.Net.Http.Headers Imports Newtonsoft.Json Public Class DocumentumRESTClient Private Const DocbaseName As String = "myDocbase" Private Const Username As String = "myUsername" Private Const Password As String = "myPassword" Private Const RestUrl As String = "http://mydocbase.com/dctm-rest" Public Async Function CreateDocumentAsync(ByVal documentName As String, ByVal documentContent As Byte()) As Task(Of String) Dim accessToken = Await GetAccessTokenAsync() Dim docId = Await CreateDocIdAsync(accessToken, documentName) Await CreateDocContentAsync(accessToken, docId, documentContent) Return docId End Function Private Async Function GetAccessTokenAsync() As Task(Of String) Using client As New HttpClient() Dim authHeader = New AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{Username}:{Password}"))) client.DefaultRequestHeaders.Authorization = authHeader Dim response = Await client.PostAsync($"{RestUrl}/authentication/token", Nothing) response.EnsureSuccessStatusCode() Dim jsonResponse = Await response.Content.ReadAsStringAsync() Dim tokenResponse = JsonConvert.DeserializeObject(Of TokenResponse)(jsonResponse) Return tokenResponse.AccessToken End Using End Function Private Async Function CreateDocIdAsync(ByVal accessToken As String, ByVal documentName As String) As Task(Of String) Using client As New HttpClient() client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", accessToken) client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json")) Dim content = New StringContent(JsonConvert.SerializeObject(New With {.properties = New List(Of DocumentumProperty) From {New DocumentumProperty With {.Name = "object_name", .Value = documentName}}} ), Encoding.UTF8, "application/json") Dim response = Await client.PostAsync($"{RestUrl}/repositories/{DocbaseName}/objects", content) response.EnsureSuccessStatusCode() Dim jsonResponse = Await response.Content.ReadAsStringAsync() Dim objectResponse = JsonConvert.DeserializeObject(Of ObjectResponse)(jsonResponse) Return objectResponse.Object.Id End Using End Function Private Async Function CreateDocContentAsync(ByVal accessToken As String, ByVal docId As String, ByVal documentContent As Byte()) As Task Using client As New HttpClient() client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", accessToken) client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json")) Dim content = New ByteArrayContent(documentContent) content.Headers.ContentType = New MediaTypeHeaderValue("application/octet-stream") Dim response = Await client.PutAsync($"{RestUrl}/repositories/{DocbaseName}/objects/{docId}/contents", content) response.EnsureSuccessStatusCode() End Using End Function End Class Public Class TokenResponse Public Property AccessToken As String Public Property ExpiresIn As Integer Public Property TokenType As String End Class Public Class DocumentumProperty Public Property Name As String Public Property Value As Object End Class Public Class ObjectResponse Public Property Object As ObjectInfo End Class Public Class ObjectInfo Public Property Id As String End Class


No comments:

Post a Comment