I want to integrate Android application with Dynamics CRM 2015 Online and On-Premise.
For online version Connect Android App to Dynamics CRM using Web API this works fine, But ADAL dependency is not supported for OnPremise.
Are there any resources which show the basic steps to access the Microsoft CRM on-premise.
Any sample code around same for connecting to REST endpoint will be helpful.
3 Answers
Setup IFD for your on-premise deployment.
Authenticate to Microsoft Dynamics 365 with the Web API
When you use the Web API for Dynamics 365 (online) or an on-premises Internet-facing deployment (IFD) you must use OAuth as described in Connect to Microsoft Dynamics 365 web services using OAuth.
Connect to Microsoft Dynamics 365 web services using OAuth
Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online
The recommended authentication API for use with the Dynamics 365 Web API is Azure Active Directory Authentication Library (ADAL), which is available for a wide variety of platforms and programming languages. The ADAL API manages OAuth 2.0 authentication with the Dynamics 365 web service identity provider. For more details on the actual OAuth protocol used, see Use OAuth to Authenticate with the CRM Service.
ADFS is required for on prem. Check this documentation for how to set up the servers. https://msdn.microsoft.com/en-us/library/dn531009(v=crm.7).aspx
If this is a one off project for a client it may also be worth creating a .Net based proxy between the app and CRM. That way you can use the .Net SDKs without worrying about losing support
On-Prem using ADAL:
public static String GetAdfs(String url) throws IOException,
ParserConfigurationException, SAXException {
URL WsdlURL = new URL(url
+ "/XrmServices/2011/Organization.svc?wsdl=wsdl0");
HttpURLConnection rc = (HttpURLConnection) WsdlURL.openConnection();
rc.setRequestMethod("GET");
rc.setDoOutput(true);
InputStreamReader read = new InputStreamReader(rc.getInputStream());
StringBuilder sb = new StringBuilder();
int ch = read.read();
while (ch != -1) {
sb.append((char) ch);
ch = read.read();
}
String response = sb.toString();
read.close();
rc.disconnect();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document x = builder
.parse(new ByteArrayInputStream(response.getBytes()));
NodeList nodes = x.getElementsByTagName("ms-xrm:Identifier");
if (nodes.getLength() == 0)
return null;
return nodes.item(0).getFirstChild().getTextContent()
.replace("http://", "https://");
}
// ADAL init
AuthenticationContext authenticationContext = new AuthenticationContext(LoginActivity.this, GetAdfs(url), false);
authenticationContext.acquireToken(context, domain, Constants.CLIENT_ID, Constants.REDIRECT_URL, "", PromptBehavior.Auto, "", callback);
private AuthenticationCallback<AuthenticationResult> callback = new AuthenticationCallback<AuthenticationResult>() {
@Override
public void onError(Exception exc) {
ViewHelper.showToast(context, "Domain name or user not available in ms crm");
}
@Override
public void onSuccess(AuthenticationResult result) {
if (result == null || result.getAccessToken() == null || result.getAccessToken().isEmpty()) {
Toast.makeText(context, "Token is Empty", Toast.LENGTH_SHORT).show();
} else {
Log.i(Keys.TOKEN_KEY, result.getAccessToken());
}
}
};