Wednesday, March 8, 2023

VB.net Code to create Documwnts using Documentums REST Model 2

 Imports System.IO

Imports System.Net.Http

Imports System.Net.Http.Headers


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


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


Wednesday, December 28, 2011

Tuesday, January 25, 2011

Batch file for executing SQL Script from specified Folder

REM ******************************************************************
REM ** Batch File : Database_Desing_Procedures.Bat
REM ** Purpose : This batch file is used to deploy all the sql Procedures into the specified database
REM ** Author : Vasanth Thangasamy
REM ** Written Date: 17-Apr-2007
REM ** Version : 1.0
REM ******************************************************************

@echo off
setlocal
dir/b /on *.sql >tmp.lst


REM ******************************************************************
REM *** Put your Server name, Database name and login credential ***

set ServerName=HCL-SV74QJMEZMB\SQLEXPRESS
set DataBaseName=Universal
set Login=sa
set Password=DigitalServer

REM ******************************************************************
MD C:\DBLongs\Universal
set Connection=Sqlcmd -S %ServerName% -E -d %DataBaseName%

for /F %%i in (tmp.lst) do %Connection% -i %%i -o C:\DBLongs\Universal\%%i%.txt



endlocal


goto :eof