HOW TO CONSUME EXTERNAL WEBSERVICES
IN MICROSOFT DYNAMICS AX
Firstly add a service reference and then use that service reference in the below code to consume the web services.
// This job uses an external service to run a Bing search
// The search string is “Dynamics AX”
static void CallBingService(Args _args)
{
// Replace the string [YOUR_BING_APPID] with your Application ID.
#define.AppId([YOUR_BING_APPID])
// This is the variable for the service client.
ClrObject clientType;
// This is the variable for the service client type.
BingSearch.ServiceReferences.BingV2ServiceReference.BingPortTypeClient _client;
// These are the variables for web query objects.
BingSearch.ServiceReferences.BingV2ServiceReference.SearchRequest request;
BingSearch.ServiceReferences.BingV2ServiceReference.SourceType[] sourceTypes;
BingSearch.ServiceReferences.BingV2ServiceReference.SearchResponse response;
BingSearch.ServiceReferences.BingV2ServiceReference.WebResponse webResponse;
BingSearch.ServiceReferences.BingV2ServiceReference.WebResult[] webResults;
BingSearch.ServiceReferences.BingV2ServiceReference.WebResult webResult;
int integer;
str string;
System.Exception ex;
new InteropPermission(InteropKind::ClrInterop).assert();
// Use a try/catch block to catch errors immediately as CLR exceptions in the job.
try
{
//Construct and configure the service client by retrieving the X++ type for the service and using the AifUtil class/
// Retrieve the X++ type for the Bing service client object.
clientType = CLRInterop::getType("BingSearch.ServiceReferences.BingV2ServiceReference.BingPortTypeClient");
// Use the AifUtil class to create an instance of the service client object.
_client = AifUtil::CreateServiceClient(clientType);
// Create the search request.
request = new BingSearch.ServiceReferences.BingV2ServiceReference.SearchRequest();
request.set_AppId(#AppId);
// Note the search request string is “Dynamics AX”
request.set_Query("Dynamics AX");
sourceTypes = new BingSearch.ServiceReferences.BingV2ServiceReference.SourceType[1]();
sourceTypes.SetValue(BingSearch.ServiceReferences.BingV2ServiceReference.SourceType::Web, 0);
request.set_Sources(sourceTypes);
// Configure the response for output.
response = _client.Search(request);
webResponse = response.get_Web();
// Get the search results.
webResults = webResponse.get_Results();
webResult = webResults.GetValue(0);
// Display the first result in the InfoLog.
integer = webResponse.get_Total();
info(strFmt("%1 total web results.", integer));
integer = webResults.get_Count();
info(strFmt("%1 results in response.", integer));
info("");
info("First result:");
string = webResult.get_Title();
info(strFmt("Title: %1", string));
string = webResult.get_Description();
info(strFmt("Description: %1", string));
string = webResult.get_Url();
info(strFmt("Url: %1", string));
}
// Catch any exceptions.
catch(Exception::CLRError)
{
// Handle the exception and display message in the InfoLog.
ex = CLRInterop::getLastException();
info(ex.ToString());
}
}