header
DavidM 4/11/2015 8:10:22 AM

GET a List of Sites

This article contains sample code for getting a Generic List of Sites (Locations/Warehouses) 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.  

Then add the JSONSiteList class:

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

 

 Then add the main code 

''' <summary>
''' Returns a Generic List(of Site)
''' </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> Generic List(of Site)</returns>
''' <remarks></remarks>
Function GetSites(strUserName As String, strPassword As String, strMachineName As String) As List(Of 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 List of Dynamics Site objects
    Dim oJSONSiteList As JSONSiteList = 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", 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
        oJSONSiteList = js.Deserialize(Of JSONSiteList)(strResponse)
        'this is an example of how the data is used
        Dim strLocationCode As String = oJSONSiteList.Payload(0).LocationCode
 
        'this is method #2, we'll read the response into a dictionary object
        'this section of code is not used, it's here as an example only
        '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 aPayload As System.Collections.ArrayList = dicResponse("Payload")
        'this is an example of how the data is used.
        For i As Int16 = 0 To aPayload.Count - 1
            Dim dicPayload As Dictionary(Of String, Object) = aPayload(i)
            Dim strLocationCode2 As String = dicPayload("LocationCode")
        Next
 
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
 
    'return a List (of Microsoft.Dynamics.GP.BusinessObjects.Inventory.Site) object
    Return oJSONSiteList.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