Monday, March 10, 2025

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 under a given SharePoint site and a specified parent path. The parent path can be a direct drive or a nested structure like A/B or A/B/C, where A is the drive name.

Using Microsoft Graph API 1.0, we can achieve this by sending requests to create folders dynamically based on an input JSON file containing:

  • Site Name: The SharePoint site where folders should be created.
  • Parent Path: The target location within the document library.
  • Folder Name: The name of the new folder to be created.

Solution Steps

Step 1: Prerequisites

Before proceeding, ensure the following:

  • Access to Microsoft Graph API 1.0
  • Appropriate SharePoint permissions (Files.ReadWrite.All or Sites.Manage.All)
  • Azure AD App Registration with API permissions granted

Step 2: Retrieve the Site ID

First, fetch the Site ID using the site name:

API Request:

GET https://graph.microsoft.com/v1.0/sites/{tenant-name}.sharepoint.com:/sites/{site-name}
Authorization: Bearer {access-token}

Response:

{
  "id": "site-id",
  "name": "Your Site Name"
}

Step 3: Retrieve the Drive ID

Every SharePoint document library is associated with a drive. Retrieve the drive ID using:

API Request:

GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives
Authorization: Bearer {access-token}

Response:

{
  "value": [
    {
      "id": "drive-id",
      "name": "Documents"
    }
  ]
}

Step 4: Create Folders Dynamically

To create a folder, use the parent path from the input JSON.

Scenario 1: Creating Folder in Root (A)

If the parentPath is A, the request should be:

API Request:

PUT https://graph.microsoft.com/v1.0/drives/{drive-id}/root/children
Content-Type: application/json
Authorization: Bearer {access-token}

{
  "name": "FolderName",
  "folder": {},
  "@microsoft.graph.conflictBehavior": "fail"
}
Scenario 2: Creating Folder in Subdirectories (A/B or A/B/C)

For nested folders, modify the request to include the path:

API Request:

PUT https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{parentPath}/FolderName
Content-Type: application/json
Authorization: Bearer {access-token}

{
  "folder": {},
  "@microsoft.graph.conflictBehavior": "fail"
}

Step 5: Handling Errors and Edge Cases

  • Folder Already Exists: Use "@microsoft.graph.conflictBehavior": "replace" if you want to replace an existing folder.
  • Invalid Path: Ensure that the parentPath exists before attempting to create a folder.
  • Permissions Issue: Validate that the API token has the correct scope for SharePoint folder creation.

Detailed steps with screenshots:

Create Rest connection using microsoft graph:



Integration flow:


Configure trigger:

Request:

{ "parentpath" : "DriveName/FolderName", "foldername" : "New Folder Name", 
"site" : "Site Name" }

Response:

{ "status" : "Success", "url" : "<folder url returned from API response>", "Error_Message" : "<Error message information>" }




Get site id:
/sites/{tenant-name}.sharepoint.com:/sites/{site_name}




Get Drive Id:
/sites/{siteid}/drives
Add $filter






Xslt code:
<xsl:template name="tracking_var_3" xml:id="id_76"/>
<xsl:template match="/" xml:id="id_12">
    <nstrgmpr:execute xml:id="id_31">
        <nstrgmpr:TemplateParameters xml:id="id_32">
            <ns32:siteId xml:id="id_32">
                <xsl:value-of xml:id="id_33" select="$GetSiteID/nsmpr0:executeResponse/ns27:response-wrapper/ns27:id"/>
            </ns32:siteId>
        </nstrgmpr:TemplateParameters>
        <nstrgmpr:QueryParameters xml:id="id_47">
            <xsl:choose>
                <xsl:when test="contains(/nssrcmpr:execute/ns18:request-wrapper/ns18:parentpath, &quot;/&quot;)">
                    <ns32:_0x646c72_filter>
                        <xsl:value-of select="concat (&quot;name eq '&quot;, substring-before (/nssrcmpr:execute/ns18:request-wrapper/ns18:parentpath, &quot;/&quot; ), &quot;'&quot; )"/>
                    </ns32:_0x646c72_filter>
                </xsl:when>
                <xsl:otherwise xml:id="id_61">
                    <ns32:_0x646c72_filter xml:id="id_62">
                        <xsl:value-of xml:id="id_63" select="concat (&quot;name eq '&quot;, /nssrcmpr:execute/ns18:request-wrapper/ns18:parentpath, &quot;'&quot; )"/>
                    </ns32:_0x646c72_filter>
                </xsl:otherwise>
            </xsl:choose>
        </nstrgmpr:QueryParameters>
    </nstrgmpr:execute>
</xsl:template>
</xsl:stylesheet>

Create folder

/drives/{drive-id}/root{parentPath}/children

Request:
{
  "name": "Test3",
  "folder": {"name":"Test3"},
  "@microsoft.graph.conflictBehavior": "rename"
}





