Looking to hire Laravel developers? Try LaraJobs

laravel-sharepoint maintained by valter-medeiros

Description
A Laravel filesystem driver for SharePoint and OneDrive using Microsoft Graph API with support for certificates.
Last update
2026/07/13 15:02 (dev-main)
License
Downloads
0

Comments
comments powered by Disqus

Laravel SharePoint Filesystem Driver

Tests Latest Version on Packagist License

A Flysystem V3 adapter for Laravel that allows you to use Microsoft SharePoint and OneDrive as a storage disk via the Microsoft Graph API.

This package includes a unique secure certificate handler that allows you to encrypt your .pfx certificates using Laravel's built-in encryption, ensuring your private keys are never stored in plaintext on your server.

Features

  • Fully integrates with Laravel's Storage facade.
  • Supports all standard Flysystem operations (read, write, delete, list, move, copy).
  • Authenticates via Microsoft Graph API (OAuth2 Client Credentials flow).
  • Supports both Client Secret and Certificate (.pfx) authentication.
  • Secure Certificate Storage: Encrypt your .pfx files so they are decrypted on-the-fly in memory, keeping your server secure even if compromised.

Requirements

  • PHP >= 8.1
  • Laravel >= 10.0
  • A Microsoft Azure Active Directory (Entra ID) App Registration with Graph API permissions (Files.ReadWrite.All, Sites.ReadWrite.All).

Installation

You can install the package via composer:

composer require valter-medeiros/laravel-sharepoint

Configuration

  1. Open your config/filesystems.php file and add the sharepoint disk to the disks array:
'disks' => [
    // ... other disks

    'sharepoint' => [
        'driver' => 'sharepoint',
        'tenant_id' => env('SHAREPOINT_TENANT_ID'),
        'client_id' => env('SHAREPOINT_CLIENT_ID'),
        
        // Use EITHER client_secret OR certificate_path
        'client_secret' => env('SHAREPOINT_CLIENT_SECRET'), 
        
        // Certificate Authentication (Recommended for production)
        'certificate_path' => env('SHAREPOINT_CERTIFICATE_PATH'), // Path to .pfx or .pfx.enc
        'certificate_password' => env('SHAREPOINT_CERTIFICATE_PASSWORD'), // Password for the .pfx
        
        // Optional Configuration
        'drive_id' => env('SHAREPOINT_DRIVE_ID'), // Specific drive ID. If null, uses the default drive.
        'prefix' => env('SHAREPOINT_PREFIX', ''), // Optional path prefix
    ],
],
  1. Add the corresponding variables to your .env file:
SHAREPOINT_TENANT_ID="your-azure-tenant-id"
SHAREPOINT_CLIENT_ID="your-azure-client-id"

# If using a Client Secret:
SHAREPOINT_CLIENT_SECRET="your-client-secret"

# If using a Certificate (Recommended):
SHAREPOINT_CERTIFICATE_PATH="/absolute/path/to/your/certificate.pfx.enc"
SHAREPOINT_CERTIFICATE_PASSWORD="your-certificate-password"

# Optional
SHAREPOINT_DRIVE_ID="your-drive-id"
SHAREPOINT_PREFIX="Shared Documents"

Secure Certificate Authentication (Recommended)

Storing .pfx certificates in plaintext on your server can be a security risk. This package provides a command to encrypt your certificate using your Laravel APP_KEY. The package will decrypt it on-the-fly in memory when authenticating with Microsoft Graph.

  1. Upload your .pfx file to your server temporarily.
  2. Run the encryption command:
php artisan sharepoint:encrypt-cert /path/to/your/certificate.pfx
  1. This will generate a new file named certificate.pfx.enc.
  2. Delete the original .pfx file from your server.
  3. Update your .env file to point to the new .enc file:
SHAREPOINT_CERTIFICATE_PATH="/path/to/your/certificate.pfx.enc"
SHAREPOINT_CERTIFICATE_PASSWORD="your-certificate-password"

Usage

Once configured, you can use the disk just like any other Laravel storage disk:

use Illuminate\Support\Facades\Storage;

// Write a file
Storage::disk('sharepoint')->put('reports/2026/summary.pdf', $pdfContent);

// Read a file
$content = Storage::disk('sharepoint')->get('reports/2026/summary.pdf');

// Check if a file exists
if (Storage::disk('sharepoint')->exists('reports/2026/summary.pdf')) {
    // ...
}

// Delete a file
Storage::disk('sharepoint')->delete('reports/2026/summary.pdf');

// List files in a directory
$files = Storage::disk('sharepoint')->files('reports/2026');

// Create a directory
Storage::disk('sharepoint')->makeDirectory('reports/2027');

Finding your Drive ID

If you don't specify a SHAREPOINT_DRIVE_ID, the package will attempt to use the default drive associated with the authenticated application.

To target a specific SharePoint Document Library, you will need its Drive ID. You can find this using the built-in artisan command or Microsoft Graph Explorer.

Using the Artisan Command (Recommended)

  1. First, you need your Site ID. You can find this by taking your SharePoint site URL and appending /_api/site/id/ to it.

    • Example: https://universidadedosacores.sharepoint.com/sites/MySite/_api/site/id/
    • The Site ID usually looks like: universidadedosacores.sharepoint.com,4252a976-98b4-48ae-b79c-abb17b10f02c
  2. Add this Site ID to your .env file:

SHAREPOINT_SITE_ID="universidadedosacores.sharepoint.com,4252a976-98b4-48ae-b79c-abb17b10f02c"
  1. Run the list-drives command:
php artisan sharepoint:list-drives

(You can also pass the site ID directly to the command: php artisan sharepoint:list-drives "your-site-id")

You can also search for a specific drive by name:

php artisan sharepoint:list-drives --search="ADSR_Cloud"
  1. The command will output a table of all available document libraries and their Drive IDs. Copy the ID of the library you want to use and add it to your .env file:
SHAREPOINT_DRIVE_ID="b!..."

Finding a Specific Folder ID (Optional)

If you need to find the ID of a specific folder inside a drive, you can use the list-folders command:

php artisan sharepoint:list-folders

You can also search for a specific folder by name, or look inside a specific path:

php artisan sharepoint:list-folders --search="My Folder"
php artisan sharepoint:list-folders --path="Shared Documents/2026"

Using Microsoft Graph Explorer

Alternatively, you can find your Site ID and Drive ID manually using the Microsoft Graph Explorer. For a detailed guide on how to do this, check out this excellent tutorial: SharePoint: Find Site ID and Drive IDs - Dave Herrell

License

The MIT License (MIT). Please see License File for more information.