web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Scaleable Solutions Blog / Connect Android App to Dyna...

Connect Android App to Dynamics CRM using Web API

Scaleable Solutions Profile Picture Scaleable Solutions 394

In our previous posts we have seen how you can connect Dynamics CRM with iOS and Cordova apps using Web API. Now we will learn how to  connect android app to Dynamics CRM Web API from android apps.

Application

The sample App will take URL, username and password of Dynamics CRM Online and returns the full name of the logged-in user using Dynamics CRM Web API. The final application will look like below:

Connect android app to Dynamics CRM     Authenticate android app to Dynamics CRM    Connect android app to Dynamics CRM

Note: The complete code of the application is hosted on Github.

Working

First of all the App must be registered with Azure Active Directory. If you are not familiar with it, please see our post: How to Register Dynamics CRM App with Azure Active Directory.

Next step is to provide Client_ID, Redirect_URL and Service_URL inside Constants.java.

public class Constants {
 static final String AUTHORITY_URL = "https://login.windows.net/common";
 static final String CLIENT_ID = "<Please provide your Client Id>";
 static final String REDIRECT_URL = "<Please provide your Redirect URL>";
 // Endpoint we are targeting for the deployed WebAPI service
 static final String SERVICE_URL = "<Please provide your Dynamics CRM URL>";
}

After providing Client_ID, Redirect_URL and Service_URL next step is to create Authentication Context and its CallBack as shown below:

private AuthenticationContext context;
private AuthenticationCallback<AuthenticationResult> callback = new AuthenticationCallback<AuthenticationResult>() {

    @Override
    public void onError(Exception exc) {
    }

    @Override
    public void onSuccess(AuthenticationResult result) {
    }
};

After making context and callback we will acquire Access Token using the code below:


context.acquireToken(MainActivity.this,
Constants.SERVICE_URL,
Constants.CLIENT_ID,
Constants.REDIRECT_URL, "", PromptBehavior.Auto, "", callback);

When authentication is done. OnSuccess is called along with Access Token. Now we can consume Web API from Dynamics CRM.

To get the UserId of the Logged-In user we will call WhoAmIRequest.

Below is a WhoAmIRequest 

URL LoginURL = new URL("https://OrgName.crm.dynamics.com/api/data/v8.0/WhoAmI");
HttpURLConnection rc = (HttpURLConnection) LoginURL.openConnection();

rc.setRequestMethod("GET");

rc.setRequestProperty("OData-MaxVersion", "4.0");
rc.setRequestProperty("OData-Version", "4.0");
rc.setRequestProperty("Accept", "application/json");
rc.setRequestProperty("Authorization", "Bearer " + token);

rc.connect();

InputStreamReader read = new InputStreamReader(rc.getInputStream());
StringBuilder sb = new StringBuilder();
int ch = read.read();
while (ch != -1) {
    sb.append((char) ch);
    ch = read.read();
}

response = sb.toString();
read.close();
rc.disconnect();

Now with UserId we can get fullname of the logged-In user.


URL LoginURL = new URL("https://OrgName.crm.dynamics.com/api/data/v8.0/systemusers("+ User_id +")?$select=fullname");
HttpURLConnection rc = (HttpURLConnection) LoginURL.openConnection();

rc.setRequestMethod("GET");

rc.setRequestProperty("OData-MaxVersion", "4.0");
rc.setRequestProperty("OData-Version", "4.0");
rc.setRequestProperty("Accept", "application/json");
rc.setRequestProperty("Authorization", "Bearer " + token);

rc.connect();

InputStreamReader read = new InputStreamReader(rc.getInputStream());
StringBuilder sb = new StringBuilder();
int ch = read.read();
while (ch != -1) {
    sb.append((char) ch);
    ch = read.read();
}

response = sb.toString();
read.close();
rc.disconnect();

Conclusion

That’s it .You have successfully connected you android app with Microsoft Dynamics CRM .The complete code of the application can be found on Github. In this way you can easily authenticate and connect android app to Dynamics CRM.

Comments

*This post is locked for comments