Xslt code:
<xsl:template match="/" xml:id="id 11">
trgmpr:execute xml:id="id 12">
<nstrgmpr: TemplateParameters xml:id="id_42">
<xsl:choose xml:id="id 119">
<xsl:when xml:id="id_120" test="contains (/nssrcmpr: execute/ns17: request-wrapper/ns17: parentpath, &quot;/&quot;)"> <ns40:driveid xml:id="id_ 121">
<xsl:value-of xml:id="id_122" select="$GetDriveID/nsmpro: executeResponse/ns33: response-wrapper/ns33: value [ns33: name=substring-before (/nssrcmpr: execute/ns17: request-wrapper/ns17: parentpath, &quot;/&quot;)]/ns33:id"/> </ns40: driveid>
</xsl:when>
<xsl:otherwise xml:id="id_123">
<ns40: drive id xml:id="id 124">
<xsl:value-of xml:id="id_125" select="$GetDriveID/nsmpre: executeResponse/ns33: response-wrapper/ns33: value [ns33: name=/nss rcmpr: execute/ns17: request-wrapper/ns17: parentpath]/ns33:id"/> </ns40: driveid> </xsl:otherwise>
</xsl:choose>
<xsl:choose xml:id="id_83">
<xsl:when xml:id="id_84" test="contains (/nssrcmpr: execute/ns17: request-wrapper/ns17: parentpath, &quot;/&quot;)">
<ns40:directory xml:id="id_45">
<xsl:value-of xml:id="id_46" select="concat (&quot;:/&quot;, substring-after (/nssrcmpr: execute/ns17: request-wrapper/ns17: parentpath, &quot;/&quot; ), &quot;:/&quot;)"/> </ns40:directory>
</xsl:when>
<xsl:otherwise xml:id="id_87">
<ns40:directory xml:id="id_45">
<xsl:value-of xml:id="id_89" select="&quot;/&quot;"/>
</ns40:directory>
</xsl:otherwise>
</xsl:choose>
</nstrgmpr:TemplateParameters>
<ns39:request-wrapper xml:id="id_47">
<ns39: name xml:id="id_48">
<xsl:value-of xml:id="id_49" select="/nssrcmpr: execute/ns17: request-wrapper/ns17: foldername"/>
</ns39:name>
<ns39:folder xml:id="id_50">
<ns 39: name xml:id="id_51">
<xsl:value-of xml:id="id_52" select="/nssrcmpr: execute/ns17: request-wrapper/ns17: foldername"/>
</ns39:name>
</ns39: folder>
<ns39: 8x617472_microsoft.graph.conflictBehavior xml:id="id_53">
<xsl:value-of xml:id="id_54" select="&quot; rename&quot;"/>
</ns 39: 0x617472_microsoft.graph.conflictBehavior>
</ns39:request-wrapper>
</nstrgmpr: execute>
</xsl:template>

Map response:


Xslt code:

<nstrgmpr:executeResponse xml:id="id_12">
<ns17: response-wrapper xml:id="id_52">
<xsl:choose>
<xsl:when test="$CreateSharePointFolder/nsmpr4: executeResponse/ns28: ConnectivityProperties/ns28: RestAPI/ns28:http.response.status = 201">
<ns17:status>
<xsl:value-of select="&quot; Success&quot;"/>
</ns17: status>
</xsl:when>
<xsl:otherwise xml:id="id_115">
<ns17:status xml:id="id 116">
<xsl:value-of xml:id="id_117" select="&quot; Error&quot;"/>
</ns17:status>
</xsl:otherwise>
</xsl:choose>
<ns17:url xml:id="id 126">
<xsl:if xml:id="id_125" test="$CreateSharePointFolder/nsmpr4: executeResponse/ns28: ConnectivityProperties/ns28: RestAPI/ns28:http.response.status = 201"> <xsl:value-of xml:id="id_127" select="$CreateSharePointFolder/nsmpr4: executeResponse/ns29: response-wrapper/ns29:webUrl"/>
</ns17:url>
</xsl:if>
<xsl:if xml:id="id_129" test="$CreateSharePointFolder/nsmpr4: executeResponse/ns28: ConnectivityProperties/ns28: RestAPI/ns28:http.response.status != 201"> <ns17: Error Message xml:id="id_130"> <xsl:value-of xml:id="id_131" select="$CreateSharePointFolder/nsmpr4: executeResponse/ns28: ConnectivityProperties/ns28: RestAPI/ns28:http.response.reason"/> </ns17: Error_Message>
</xsl:if>
</ns17: response-wrapper> 
</nstrgmpr: executeResponse>
</xsl:template>

Test:

Reference:

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:



How to use Microsoft graph explorer to test graph APIs

Use Case: How to use or test Microsoft 365 graph APIs using Graph Explorer.

Solution Steps:

  1. Access Graph Explorer: Go to Microsoft Graph Explorer.
  2. Sign In: Click "Sign in with Microsoft" and log in using your Microsoft 365 credentials.
  3. Select API: Choose GET https://graph.microsoft.com/v1.0/me to fetch the logged-in user's details.
  4. Run Query: Click "Run Query" to execute the API call and view the response.
  5. Modify & Test: Try different APIs like /users, /groups, or /me/messages by modifying the request.
  6. Check Permissions: If needed, click "Modify Permissions" and consent to required scopes.
  7. Analyze Response: Review the JSON output to validate the API response.

This helps test and understand Microsoft Graph APIs without writing code.

Screenshots:

Login


Checking my user details

Checking my drive id


Testing to see the drive children.



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 ...