Search This Blog

Thursday 3 January 2013

AIF Framework In Dynamics AX 2012 Part 2

In the Last Session i,e in the Part 1 I have guide you on how to configure the AIF Service.Now in this session I will guide you on how to Consume the AIF service.

Below are some steps to consume the AIF service:

Step 1:

 First of all I will create the Console Application in dot net c#.And than add the refrence of our AIF service that we have created in the part 1.

After creating your project just go to the solution explorer and than right click service refrences and than click add service refrence as you can see in the below image.









Now when you click the add service refrence a new wizard will open on which you have to past the WSDL URL that we have got during the making of the inbound port.

You can see the screen shot of that port in the below image.














 
 
 
 


Now press the ok button.IT will make the refrence of the service.
 
Step 2:
 
 Now in the next step i will show you the code snippet.

First of all we have to add the name spaces i.e

using project.servicename;

in my case it is:

using zahidemp.zahidempServiceRefrence1;

using System.Diagnostics;

Now i have to define the company name in which i am going to perform the insert update delete operation.

Callcontext context = new CallContext { Company = "dat"};

EntityKey[] keys = null;

In will tell you inside the code that why i have use that EntityKey.

Now I am going to show you the code snippet for the adding the data in to the ax table.

Axdgetemp1 empinfo = new Axdgetemp1();


/**Creating the Data ***/
 
Console.WriteLine("Inserting the record");

List<AxdEntity_employee_1> emplist=new List<AxdEntity_employee_1>();

AxdEntity_employee_1 zahid=new AxdEntity_employee_1();

zahid.EmpNo=1;

zahid.EmpNoSpecified =
true;

zahid.Dob="28-5-1989";

zahid.Empname = "Muhammad Zahid";

AxdEntity_employee_1 asim=new AxdEntity_employee_1();

asim.EmpNo = 2;

asim.EmpNoSpecified =
true;

asim.Empname ="Muhammad Asim";

asim.Dob="19-5-1955";

emplist.Add(zahid);

emplist.Add(asim);

               

empinfo.employee_1 = emplist.ToArray();


keys = client.create(context,empinfo);

Console.WriteLine("Records inserted successfully");
Console.ReadKey();


What I did in the above code is.I have just made the object of my query i,e empinfo and than
 and than made the list in which i will add the employee names and than make the object of one employee i.e zahid and the object of another employee i,e Asim and than i have assign the values in these objects and than added them into the list and than added that list to the object of Query and than we have call the create method of our proxy class that inserted that data.

Now when the data is inserted successfully it will return the Recid of the record which we are going to save it into the keys object that i have created earlier in the code which I told that I will inform you inside the code.

Now the data is inserted successfuly I am going to show you the screen shot of the record inside the table that i have inserted.


 Step 3:

Now I will show you the demo about how can we comsume the service in update operation.

below is the code snippet for the update operation.

Console.WriteLine("Updating the record"); 
List<CriteriaElement> criterialist = new List<CriteriaElement>();

CriteriaElement criteria = new CriteriaElement();

criteria.DataSourceName = "employee_1"; 
criteria.FieldName = "EmpNo"; 
criteria.Operator = Operator.Equal; 
criteria.Value1 = "1"; 
criterialist.Add(criteria);QueryCriteria querycriteria = new QueryCriteria();

querycriteria.CriteriaElement = criterialist.ToArray();

keys = client.findKeys(context,querycriteria);

empinfo = client.read(context, keys);

zahid = empinfo.employee_1[0];

zahid.Empname = "Zahid";
 

List<AxdEntity_empdepartment_1> departmentlist = new List<AxdEntity_empdepartment_1>();


AxdEntity_empdepartment_1 empdepartment =new AxdEntity_empdepartment_1();

empdepartment.Empdepartment = "Dynamics Core";
empdepartment.Experience = 1;

departmentlist.Add(empdepartment);

zahid.empdepartment_1 = departmentlist.ToArray();

client.update(context, keys, empinfo)
Console.WriteLine("Data Updated successfully");

Console.ReadKey();

In the above code i have just made the criteria on bahalf of which i am going to get the record and than updating that record and than there is another table empdepartment related to the table emp.I am also inserting the record in that table.

Note:There is the relation between emp table and emp department table.

Step 4:

Now I will show you the demo on how can we consume the service in delete operation.

//for delete

criterialist =new List<CriteriaElement>();

criteria =new CriteriaElement();
criteria.DataSourceName = "employee_1";

criteria.FieldName = "EmpNo";

criteria.Operator =Operator.Equal;
criteria.Value1 = "1";

criterialist.Add(criteria);

querycriteria =
new QueryCriteria();

querycriteria.CriteriaElement = criterialist.ToArray();

keys = client.findKeys(context, querycriteria);
Debug.Assert(keys.Length == 1);

client.delete(context, keys);

Now I will show you the demo on how can we consume the service in delete operations.In the above code I have just created the criteris as I did earlier in the update operation and than deleted the record based on that criteria.

Now that was all in the AIF Framework part 2.I have shown all the examples of Insert,Update and Delete Operations through AIF Service.

There is the link below if you want  to go the AIF Framework Part 1.

AIF Framework Part 1

Thanks

Muhammad Zahid.


No comments:

Post a Comment