Microsoft 365 Library for PHP. A REST/OData based client library for Microsoft 365.
- Installation
- Working with SharePoint API
- Working with Teams API
- Working with Outlook API
- Working with OneDrive API
You can use Composer or simply Download the Release
The preferred method is via composer. Follow the installation instructions if you do not already have composer installed.
Once composer installed, execute the following command in your project root to install this library:
composer require vgrem/php-spoor via composer.json file:
{
"require": {
"vgrem/php-spo": "^2.4"
}
}Finally, be sure to include the autoloader:
require_once '/path/to/your-project/vendor/autoload.php';
PHP version: PHP 7.1 or later
The list of supported SharePoint versions:
- SharePoint Online and OneDrive for Business
- SharePoint On-Premises (2013-2019)
The following auth flows supported:
useOffice365\Runtime\Auth\ClientCredential;
useOffice365\SharePoint\ClientContext;
$credentials = newClientCredential("{clientId}", "{clientSecret}");
$ctx = (newClientContext("{siteUrl}"))->withCredentials($credentials);Documentation:
useOffice365\Runtime\Auth\ClientCredential;
useOffice365\SharePoint\ClientContext;
$tenant = "{tenant}.onmicrosoft.com"; //tenant id or name$privateKetPath = "-- path to private.key file--"$privateKey = file_get_contents($privateKetPath);$ctx = (new ClientContext("{siteUrl}"))->withClientCertificate($tenant, "{clientId}", $privateKey, "{thumbprint}");Documentation:
useOffice365\Runtime\Auth\UserCredentials;
useOffice365\SharePoint\ClientContext;
$credentials = newUserCredentials("{userName}", "{password}");
$ctx = (newClientContext("{siteUrl}"))->withCredentials($credentials);useOffice365\Runtime\Auth\UserCredentials;
useOffice365\SharePoint\ClientContext;
$credentials = newUserCredentials("{userName}", "{password}");
$ctx = (newClientContext("{siteUrl}"))->withNtlm($credentials);The following examples demonstrates how to perform basic CRUD operations against SharePoint list item resources:
Example 1. How to read SharePoint list items:
useOffice365\SharePoint\ClientContext;
useOffice365\Runtime\Auth\ClientCredential;
useOffice365\SharePoint\ListItem;
$credentials = newClientCredential("{client-id}", "{client-secret}");
$client = (newClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);
$web = $client->getWeb();
$list = $web->getLists()->getByTitle("{list-title}"); //init List resource$items = $list->getItems(); //prepare a query to retrieve from the$client->load($items); //save a query to retrieve list items from the server$client->executeQuery(); //submit query to SharePoint server/** @var ListItem $item */foreach($itemsas$item) {
print"Task: {$item->getProperty('Title')}\r\n";
}or via fluent API syntax:
useOffice365\SharePoint\ClientContext;
useOffice365\Runtime\Auth\ClientCredential;
useOffice365\SharePoint\ListItem;
$credentials = newClientCredential("{client-id}", "{client-secret}");
$client = (newClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);
$items = $client->getWeb()
->getLists()
->getByTitle("{list-title}")
->getItems()
->get()
->executeQuery();
/** @var ListItem $item */foreach($itemsas$item) {
print"Task: {$item->getProperty('Title')}\r\n";
}Example 2. How to create SharePoint list item:
useOffice365\SharePoint\ClientContext;
useOffice365\Runtime\Auth\ClientCredential;
$credentials = newClientCredential("{client-id}", "{client-secret}");
$client = (newClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);
$list = $client->getWeb()->getLists()->getByTitle("Tasks");
$itemProperties = array('Title' => 'Order Approval', 'Body' => 'Order approval task');
$item = $list->addItem($itemProperties);
$client->executeQuery();
print"Task {$item->getProperty('Title')} has been created.\r\n";Example 3. How to delete a SharePoint list item:
useOffice365\SharePoint\ClientContext;
useOffice365\Runtime\Auth\ClientCredential;
$credentials = newClientCredential("{client-id}", "{client-secret}");
$client = (newClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);
$list = $client->getWeb()->getLists()->getByTitle("Tasks");
$listItem = $list->getItemById("{item-id-to-delete}");
$listItem->deleteObject();
$client->executeQuery();Example 4. How to update SharePoint list item:
useOffice365\SharePoint\ClientContext;
useOffice365\Runtime\Auth\ClientCredential;
$credentials = newClientCredential("{client-id}", "{client-secret}");
$client = (newClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);
$list = $client->getWeb()->getLists()->getByTitle("Tasks");
$listItem = $list->getItemById("{item-id-to-update}");
$listItem->setProperty('PercentComplete',1);
$listItem->update();
$client->executeQuery();The following is an example of a minimal request to create a Team (via delegated permissions)
useOffice365\GraphServiceClient;
useOffice365\Runtime\Auth\AADTokenProvider;
useOffice365\Runtime\Auth\UserCredentials;
functionacquireToken()
{
$tenant = "{tenant}.onmicrosoft.com";
$resource = "https://graph.microsoft.com";
$provider = newAADTokenProvider($tenant);
return$provider->acquireTokenForPassword($resource, "{clientId}",
newUserCredentials("{UserName}", "{Password}"));
}
$client = newGraphServiceClient("acquireToken");
$teamName = "My Sample Team";
$newTeam = $client->getTeams()->add($teamName)->executeQuery();Supported list of APIs:
The following example demonstrates how to send a message via Outlook Mail API:
useOffice365\GraphServiceClient;
useOffice365\Outlook\Message;
useOffice365\Outlook\ItemBody;
useOffice365\Outlook\BodyType;
useOffice365\Outlook\EmailAddress;
useOffice365\Runtime\Auth\AADTokenProvider;
useOffice365\Runtime\Auth\UserCredentials;
functionacquireToken()
{
$tenant = "{tenant}.onmicrosoft.com";
$resource = "https://graph.microsoft.com";
$provider = newAADTokenProvider($tenant);
return$provider->acquireTokenForPassword($resource, "{clientId}",
newUserCredentials("{UserName}", "{Password}"));
}
$client = newGraphServiceClient("acquireToken");
/** @var Message $message */$message = $client->getMe()->getMessages()->createType();
$message->setSubject("Meet for lunch?");
$message->setBody(newItemBody(BodyType::Text,"The new cafeteria is open."));
$message->setToRecipients([newEmailAddress(null,"fannyd@contoso.onmicrosoft.com")]);
$client->getMe()->sendEmail($message,true)->executeQuery();The following example demonstrates how retrieve My drive Url via OneDrive API:
useOffice365\GraphServiceClient;
useOffice365\Runtime\Auth\AADTokenProvider;
useOffice365\Runtime\Auth\UserCredentials;
functionacquireToken()
{
$tenant = "{tenant}.onmicrosoft.com";
$resource = "https://graph.microsoft.com";
$provider = newAADTokenProvider($tenant);
return$provider->acquireTokenForPassword($resource, "{clientId}",
newUserCredentials("{UserName}", "{Password}"));
}
$client = newGraphServiceClient("acquireToken");
$drive = $client->getMe()->getDrive();
$client->load($drive);
$client->executeQuery();
print$drive->getWebUrl();