Showing posts with label AX web service. Show all posts
Showing posts with label AX web service. Show all posts

Tuesday, 8 November 2016

Query Service AIF AX2012


 AIF query service can be used to fetch ad hoc data from AX2012 without creating a new service.
Its a common service which can be used to retrieve the data from AX database. There are three types of queries that you can run through a query service but I am discussing only the first two options here:


In the below code I will demonstrate the use of query service for fetching all the records from the SalesTable through the user defined query. In order to run this snippet, please make sure that AIF services are deployed and you are able to access the query service.  URI for the query service WSDL is:

http://<AOS_HOST>:8101/DynamicsAx/Services/QueryService


C# code for fetching the records

QueryServiceClient client = new QueryServiceClient();
QueryMetadata queryMetaData = new QueryMetadata();
queryMetaData.DataSources = new QueryDataSourceMetadata[] {
new QueryDataSourceMetadata() { DynamicFieldList=true, Table=”StudentTable”,  Name=”StudentTable”,  Enabled=true}
};

Paging paging = null;

DataSet ds = client.ExecuteQuery(queryMetaData, ref paging);

foreach (DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr[“PersonnelNumber”].ToString());
}

Console.ReadLine();


Tuesday, 18 October 2016

C# Code to test AX document service.

In my previous we created a document service for a custom entity with postal address. In this post I will demonstrate the process to test the same service using visual studio.

Step1: Go-to Inbound port and copy the wsdl url.
 Step2: Go-to the visual studio File --> New --> Project -->Console application


 Step3: Right click  References node and Add new service reference ()





Step4: Paste the below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestStudentService.StudentServiceReference;//Added namespace o access service in below application

namespace TestStudentService
{
   class Program
{
static void Main(string[] args)
{
AxStudentServiceClient proxy = new AxStudentServiceClient();
// Create an instance of the document class.

AxdAxStudent axdStudent = new AxdAxStudent();
//Create instances of the entities that are used in the service and

// set the needed fields on those entities.
AxdEntity_StudentTable studentTable = new AxdEntity_StudentTable();
studentTable.RollNumber = 28;
studentTable.Name = "Ramesh Kumar";
studentTable.Standard = "Tenth";
//Create a postal address
AxdEntity_DirPartyPostalAddressView address = new AxdEntity_DirPartyPostalAddressView();
address.Street = "2122 NE 5th St Parent ";
address.City = "Beverly Hills";
address.State = "";
address.CountryRegionId = "USA";
address.ZipCode = "90210";
address.Roles = "Home";
AxdEntity_DirPartyContactInfoView electronic = new AxdEntity_DirPartyContactInfoView();
electronic.Locator = "Ajay@upwork.com";
electronic.Type = AxdEnum_LogisticsElectronicAddressMethodType.Email;
electronic.TypeSpecified = true;
electronic.Roles = "Home";
//Add the addresses to the party record and the party to the table record

AxdEntity_DirPartyTable_DirPartyTable dirPartyTable = new AxdEntity_DirPartyTable_DirPartyTable();
dirPartyTable.Name = "Ramesh Kumar";
dirPartyTable.DirPartyPostalAddressView = new AxdEntity_DirPartyPostalAddressView[1] { address };
dirPartyTable.DirPartyContactInfoView = new AxdEntity_DirPartyContactInfoView[1] { electronic };
studentTable.DirPartyTable = new AxdEntity_DirPartyTable_DirPartyTable[1] { dirPartyTable };
axdStudent.StudentTable = new AxdEntity_StudentTable[1] { studentTable };
try

{
// Call the create method on the service passing in the document.
CallContext context = new CallContext();
context.Company = "EAM";
context.Language = "en-au";
EntityKey[] returnFloc = proxy.create(context, axdStudent);
// The create method returns an EntityKey which contains the ID of the sales order.

EntityKey returnedFloc = (EntityKey)returnFloc.GetValue(0);
Console.WriteLine("the student has been created of " + returnedFloc.KeyData[0].Value);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}    
}
}
}
Step5: Check the record has been created with added information.

In my next post I will demonstrate how to use the update operation on AIF service