SharePoint Integration With MuleSoft
This post discusses how to integrate SharePoint online with the MuleSoft series. Today, I will be covering several of the folder and list operations.
Join the DZone community and get the full member experience.
Join For FreeWhat Is SharePoint?
Microsoft SharePoint is used to create websites and it's a secure place to store, organize, share, and access information from any device.
Anypoint SharePoint Connector
SharePoint connector supports both SharePoint 2013 and SharePoint Online for use in the cloud and on-premises. This connector enables you to manage content and document management within your organization. This connector supports a number of operations and you can find more details on this page.
I will first cover the Folder Create and File Edd operations. Following this, I will cover the Folder Query, Folder Delete, List Create, List Get, and List Get All operations.
Prerequisite
Before getting started, the prerequisite is to have SharePoint online account. You can create a trial account by following the steps mentioned in the link. Also, make sure to add a SharePoint connector in Anypoint Studio from Anypoint Exchange. In this walkthrough, I have used SharePoint connector version 3.3.2.
Let's Get Started With Integration
Let's create a global SharePoint online element. In SharePoint, I have created a POC site as a teams site and configured POC site URL in the site URL. You can follow this article to create a SharePoint site. Now, refer to the below snapshot for configuration.
Now, read a file using the file connector read operation. Let's, configure SharePoint connector File Add operation to add a file to the newly created SharePoint folder. Provide the details for the below two fields in the File Add configuration
- File Server Relative URL = Where to add the new file.
- File Content Stream = The content that this new file should contain.
You can refer to the below snapshot for File Add configuration.
The final Mule flow will look like below once completed. I have provided the code at the end.
Let's Test a Mule Application
For the above postman request, the Mule application has created a SharePoint folder with the name xyz-docs and added sharepoint-test1.text file in it.
Mule Flow Code
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:sharepoint="http://www.mulesoft.org/schema/mule/sharepoint"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/sharepoint http://www.mulesoft.org/schema/mule/sharepoint/current/mule-sharepoint.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<sharepoint:sharepoint-online-config name="Sharepoint_Sharepoint_online"
doc:name="Sharepoint Sharepoint online"
doc:id="03c666f9-bc7e-4848-b969-aba494f4cae9">
<sharepoint:online-connection onlineUsername="*************@*****************.onmicrosoft.com"
onlinePassword="*****************************"
siteUrl="https://******************.sharepoint.com/teams/POC"/>
</sharepoint:sharepoint-online-config>
<http:listener-config name="HTTP_Listener_config"
doc:name="HTTP Listener config"
doc:id="c468f318-0920-426b-82d6-e41186dd786e">
<http:listener-connection host="0.0.0.0"
port="8081"/>
</http:listener-config>
<file:config name="File_Config"
doc:name="File Config"
doc:id="b54c5ea3-d94b-43e2-b723-b383ef6a4f46">
<file:connection workingDir="C:\luffy\Desktop\test\sharepoint-poc"/>
</file:config>
<flow name="sharepoint-poc-create-folder"
doc:id="eed8ec3d-48f5-4500-b6eb-404982540f2e">
<http:listener doc:name="listener"
doc:id="ae2d4983-d00c-4e19-b80b-c1ce57bc9459"
config-ref="HTTP_Listener_config"
path="sharepoint/create-folder"/>
<sharepoint:folder-create doc:name="create-folder"
doc:id="7f26ceac-bb63-48c1-9e92-1b7f5d9aef5e"
config-ref="Sharepoint_Sharepoint_online"
url='#["Shared Documents/" ++ (attributes.queryParams.newFolderName default "")]'
target="createFolderResponse"/>
<file:read doc:name="read-file"
doc:id="102e73c6-0ca3-40a9-8b85-9f54ce912d3d"
config-ref="File_Config"
path="sharepoint-test1.txt"/>
<sharepoint:file-add doc:name="add-file"
doc:id="3509ec2b-6135-4ed6-bf70-c98320a7ad64"
config-ref="Sharepoint_Sharepoint_online"
fileServerRelativeUrl='#[vars.createFolderResponse.serverRelativeUrl default "" ++ "/" ++ attributes.fileName]'>
<sharepoint:file-content-stream><![CDATA[#[payload]]]></sharepoint:file-content-stream>
</sharepoint:file-add>
<set-payload value="#[%dw 2.0
output application/json
---
payload]"
doc:name="response"
doc:id="8cb2cc38-cfee-43ef-ab93-08bd7c6e3852"/>
</flow>
</mule>
Now we will explore the Folder Query, Folder Delete, List Create, List Get, and List Get All operations.
Folder Query
This operation retrieves all folders that match the specified criteria. As a use case, we will retrieve folder details under the test folder. Here, Mule is a document library.
We have to provide the below 2 parameters to configure the Folder Query operation
- Query: OData query.
- Starting folder path (optional): The starting path of the folder from where to begin the query, relative to the document library selected as part of the query. For example, /test searches for folders inside /Mule/test.
Folder Delete
It deletes a folder from a document library. We will delete the xyz-docs folder under the test folder. We have to provide the server-relative URL of the folder to delete in the URL field.
If the folder is successfully deleted, then the Folder Delete operation will return a null payload.
List Create
It creates a new SharePoint list. We have to provide a SharePoint list reference in the list parameter.
In the case that you are using a lower version of the SharePoint connector, you may have to explicitly typecast the list parameter to SharePointList class. You can refer to this article for that.
List Get
It retrieves SharePoint list details of the provided list ID. We will provide the list ID of the employee list.
List Get All: It retrieves all (custom and system) SharePoint lists present on the site.
Wrapping Up
That will be all. I hope this article helps you with integrating your SharePoint projects with MuleSoft.
Opinions expressed by DZone contributors are their own.
Comments