Wednesday, March 5, 2025

OIC - How to download file from sharepoint using Microsoft Graph API 1.0

Downloading Files from SharePoint Using Microsoft Graph API in Oracle Integration Cloud (OIC)

Use Case

In an Oracle Integration Cloud (OIC) implementation, there may be a requirement to download files stored in a SharePoint document library for further processing. Microsoft Graph API provides a secure and efficient way to fetch files from SharePoint Online.

Solution Steps

Prerequisites

  • A registered Azure AD application with permissions to access SharePoint
  • Client ID, Tenant ID, and Client Secret for authentication
  • A SharePoint site with files stored in a document library
  • Oracle Integration Cloud (OIC) with REST Adapter

Step 1: Configure Microsoft Azure App

  1. Go to Azure Portal.
  2. Register a new application under Azure Active Directory (AAD).
  3. Assign API permissions:
    • Sites.Read.All or Sites.FullControl.All (Application permissions)
    • Files.Read.All for file access
  4. Generate a Client Secret for authentication.

Step 2: Obtain an Access Token

Use OIC to fetch an access token from Microsoft Identity Platform using the following API:

Request:

POST https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials
&client_id={client_id}
&client_secret={client_secret}
&scope=https://graph.microsoft.com/.default

Response:

{
  "access_token": "eyJ0eXAiOiJKV..."
}

Step 3: Download a File from SharePoint

Use Microsoft Graph API to fetch the file from the document library.

API Request:

GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/items/{file-id}/content
Authorization: Bearer {access_token}
  • {site-id}: Retrieve using https://graph.microsoft.com/v1.0/sites/{tenant-name}.sharepoint.com:/sites/{site-name}
  • {drive-id}: Retrieve using https://graph.microsoft.com/v1.0/sites/{site-id}/drives
  • {file-id}: Retrieve using https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root/children

Step 4: Implement in OIC

  1. Create an OIC Integration
    • Use the REST Adapter to invoke the Microsoft Graph API.
  2. Configure Authentication
    • Use OIC REST connections to pass the access token dynamically.
  3. Download and Store File
    • Store the response content in an FTP/SFTP location or pass it to another service.
Highlevel steps implemented:
  1. User will feed site name, drivename, file name pattern to download the file from sharepoint
  2. Get site id from site name
  3. Get drives under the site id
  4. Select the drive id for the drive name provided.
  5. List the children files or folders under the drive id
  6. Exclude the folders and select the file id for the file pattern
  7. Download content stream reference for the file id.
  8. Upload file content to object storage or other oprion as per requirement.

Detailed screenshots:

Create rest connection:

Connection url: https://graph.microsoft.com/v1.0

Provide client id , client secret, access token, scope and client authentication

Scope: https://graph.microsoft.com/.default



Configure trigger:

Request payload

{

  "ProcessingId": "",

  "SiteName": "",

  "DriveName": "",

  "FileNamePattern": "",

  "bucketName": "",

  "NameSpace": ""

}

Response payload:

{

  "Status": "WARNING/ERROR/SUCCESS",

  "File": [

    {

      "Names": "",

      "Message": "actual error"

    }

  ]

}




Get site id for site name

GET /{tenant}.sharepoint.com:/sites/{site-name}





Get drives under the site

/sites/{siteId}/drives

Add $filter query param and assign 

concat("name eq '",<driveName>,"'")





Assign drive id for the drive name


List drive files

/sites/{siteId}/drives/{driveId}/root/children






For each item


Exclude folders and select the file which match the pattern


Get content stream reference for the file:

/drives/{driveId}/items/{itemId}/content





Testing:



No comments:

Post a Comment

Featured Post

OIC - Creating Folders in SharePoint Using Microsoft Graph API 1.0 in Oracle Integration Cloud

Use Case Many businesses need to automate folder creation in SharePoint based on dynamic inputs. A common requirement is to create folders ...