Codementor Events

Using Java to get appointments and business documents into SharePoint

Published Jun 08, 2020Last updated Aug 19, 2020
Using Java to get appointments and business documents into SharePoint

I wanted to save some documents in Microsoft SharePoint automatically. I assumed it would not be that difficult. After all, SharePoint has been around for quite some time now. There must be a Java API I can use, I thought. Or maybe some exposed web services that I could consume, right?

How do I access SharePoint from Java?

Well, it is not as simple to integrate with SharePoint using Java as I thought. The good news is that, although the road might not be as straightforward as I wished, I found that you can avoid many obstacles if you take the right turns along the way.
In the end, I was able to perform all CRUD operations I wanted on SharePoint files, libraries, and folders from my Java code. But let's start at the beginning.

Does SharePoint have an API?

For Java developers, it is possible to access SharePoint resources using REST. For this, you need to construct a RESTful HTTP request using the OData standard, which corresponds to the desired client object model API.
You don't get the functionality and ease of use that client object models provide. They remain the primary development option for communicating with SharePoint sites when you are using .NET, Silverlight, or JavaScript. But REST is what you are left with if you are using Java, so let's check out what you can do with it.

SharePoint REST service - the good, the bad and the ugly

The good thing about using SharePoint REST API is that you can interact with SharePoint 2013 and above (2016/2019/Online/Office 365) and perform Create, Read, Update, and Delete (CRUD) operations.
The bad thing is that you have to map out these operations into GET, POST, PUT, MERGE, and PATCH methods. I personally feel it is fine once you have some experience, but when you are getting started, it can get absolutely confusing, especially in terms of the difference between POST, PUT, and PATCH.
Things get worse in terms of maintenance. When the API changes (and you know it IS going to change…), you will have to get back to your code and fix things that were working fine. I hate it when I have to do that. Not that I don't love my code 😊 It is just that it feels like someone else broke it and I have to fix it.

Is there an alternative to the API?

If you don't want to:
• Work with Microsoft in the first place
• Spend tons of time to study the documentation
• Keep up with future changes of the API and spend time fixing the problems you get as a result

You might be questioning yourself if there is another way around it. Yes, there is a better way forward!

You can use Connect Bridge instead of using the API directly. Connect Bridge is a thin layer you can put between the API and your code. It is thin enough to be efficient and fast, but thick enough to ensure you never have to touch Microsoft SharePoint or the API.

Connect Bridge is a paid tool. Still, a free trial is available so you can see if it works for you.

Login.PNG

The best thing about this tool is that it ensures forward and backward compatibility.

You build your Java code using Connect Bridge, and then your users can upgrade SharePoint, Microsoft can launch new versions of the API… and you never have to worry about it. Your software will still work! You write your code once, and all that complexity is something you can forget about.

In terms of deployment, there is no limitation. You can run Connect Bridge:
• On-premises or self-hosted (on your own virtual machines or cloud servers)
• In an Azure-built SaaS platform managed by Connecting Software

How does Connect Bridge work? What is it doing behind the scenes? It translates the standard SQL statements you write in your code into API calls. On your side, it looks like you are using JDBC to get data to and from a relational database like you normally would (ODBC and Web services are also supported if you need them for similar projects).

You write your regular SELECT, UPDATE, INSERT, and DELETE statements. You use what looks like stored procedures. But it is all merely a layer of abstraction. It is NOT Microsoft SharePoint's database you are accessing (and trust me, that is something you don't want to do!).

Getting started with Connect Bridge

Assuming the Exchange and SharePoint instances already exists and that you have your Java environment ready, these are simple steps you need to follow:

  1. Make sure you have your target system login credentials at hand (in this case, Exchange and SharePoint)
  2. Request a free trial and install Connect Bridge
  3. Run Connect Bridge Management Studio and:
    o add an account for SharePoint (Accounts – Add account). For adding the account, you should select the connector CBSharePointConnector and use the credentials mentioned on point 1.
    o add an account for Exchange (Accounts – Add account). For adding the account, you should select the connector MGEXPlugin2010 and use the credentials mentioned on point 1.
    For each account, make sure you test the connection, to make sure everything is OK with the credentials and parameters you used.
    test_connection_sharepoint.PNG
  4. Open the New Query option and then the Connection Browser. Find the Exchange Connector and expand it until you see the DefaultConnection. Right click the DefaultConnection and choose Get Connection string. Copy the ODBC connection string, as you will need it to pass it on to the script.
    The Query tool you just used in point 4 is where you can test your (fake) queries, see the (fake) tables and their (fake) columns or even find a convenient (fake) stored procedure. As I mentioned, it is as if you were using a SQL database, but it is just an abstraction.

Once you have done all the testing you want, you can move on to code! Put in the connection and the queries in your code. It works the same way as if you were using JDBC to connect to a database… that simple!

