valter-medeiros / laravel-sharepoint
A Laravel filesystem driver for SharePoint and OneDrive using Microsoft Graph API with support for certificates.
Package info
github.com/valtermedeiros/laravel-sharepoint
pkg:composer/valter-medeiros/laravel-sharepoint
Requires
- php: ^8.1
- ext-openssl: *
- guzzlehttp/guzzle: ^7.0
- illuminate/cache: ^10.0|^11.0|^12.0
- illuminate/console: ^10.0|^11.0|^12.0
- illuminate/filesystem: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
- league/flysystem: ^3.0
Requires (Dev)
- orchestra/testbench: ^10.0
- phpunit/phpunit: ^11.5
README
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
Storagefacade. - 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
.pfxfiles 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
- Open your
config/filesystems.phpfile and add thesharepointdisk to thedisksarray:
'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 ], ],
- Add the corresponding variables to your
.envfile:
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.
- Upload your
.pfxfile to your server temporarily. - Run the encryption command:
php artisan sharepoint:encrypt-cert /path/to/your/certificate.pfx
- This will generate a new file named
certificate.pfx.enc. - Delete the original
.pfxfile from your server. - Update your
.envfile to point to the new.encfile:
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)
-
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
- Example:
-
Add this Site ID to your
.envfile:
SHAREPOINT_SITE_ID="universidadedosacores.sharepoint.com,4252a976-98b4-48ae-b79c-abb17b10f02c"
- 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"
- 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
.envfile:
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.