The following is a set of code samples written in VB.Net, the samples demonstrates the process of making a number of calls to the XData API.
Contacts List Sample:
'Declare Required objects and Variables
Dim sURI as string = "http://yourhost/XData/ACT/Contacts/"
Dim oRequest As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(sURI), System.Net.HttpWebRequest)
Dim oResponse As System.Net.WebResponse = Nothing
Dim sKey as string = "XXXXXXX"
Dim sMessage as string = ""
'Initialize the Request Parameter and set the Key Header
oRequest.Method = "GET"
oRequest.Headers.Add("key", System.Web.HttpUtility.UrlEncode(sKey))
Try
'Submit the Request and get the Response
oResponse = oRequest.GetResponse
'Use a Stream Reader to store the Response Stream and then read the contents of the stream to a string
Dim oReader As System.IO.StreamReader = New System.IO.StreamReader(oResponse.GetResponseStream)
sMessage= oReader.ReadToEnd
'Decode the Message if needed
sMessage = System.Web.HttpUtility.UrlDecode(sMessage)
'The Result of the message should be an xml string if the call was successful
'otherwise there will be a short error message instead.
Catch ex as Exception
'Deal with any errors here
End Try |
Filtered Contacts List:
'Declare Required objects and Variables
Dim sURI as string = "http://yourhost/XData/ACT/Contacts/"
Dim sLookupField as string = "Country"
Dim sLookupValue as string = "New Zealand"
Dim oRequest As System.Net.HttpWebRequest = Nothing
Dim oResponse As System.Net.WebResponse = Nothing
Dim sKey as string = "XXXXXXX"
Dim sMessage as string = ""
'Initialize the Request Parameter and set the Key Header
sURI += "?" & "LookupField=" & sLookupField & "&LookupValue=" & sLookupValue
oRequest = CType(System.Net.WebRequest.Create(sURI), System.Net.HttpWebRequest)
oRequest.Method = "GET"
oRequest.Headers.Add("key", System.Web.HttpUtility.UrlEncode(sKey))
Try
'Submit the Request and get the Response
oResponse = oRequest.GetResponse
'Use a Stream Reader to store the Response Stream and then read the contents of the stream to a string
Dim oReader As System.IO.StreamReader = New System.IO.StreamReader(oResponse.GetResponseStream)
sMessage= oReader.ReadToEnd
'Decode the Message if needed
sMessage = System.Web.HttpUtility.UrlDecode(sMessage)
'The Result of the message should be an xml string if the call was successful
'otherwise there will be a short error message instead.
Catch ex as Exception
'Deal with any errors here
End Try |
Contact Details Sample:
'Declare Required objects and Variables
Dim sURI as string = "http://yourhost/XData/ACT/Contacts/ContactDetails.aspx"
Dim sContactID as string = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Dim oRequest As System.Net.HttpWebRequest = Nothing
Dim oResponse As System.Net.WebResponse = Nothing
Dim sKey as string = "XXXXXXX"
Dim sMessage as string = ""
'Initialize the Request Parameter and set the Key Header
sURI += "?" & "ID=" & sContactID
oRequest = CType(System.Net.WebRequest.Create(sURI), System.Net.HttpWebRequest)
oRequest.Method = "GET"
oRequest.Headers.Add("key", System.Web.HttpUtility.UrlEncode(sKey))
Try
'Submit the Request and get the Response
oResponse = oRequest.GetResponse
'Use a Stream Reader to store the Response Stream and then read the contents of the stream to a string
Dim oReader As System.IO.StreamReader = New System.IO.StreamReader(oResponse.GetResponseStream)
sMessage= oReader.ReadToEnd
'Decode the Message if needed
sMessage = System.Web.HttpUtility.UrlDecode(sMessage)
'The Result of the message should be an xml string if the call was successful
'otherwise there will be a short error message instead.
Catch ex as Exception
'Deal with any errors here
End Try |
Create Contact Sample:
'Declare Required objects and Variables
Dim sURI as string = "http://yourhost/XData/ACT/Contacts/CreateContact.aspx"
Dim sXML as string = ""
Dim oRequest As System.Net.HttpWebRequest = Nothing
Dim oResponse As System.Net.WebResponse = Nothing
Dim sKey as string = "XXXXXXX"
Dim sMessage as string = ""
'Initialize the Request Parameter and set the Key Header
sXML="<?xml version=" & chr(34) & "1.0" & chr(34) & "?>"
sXML+="<XDataRequest>"
sXML+="<Payload>"
sXML+="<Field>"
sXML+="<Name>" & "Contact" & "</Name>"
sXML+="<Value>" & "John Smith" & "</Value>"
sXML+="</Field>"
sXML+="</Payload>"
sXML+="</XDataRequest>"
oRequest = CType(System.Net.WebRequest.Create(sURI), System.Net.HttpWebRequest)
oRequest.Method = "POST"
oRequest.ContentType = "text/xml"
oRequest.Headers.Add("key", System.Web.HttpUtility.UrlEncode(sKey))
Dim arBytes() As Byte
arBytes = System.Text.Encoding.UTF8.GetBytes(sXML)
oRequest.ContentLength = arBytes.Length
Dim oStream As System.IO.Stream = oRequest.GetRequestStream
oStream.Write(arBytes, 0, arBytes.Length)
oStream.Close()
Try
'Submit the Request and get the Response
oResponse = oRequest.GetResponse
'Use a Stream Reader to store the Response Stream and then read the contents of the stream to a string
Dim oReader As System.IO.StreamReader = New System.IO.StreamReader(oResponse.GetResponseStream)
sMessage= oReader.ReadToEnd
'Decode the Message if needed
sMessage = System.Web.HttpUtility.UrlDecode(sMessage)
'The Result of the message should be an xml string if the call was successful
'otherwise there will be a short error message instead.
Catch ex as Exception
'Deal with any errors here
End Try |
Update Contact Sample:
'Declare Required objects and Variables
Dim sURI as string = "http://yourhost/XData/ACT/Contacts/UpdateContact.aspx"
Dim sContactID as string = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Dim sXML as string = ""
Dim oRequest As System.Net.HttpWebRequest = Nothing
Dim oResponse As System.Net.WebResponse = Nothing
Dim sKey as string = "XXXXXXX"
Dim sMessage as string = ""
'Initialize the Request Parameter and set the Key Header
sURI += "?" & "ID=" & sContactID
sXML="<?xml version=" & chr(34) & "1.0" & chr(34) & "?>"
sXML+="<XDataRequest>"
sXML+="<Payload>"
sXML+="<Field>"
sXML+="<Name>" & "Company" & "</Name>"
sXML+="<Value>" & "Acme Research Inc" & "</Value>"
sXML+="</Field>"
sXML+="</Payload>"
sXML+="</XDataRequest>"
oRequest = CType(System.Net.WebRequest.Create(sURI), System.Net.HttpWebRequest)
oRequest.Method = "POST"
oRequest.ContentType = "text/xml"
Dim arBytes() As Byte
arBytes = System.Text.Encoding.UTF8.GetBytes(sXML)
oRequest.ContentLength = arBytes.Length
Dim oStream As System.IO.Stream = oRequest.GetRequestStream
oStream.Write(arBytes, 0, arBytes.Length)
oStream.Close()
Try
'Submit the Request and get the Response
oResponse = oRequest.GetResponse
'Use a Stream Reader to store the Response Stream and then read the contents of the stream to a string
Dim oReader As System.IO.StreamReader = New System.IO.StreamReader(oResponse.GetResponseStream)
sMessage= oReader.ReadToEnd
'Decode the Message if needed
sMessage = System.Web.HttpUtility.UrlDecode(sMessage)
'The Result of the message should be an xml string if the call was successful
'otherwise there will be a short error message instead.
Catch ex as Exception
'Deal with any errors here
End Try |