A little example of Java code accessing Microsoft SharePoint

This demonstration will focus on a simple integration of Microsoft SharePoint and Microsoft Exchange. The code picks up appointments from Exchange and gets them to SharePoint. I hope it will allow you to get a feeling for how it works.
Using the same concept, you could put other types of info into SharePoint or get information from SharePoint (for example, get list items from a SharePoint List). It can go both ways.
For each target system you want (in this case Microsoft SharePoint and Microsoft Exchange Server) you need to use one of the connectors available for Connect Bridge (there are hundreds of them).
Let's get started with code and our Java SharePoint connector example:

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Program {
    
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("com.cnsconnect.mgw.jdbc.MgDriver");
        
        // STEP 1: Create a JDBC connection to each target server - you get the data for the connection string from Connect Bridge
        String exchangeConnectionString = 
"jdbc:MgDriver:IMPL=CORBA;ENC=UTF8;HOST=123.456.789.000;PORT=8087;UID=demouser;PWD='password';ACC=accountExchange;";
        String sharepointConnectionString =
        "jdbc:MgDriver:IMPL=CORBA;ENC=UTF8;HOST=123.456.789.000;PORT=8087;UID=demouser;PWD='password';ACC=accountSharePoint;";
        Connection exchangeConn =
        DriverManager.getConnection(exchangeConnectionString);
        Connection sharepointConn =
        DriverManager.getConnection(sharepointConnectionString);
        Statement exchangeSt = exchangeConn.createStatement();
        System.out.println("Connecting to Exchange...");
        
        // STEP 2: Provide an appropriate object like a ResultSet in JAVA
        
        // STEP 3: Fill the object with data from the source server
        ResultSet exchangeRs = exchangeSt.executeQuery("SELECT * FROM [Appointment]");
        // create a new JDBC statement for inserting PreparedStatement
        sharepointSt = sharepointConn.prepareStatement("INSERT INTO [Calendar] ([Title], [Description], [Location], [StartTime], [EndTime]) VALUES ( ?, ?, ?, ?, ?)");
        
        // STEP 4: Manipulate the data and/or apply a workflow rule
        // in this case, check if the appointment is private, if not, insert it into SharePoint
        while (exchangeRs.next()) {
            Boolean isPrivate = exchangeRs.getBoolean("IsPrivate");
            if (isPrivate != null && isPrivate)
            {
            System.out.println("Skipping '" + exchangeRs.getString("Subject") + "'");
            continue;
            }
           // Fill parameters with values for SharePoint 
            sharepointSt.setString(1, exchangeRs.getString("Subject"));
            sharepointSt.setString(2, exchangeRs.getString("Body")); 
            sharepointSt.setString(3, exchangeRs.getString("Location"));
            sharepointSt.setTimestamp(4, exchangeRs.getTimestamp("StartDate")); 
            sharepointSt.setTimestamp(5, exchangeRs.getTimestamp("EndDate"));
            
            System.out.println("Inserting '" + exchangeRs.getString("Subject") + "'");
            
            // STEP 5: Insert the data into the target SharePoint server, using the previously prepared statement
            sharepointSt.execute();
        }
        exchangeRs.close();
        exchangeSt.close();
        sharepointSt.close();
        // STEP 6: Close Connections 
        exchangeConn.close();
        sharepointConn.close();
    }
}

Yeap, it is that simple!

The best thing is that once you finished your code and you have a working solution, you know it requires zero maintenance. That's right! All the maintenance effort is on the side of Connect Bridge. That is why it is a paid tool. Try it out, and you'll see it is worth every penny!

It is also good to note that Connect Bridge also handles authentication and security, which can take up a significant amount of time if you are developing from scratch.

Takeaway

I have shown you how accessing Microsoft SharePoint data in Java can be easily done using the Connect Bridge integration platform.

And the good news is that this is also possible with Microsoft Dynamics and much of Microsoft software, Salesforce, and many others. You just need to get Connect Bridge, as the base integration platform, and choose the right connector.

In case you want to know what connectors are currently available for Connect Bridge, either go to https://www.connecting-software.com/connect-bridge-connectors/ or ask me about it in the comments, and I will check it out (new connectors are launched regularly).

As a final point, it is also important to note that some ready-made, out-of-the-box products use Connect Bridge. An important example is CB SharePoint Exchange Sync, which allows you to choose custom one-way or two-way synchronization of your SharePoint content with Exchange / Outlook with no coding at all.
So, now you know, there is a better way and it is called Connect Bridge. Why don't you give it a try?
Let me know if you enjoyed this article in the comments below!

Discover and read more posts from Ana Neto
get started
post commentsBe the first to share your opinion
VAIBHAV MEHTA
4 years ago

Thanks for sharing.

Show more replies