Service Based Architecture - How to insert an Item using VB.NET

This article shows how to update an item in VB.NET using Service Based Architecture for Dynamics GP 2015

Related Articles

... and you 'll find more on the ApisAndWebhooks Menu

 The code is self describing. We show here how to insert an item.

  

''' <summary>
''' Inserts a Dynamcs Item obect
''' </summary>
''' <param name="strUserName">Active Directory DomainName/UserName</param>
''' <param name="strPassword">Active Directory Password</param>
''' <param name="strMachineName">The name of the computer hosting the Service Based Architecture service</param>
''' <returns>Boolean</returns>
''' <remarks></remarks>
Function InsertItem(strUserName As String, strPassword As String, strMachineName As String) As Boolean
    'note that all the objects are fully qualified, to make the code more transportable
 
    'declare objects
    Dim byteData() As Byte
    Dim strNewItem As String = "{'Payload':{'ItemNumber':'DEMO6','ItemShortName':'Demo Item','ItemDescription':'Demo Item created with SBA','UOfMSchedule':'EACH'} }"
    Dim oResponse As System.Net.WebResponse = Nothing
    Dim oRequest As System.Net.HttpWebRequest
    Dim oReader As System.IO.StreamReader
    Dim oPostStream As System.IO.Stream = Nothing
 
    Try
        ' Create a byte array of the data we want to send 
        byteData = System.Text.UTF8Encoding.UTF8.GetBytes(strNewItem)
 
        'passing in the machine name makes the examples easier to code and publish
        Dim strUrl As String = String.Format("https://{0}/GPService/Tenants(DefaultTenant)/Companies(Fabrikam,%20Inc.)/Dynamics/Inventory/Items", strMachineName)
 
        ' Create the web request 
        oRequest = DirectCast(System.Net.WebRequest.Create(strUrl), System.Net.HttpWebRequest)
        oRequest.Method = "POST"
        oRequest.Accept = "application/json"
        oRequest.ContentType = "application/json"
        oRequest.ContentLength = strNewItem.Length
        oRequest.Credentials = New System.Net.NetworkCredential(strUserName, strPassword)
 
        ' Write data 
        oPostStream = oRequest.GetRequestStream()
        oPostStream.Write(byteData, 0, byteData.Length)
 
        ' Get response 
        oResponse = DirectCast(oRequest.GetResponse(), System.Net.HttpWebResponse)
 
        ' Get the response stream into a reader 
        oReader = New System.IO.StreamReader(oResponse.GetResponseStream())
        'read the response into a string for debugging, but it's not really useful to us.
        'if it succeeds, we're good
        Dim strResponse As String = oReader.ReadToEnd
 
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    Finally
        If Not oPostStream Is Nothing Then oPostStream.Close()
        If Not oResponse Is Nothing Then oResponse.Close()
    End Try
 
    Return True
 
End Function

 

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences