February 29, 2012

Connecting Microsoft Dynamics GP10 web services - using Java

import java.net.URL;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.Properties;
import org.apache.axis.SimpleTargetedChain;
import org.apache.axis.client.AxisClient;
import org.apache.axis.configuration.SimpleProvider;
import org.apache.axis.transport.http.CommonsHTTPSender;
import org.apache.log4j.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import com.microsoft.schemas.dynamics._2006._01.CompanyKey;
import com.microsoft.schemas.dynamics._2006._01.CurrencyType;
import com.microsoft.schemas.dynamics._2006._01.OrganizationKey;
import com.microsoft.schemas.dynamics.gp._2006._01.CurrencyKey;
import com.microsoft.schemas.dynamics.gp._2006._01.Dynamics_x0020_GPLocator;
import com.microsoft.schemas.dynamics.gp._2006._01.Dynamics_x0020_GPSoap;
import com.microsoft.schemas.dynamics.gp._2006._01.GetInventoriedItemByKey;
import com.microsoft.schemas.dynamics.gp._2006._01.Item;
import com.microsoft.schemas.dynamics.gp._2006._01.ItemCriteria;
import com.microsoft.schemas.dynamics.gp._2006._01.ItemKey;
import com.microsoft.schemas.dynamics.gp._2006._01.ItemSummary;
import com.microsoft.schemas.dynamics.gp._2006._01.LikeRestrictionOfString;
import com.microsoft.schemas.dynamics.gp._2006._01.Service;


import org.apache.axis.client.Stub;

public class GPConnect {

public static void main(String args[]) {

String endpoint = "http://xxxx:PORT/DynamicsGPWebServices/DynamicsGPService.asmx";
String username="domain\\xxxx";
String password = "xxxxx";
CompanyKey companyKey = null;
String currencyType = "Transactional";
String cultureName = "en-US";
String portNumber = "";
String roleKey = null;

Dynamics_x0020_GPSoap stub;
com.microsoft.schemas.dynamics.gp._2006._01.Dynamics_x0020_GPLocator locator = new Dynamics_x0020_GPLocator();
locator.setDynamics_x0020_GPSoap12EndpointAddress(endpoint);
com.microsoft.schemas.dynamics._2006._01.Context context= new com.microsoft.schemas.dynamics._2006._01.Context();
try {
stub = locator.getDynamics_x0020_GPSoap12();
((Stub)stub).setUsername(username);
((Stub)stub).setPassword(password);
Hashtable headers = new Hashtable();
headers.put("chunked", "false");
headers.put("Connection","keep-alive");
((Stub)stub)._setProperty("HTTP-Request-Headers", headers);
System.out.println("StubCreated");

ItemCriteria criteria= new ItemCriteria();
LikeRestrictionOfString itemClassId= new LikeRestrictionOfString();
itemClassId.setEqualValue("CATALOG");
criteria.setItemClassId(itemClassId)

context.setCultureName("en-US");
CurrencyType cuType = new CurrencyType("Transactional");
context.setCurrencyType(cuType);
companyKey = new CompanyKey();
companyKey.setId(-1);

OrganizationKey orgkey=(OrganizationKey)companyKey;
context.setOrganizationKey(orgkey);
ItemKey key = new ItemKey("128 SDRAM");
Item o = stub.getItemByKey(key , context);
System.out.println(o.getDescription());

} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printmessage()
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printmessage()
}
}
}




NOTE : Using version AXIS 1.4 java client created and download the GP10.jar from the below link

"https://docs.google.com/open?id=0Byft-gwJbzuocFV6eUtxRExTY1NWUHQtZHQ2Tl9Sdw"