How to access User profile on SharePoint with Android

Hubfly
Hubfly
Aug 8, 2017 · 2 min read

While developing Android App for SharePoint, often we might need to access the User Profile information from SharePoint. For reasons like enabling login via Android App, get session information etc.

Let us take you through step by step with sample code on accessing User Profile information. The first thing is to get OAuth or FormDigest value. To do that,

Open Android Studio -> Create a New Project

Now you need to login on SharePoint using following URL update for the SharePoint login via Android. Here is a detailed article on how to Login to SharePoint via Android.

Once you have got “RTFA” and “Fed_Auth” access, that means you are logged in to SharePoint. Now, it is time to create a RestApi call.

To do that, you need to add the three libraries viz., httpclient, httpcore, httpmime to the Library folder

Create a new class to initialize. Find below the DefaultHttpClient code.

DefaultHttpClient mHttpClient = new DefaultHttpClient();

Then you need to initialize the Httppost and your posting URL is send through HttpPost. The sample code looks like the one below.

HttpPost httpPost = new HttpPost("your home page url"+"/_api/contextinfo");

Once you are done with that, add RTFA and FedAuth in your header file. Also, you need to add “Content_type” and “Accept” for which the code is given below.

mHttpPost.addHeader("Cookie", "rtFa=" your Rtfa + ") + "; FedAuth=" + your FedAuth));
mHttpPost.setHeader("Accept", "application/json;odata=verbose");
mHttpPost.setHeader("Content-type", "application/json;odata=verbose");

Now, initialize one more protocol namely, HttpResponse. The same has been executed in the code is below.

HttpResponse mHttpResponse = mHttpClient.execute(httpPost);

Now that your code is executed, you need to check on the response code using mHttpResponse for which the code is given below.

int postStatusCode =  mHttpResponse.getStatusLine().getStatusCode();

Once your statusCode returns 200, that means you got the FormDigestValue. Now, you need to get on response via HttpEntity and then need to read a data using BufferedReader and StringBuilder. The code that performs this is shown below.

HttpEntity httpEntity = mHttpResponse.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}

Now that the response is ready, you need to split up the response using StringBuilder object. The code snippet for this is as below.

JSONObject jObj = new JSONObject(json).getJSONObject("d");
JSONObject jsonObj2 = jObj.getJSONObject("GetContextWebInformation");
response = jsonObj2.getString("FormDigestValue");

Now you got the FormDigestValue. This one also has the OAuth token for SharePoint. This is how you gain access to User Profile on SharePoint via Android.

Have any questions? Let me know. I would be more than happy to help. See you in the comments.

hubfly

Hubfly is a Digital Workplace enabler aimed at enhancing workplace efficiency. Offering Intranet as a Service with Business Apps on top of @SharePoint

Hubfly

Written by

Hubfly

Hubfly provides Intranet as a service and a suite of business productivity apps for MS SharePoint. SharePoint is indeed great, but we have made it even better!

hubfly

hubfly

Hubfly is a Digital Workplace enabler aimed at enhancing workplace efficiency. Offering Intranet as a Service with Business Apps on top of @SharePoint

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade