header
DavidM 4/11/2015 7:30:33 AM

Service Based Architecture - Get a Site

This article contains sample code for getting a Site (Location/Warehouse) from Dynamics GP using Service Based Architecture.

 

Version:
Section:


Add a reference in your application to Microsoft.Dynamics.GP.BusinessObjects which can be found in the Microsoft Dynamics\GP 2015 directory.  

First, add the JSONSite class:

 

Imports System.Xml.Serialization
 
<Serializable> _
Public Class JSONSite
    <XmlElement()> _
    Public Status As Dictionary(Of String, Object)
    <XmlElement()> _
    Public Payload As Microsoft.Dynamics.GP.BusinessObjects.Inventory.Site
 
End Class

Then add the main code:

Function GetSite(strUserName As String, strPassword As String, strMachineName As String) As Microsoft.Dynamics.GP.BusinessObjects.Inventory.Site
    'note that all the objects are fully qualified, to make the code more transportable
 
    'initialize an object that will represent a Dynamics Site object
    Dim oJSONSite As JSONSite = Nothing
    Try
        '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/Sites(01-N)", strMachineName)
 
        Dim oRequest As System.Net.HttpWebRequest
        Dim oResponse As System.Net.WebResponse = Nothing
 
        ' Create the web request 
        oRequest = DirectCast(System.Net.WebRequest.Create(strUrl), System.Net.HttpWebRequest)
 
        ' Add authentication to request 
        oRequest.Credentials = New System.Net.NetworkCredential(strUserName, strPassword)
 
        ' Get the response 
        oResponse = DirectCast(oRequest.GetResponse(), System.Net.WebResponse)
        'convert the response to a Stream
        Dim oStream As System.IO.Stream = oResponse.GetResponseStream()
        'convert the Stream to a StreamReader
        Dim oStreamReader As New System.IO.StreamReader(oStream)
        'read the response into a String
        Dim strResponse As String = oStreamReader.ReadToEnd
 
        'initialize a JavaScriptSerializer object
        Dim js As New System.Web.Script.Serialization.JavaScriptSerializer()
 
        'this is method #1, we'll read the Response into a Site object
        'deserialize the response into the Site object
        oJSONSite = js.Deserialize(Of JSONSite)(strResponse)
        'this is an example of how the data is used
        Dim strLocationCode As String = oJSONSite.Payload.LocationCode
 
        'this is method #2, we'll read the response into a dictionary object
        'this dictionary will have two nodes, Status and Payload
        Dim dicResponse As Dictionary(Of String, Object) = js.Deserialize(Of Dictionary(Of String, Object))(strResponse)
        'this dictionary takes the Payload out and makes it easier to access
        Dim dicPayload As Dictionary(Of String, Object) = dicResponse("Payload")
        Dim strLocationCode2 As String = dicPayload("LocationCode")
 
 
    Catch ex As Exception
        MsgBox(ex.Message)
 
    End Try
 
    Return oJSONSite.Payload
End Function

4Penny.net
We make companies more profitable. Serving clients nationally, our services include database, financial, ERP, CRM and Web-based solutions.

Call us for a free evaluation of your company's technology needs.

941-74P-enny x2 (941-747-3669)
Contact Us