Skip to content

Repository files navigation

About

Microsoft 365 Library for PHP. A REST/OData based client library for Microsoft 365.

Usage

  1. Installation
  2. Working with SharePoint API
  3. Working with Teams API
  4. Working with Outlook API
  5. Working with OneDrive API

Status

Total DownloadsLatest Stable VersionBuild StatusLicense

Installation

You can use Composer or simply Download the Release

Composer

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

or 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';

Requirements

PHP version: PHP 7.1 or later

Working with SharePoint API

The list of supported SharePoint versions:

  • SharePoint Online and OneDrive for Business
  • SharePoint On-Premises (2013-2019)

Authentication

The following auth flows supported:

1. app principal with client credentials:

useOffice365\Runtime\Auth\ClientCredential;
useOffice365\SharePoint\ClientContext;
$credentials = newClientCredential("{clientId}", "{clientSecret}");
$ctx = (newClientContext("{siteUrl}"))->withCredentials($credentials);

Documentation:

2. app principal with client certificate:

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:

3. user credentials auth:

useOffice365\Runtime\Auth\UserCredentials;
useOffice365\SharePoint\ClientContext;
$credentials = newUserCredentials("{userName}", "{password}");
$ctx = (newClientContext("{siteUrl}"))->withCredentials($credentials);

4. NTLM auth (for SharePoint On-Premises):

useOffice365\Runtime\Auth\UserCredentials;
useOffice365\SharePoint\ClientContext;
$credentials = newUserCredentials("{userName}", "{password}");
$ctx = (newClientContext("{siteUrl}"))->withNtlm($credentials);

Examples

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();

Working with Teams API

Example: create a Team

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();

Working with Outlook API

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();

Working with OneDrive API

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();

About

Microsoft 365 Library for PHP.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages