Interoperability between Java and SharePoint 2013 on Premises

Introduction

One of the main possibilities that provider hosted apps in SharePoint 2013 disclose is the possibility of seamless integration between SharePoint and external systems implemented in non-Microsoft technologies.

In this post are explored the basics of the integration between SharePoint on premises and a Java web application. The RESTfull web service API of SharePoint leverages the communication with the Java web application. The SharePoint chrome control allows the Java web application to have the same styling of the hosted web site.

The TokenHelper.cs source file provides the token service authentication for provided hosted .Net applications. At the moment, it wasn’t found a Java implementation equivalent to the TokenHelper.cs for SharePoint on premises. Thus, it will be used NTLM authentication for the web service calls from the Java web application to SharePoint. This topic shall be revisited.

Find below an extract of the critical parts of the solution source code.

 

The Communication Layer with SharePoint

The Java web application reads and writes to a list of cars in the hosted web. The hosted web is called Interop1 and the list with data is called Cars. The list Cars has the columns: Brand, Model and Price.

The Java web application will be called Cars App.

The following are the dependencies of the Cars App in its Java implementation:

  • json-lib 2.4                              helper classes to manipulate Json objects
  • resteasy-jaxrs 2.2.1.GA                       RESTEasy was used to access the RESTfull SharePoint API
  • commons-httpclient 3.1                      dependency of RESTEasy
  • httpclient 4.1.1                                    dependency of RESTEasy
  • spring-web 2.5.6
  • spring-core 2.5.6
  • spring-webmvc 2.5.6
  • commons-logging 1.1.1

The payload class where the data about a car is stored could be as follows (file Car.java):

It has been defined the Car service in order to read and write into the Cars list. It has the declaration (file CarService.java):

The Cars service implementation has the member variables (file CarServiceImpl.java). These member variables define the authentication parameters, the URIs for the web service endpoints in SharePoint and the field names of the Cars list.

The Cars service implements the following helper methods.

The helper method callRestEasyService reads data by performing a HTTP GET against the RESTfull API of SharePoint. The URI provided in the req parameter defines exactly what is being retrieved; it follows the OData protocol syntax. In our case it would be the items of the list Cars.

The result is returned in JSON format, as stated in “application/json; odata=verbose”. Also, it would be possible to return the result as XML by setting the format as “application/atom+xml”.

In order to create objects in SharePoint through the RESTfull API (i.e., items in the Cars list), it’s needed to provide in the call a Form Digest.  The Form Digest can be obtained through a POST to the hosted web endpoint “/_api/contextinfo”. The helper addRestEasyRetrieveDigest performs this POST and reads the Form Digest. The exact URI is defined in the variable urlDigest, already declared above.

Note the methods callRestEasyService and addRestEasyRetrieveDigest are practically the same, just the first does GET and the second POST.

The helper addRestEasyPost allows the creation of objects in SharePoint through POSTs as stated in the OData protocol. This method is used to add cars to the list Cars.

The parameter req has the URI where to create the object. Here, it would be the URI of the Cars list.

Another detail regarding objects creation, it’s the need to state its type. In the Cars list, the list items are of type SP.Data.CarsListItem. The type naming convention is as follows:

SP.Data.<ListName>ListItem

In case of doubts, it’s possible to confirm the object type by performing a HTTP GET for the list items and checking their type.

The helper method parseJsonDigestValue parses the JSON response from the endpoint “/_api/contextinfo” retrieving the form digest.

The following helper, parseJson, translates the JSON response to the Car payload class, returning a list of cars.

To read items, the Cars service defines the method getCars. The implementation is as follows:

To write items, the Cars service defines the method insertCar. The implementation uses the helper method as follows:

 

User Experience (UX) for Apps in SharePoint 2013

The App user experience can be designed on top of the three possibilities within SharePoint:

  1.  Full page user experience
  2. App Part user experience
  3. Custom Actions

The full page UX means the App will have the whole browser page for its user interface. In the full page UX, the App can make use of the SharePoint chrome control to render the same styling as of the hosted web and, optionally, render a header like SharePoint pages do.

In the App Part UX the App surfaces its UI on a page in the hosted web. A special kind of web part known as App Part takes care of embedding the App’s page into the hosted page. The App Part accomplished this by rendering an iFrame where the source is the App’s page. The App page can have the same styling of the hosted web by importing the hosted site’s CSS files through JavaScript.

Custom Actions allow the placement of links to App’s pages in the Ribbon or in the Edi Control Block of the hosted web. This option has not been explored in this post.

 

Implementing the User Interface

The code below implements the App page named Car.jsp; it displays the list of cars. The page can be rendered as in the full page UX or in the App Part UX. The URL parameter inAppPart defines which way the page will be rendered (inAppPart=true, then as in the App Part UX; if none or false, then as in the full page UX).

First add a few declarations to the JSP page (file Car.jsp):

The code in the following boxes should be added to the HEAD element of the page.

Add the references to the libraries in the JSP page (file Car.jsp):

Initialise the JavaScript block and a few variables from the URL query string:

The following script loads the CSS or the chrome control from SharePoint into the page:

The following piece of HTML defines the BODY of the page. The elements which should be seen just in the full page user experience are marked with the CSS class displayJustInFullPage. This allows the JavaScript part to hide or display them as seen above.

The declarations above should be applied on every page that should look like the hosted web.

 

The Provider Hosted App

A SharePoint provider hosted app should be created following the instructions:

How to: Create high-trust apps for SharePoint 2013 using the server-to-server protocol (advanced topic)

Once created, edit the AppManifest.xml, setting the URL to a page in your Java web application. That could be the entry page of your application for the full page user experience:

The App Part

For the App Part user experience, first add an App Part (known as well as Client Web Part) to your SharePoint project. After that, in the Elements.xml of the App Part, set the URL to the Java web application:

It is important to set there “inAppPart=true” to ensure the App Part user experience.

For instructions about how to create an App Part with Visual Studio 2012, refer to:

How to: Create app parts to deploy with apps for SharePoint

Conclusion

Provider hosted apps unleash great possibilities of integration between SharePoint and systems developed in other platforms as Java. Here, we have seen how to perform basic read/write operations on SharePoint lists. Also, it was shown how to integrate the App at user interface level with the full page and App Part user experiences.

SharePoint

The SharePoint list Cars; data is read and written into it by the Java web application.

SharePoint

In the full page user experience, the Java web application displaying the cars with the same styling as SharePoint.

From the menu item “Add a Car” it is possible to reach the page from where new cars can be added.

SharePoint

 The form to add new cars into the SharePoint list.

SharePoint

Could you tell which list in from the Java web application and which is from SharePoint?

On the left, the App Part displaying the Java web application and on the right the SharePoint list.

 

A point of improvement is that App Parts do not resize automatically to fit the Apps content. If the content grows beyond the App Part area in the hosted page, scroll bars are rendered. However, the App page can control the resizing of the App Part avoiding the scroll bars. This is explained in this post:

Resizing App Parts with PostMessage in SharePoint 2013

Never miss an update by following us and subscribing to our monthly newsletter!

Further References

 

Code Download

Authors: Leandro Bernsmüller and Angel Torralba

 

Summary
Interoperability between Java and SharePoint 2013 on Premises
Article Name
Interoperability between Java and SharePoint 2013 on Premises
Description
One of the possibilities that provider hosted apps in SharePoint 2013 disclose is the possibility of integration between SharePoint and external systems.
Author
Publisher Name
Atos Consulting CH
Publisher Logo

4 thoughts on “Interoperability between Java and SharePoint 2013 on Premises

  1. Avatar
    Alexandre Menezes Reply

    Hi Don,
    We just added a link to the code in the end of the article.

    Cheers,
    Alex

  2. Avatar
    fadi Reply

    Hey,

    i have downloaded the code but unfortunately i am unable to authenticate it using NTLM. Am i missing something?

  3. Avatar
    fadi Reply

    i am receiving the followingerror.
    org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
    INFO: ntlm authentication scheme selected
    org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge
    INFO: Failure authenticating with NTLM @i-XXXXXXXXXXX.com

Leave a Reply

Your email address will not be published. Required fields are marked *