Page 1 of 1

vb.net using apikey examples

Posted: Thu Jun 02, 2022 11:56 pm
by TMattar
I am attempting to do some API calls into SP. I would like to build a small query process using VB.net using an APIKEY authorization. (I already have an apikey generated). I have been all over this site but have not found any vb.net examples. I just need a few samples on authenticating and passing parameters to an API call.


Any assistance is greatly appreciated!

Re: vb.net using apikey examples

Posted: Wed Jul 13, 2022 3:42 am
by dmpchch
Hi, this is pretty rough but hope it helps.

API key (Bearer grabbed from textbox)
this takes PDF attachments from emails (MS outlook) and saves them as attachments to the job

Code: Select all

 Sub Mail()

        Dim olApp As Microsoft.Office.Interop.Outlook.Application
        Dim ns As [NameSpace]
        Dim Inbox As MAPIFolder
        Dim Item As MailItem
        Dim SubFolder As MAPIFolder
        Dim Atmt As Attachment
        Dim FileName As String
        Dim Num As Integer
        Dim cnt3
        Dim SF
        Dim Loc
        Dim bearer
        Dim companies
        Dim f2 As New Form2
        Dim minval
        Dim Rf
        Dim Rj
        Dim UrL
        Dim FileType
        Dim Bodytext As String
        SF = f2.TextBox6.Text
        Loc = f2.TextBox5.Text
        bearer = f2.TextBox2.Text

        minval = f2.TextBox4.Text
        UrL = f2.TextBox1.Text
        Rf = f2.TextBox7.Text
        Rj = f2.TextBox8.Text

        '###########################################
        'Look in inbox subfolder iauditor for emails
        '###########################################
        olApp = CreateObject("Outlook.Application")
        ns = olApp.GetNamespace("MAPI")
        Inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox)
        SubFolder = Inbox.Folders(SF)

        For Each Item In SubFolder.Items

            cnt3 = 0

            If IsNumeric(Item.Subject) Then
                Num = Convert.ToDouble(Item.Subject)


                If Num > minval Then
                    companies = Company(Num)
                    Dim fn As String
                    Bodytext = Item.Body
                    FileType = FormType(Bodytext)


                    For Each Atmt In Item.Attachments

                        fn = GetExtension(Atmt.FileName)



                        If fn = "pdf" Then
                            fn = Atmt.DisplayName
                            FileName = Loc & Num & ".pdf"
                            cnt3 = cnt3 + 1
                            Atmt.SaveAsFile(FileName)
                            Item.Delete()
                            Dim httpObject As Object
                            httpObject = CreateObject("MSXML2.XMLHTTP")
                            Dim OrDer
                            Dim Bs
                            Dim sRequest As String
                            Dim sgetresult As String
                            Dim sURL As String
                            Dim DT = DateTime.Parse(Now()).ToString("dd_MM_yyyy_HH_mm")
                            Dim JSONString As String

                            Bs = ConvertFileToBase64("C:\Enable_Daily_CSV\8_Scope\" & Num & ".pdf")
                            JSONString = "{""Filename"":""" & FileType & "_" & DT & "_" & Num & ".pdf"",""Public"": true,""Base64Data"":""" & Bs & """}"
                            OrDer = Num


                            sURL = UrL & companies & "/jobs/" & Num & "/attachments/files/"
                            sRequest = sURL
                            httpObject.Open("Post", sRequest, False)

                            httpObject.setRequestHeader("Authorization", bearer)
                            httpObject.setRequestHeader("Content-Type", "application/json")
                            httpObject.send(JSONString)
                            sgetresult = httpObject.responseText
                            'MessageBox.Show("Attachment created in SimPRO")
                            FileSystem.MoveFile(FileName, "C:\some folder\8_Scope\Actioned\" & Num & "_" & FileType & ".pdf", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.ThrowException)
                        End If

                    Next Atmt

                    If cnt3 < 1 Then
                        Item.Reply()
                        Item.Body = Rf
                        Item.Send()

                    End If

                Else
                    Item.Reply()
                    Item.Body = Rj
                    Item.Send()

                End If
            Else
                Item.Reply()
                Item.Body = Rj
                Item.Send()

            End If


        Next Item

    End Sub 'Main Code