The SharePoint REST API Client allows you to authenticate with SharePoint Online (2013) via OAuth2 and work with a subset of SharePoint's functionality (currently Lists and Users) using PHP.
The library is available to anyone and is licensed under the MIT license.
Add altek/sharepoint-client to your composer.json
and run composer install or composer update.
{
"require": {
"altek/sharepoint-client": "*"
}
}
<?php
require 'vendor/autoload.php';
use Altek\SharePoint\SharePointException;
use Altek\SharePoint\RESTClient;
try {
$config = array(
'url' => 'https://example-69z86c039b91sn.sharepoint.com', // Application URL
'path' => '/sites/mySite' // Application URL Path
);
$rc = new RESTClient($config);
} catch(SharePointException $e) {
// handle exceptions
}
Attention: In order to use the methods provided by this class, you need an Access Token which can be generated/requested through a logged User or an App only policy.
try {
$config = array(
'token' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyNTQyNGR...', // Context Token
'secret' => 'YzcZQ7N4lTeK5COin/nmNRG5kkL35gAW1scrum5mXVgE='
);
$rc->tokenFromUser($config);
} catch(SharePointException $e) {
// handle exceptions
}
Note: The context token is accessible via the SPAppToken input (POST) when SharePoint redirects to our Application.
try {
$config = array(
'acs' => 'https://accounts.accesscontrol.windows.net/tokens/OAuth/2', // Access Control Service URL
'client_id' => '52848cad-bc13-4d69-a371-30deff17bb4d/example.com@09g7c3b0-f0d4-416d-39a7-09671ab91f64',
'secret' => 'YzcZQ7N4lTeK5COin/nmNRG5kkL35gAW1scrum5mXVgE=',
'resource' => '00000000-0000-ffff-0000-000000000000/example-69z86c039b91sn.sharepoint.com@09g7c3b0-f0d4-416d-39a7-09671ab91f64'
);
$rc->tokenFromApp($config);
} catch(SharePointException $e) {
// handle exceptions
}
Both methods return an Array with the access token and start/expire dates as Carbon objects. The access token will be stored internally so that all methods that require it, have access to it.
try {
$ctx = $rc->getContextInfo();
} catch(SharePointException $e) {
// handle exceptions
}
This method returns an Array with the SharePoint's library version and the Form Digest (needed to update, upload and delete Items).
try {
// Extra properties to add
$extra = array(
'is_hidden' => 'Hidden',
// ...
);
$lists = $rc->getLists($extra);
} catch(SharePointException $e) {
// handle exceptions
}
This method returns an Array with all the available Lists.
Note: By default, a List Array will only contain: GUID, Title, Description, Items, Created date as a Carbon object.
If more information about a List is needed, use the $extra Array parameter to add other properties returned by the SharePoint API to the resulting Array.
try {
$library = 'MyLibrary';
$count = $rc->getListItemCount($library);
} catch(SharePointException $e) {
// handle exceptions
}
This method returns an integer with the number of Items that exist in a List.
try {
$library = 'MyLibrary';
// Extra properties to add
$extra = array(
'content_type_id' => 'ContentTypeId',
// ...
);
$items = $rc->getListItems($library, $extra);
} catch(SharePointException $e) {
// handle exceptions
}
This method returns an Array with all the available Items from a List.
Note: By default, an Item Array will only contain: ID, GUID, Title, Entity Type, URI, Author ID, Editor ID, and Created/Modified date as Carbon objects.
If more information about an Item is needed, use the $extra Array parameter to add other properties returned by the SharePoint API to the resulting Array.
try {
$library = 'MyLibrary';
$id = 1;
// Extra properties to add
$extra = array(
'content_type_id' => 'ContentTypeId',
// ...
);
$item = $rc->getListItem($library, $id, $extra);
} catch(SharePointException $e) {
// handle exceptions
}
This method returns an Array with the Item information.
Note: By default, an Item Array will only contain: ID, GUID, Title, Entity Type, URI, Author ID, Editor ID, and Created/Modified date as Carbon objects.
If more information about an Item is needed, use the $extra Array parameter to add other properties returned by the SharePoint API to the resulting Array.
try {
$library = 'MyLibrary';
$file = '/path/to/a/picture.jpg';
// Item properties
$properties = array(
'Title' => 'A picture in JPEG format',
// ...
);
// Silently overwrite an existing file with the same name
$overwrite = true;
$rc->uploadListItem($library, $file, $properties, $overwrite);
} catch(SharePointException $e) {
// handle exceptions
}
This method returns true if the file was successfully uploaded.
Note: If the $overwrite value is false, an exception will be thrown when uploading a file with a name that already exists in the List.
try {
$library = 'MyLibrary';
$id = 1;
// Item properties
$properties = array(
'Title' => 'A different title for the picture in JPEG format',
// ...
);
// Entity Type
$type = 'SP.Data.ReportsListItem';
$rc->updateListItem($library, $id, $properties, $type);
} catch(SharePointException $e) {
// handle exceptions
}
This method returns true if the file was successfully updated.
Note: The Entity Type of an Item can be fetched using the getListItem() method.
try {
$library = 'MyLibrary';
$id = 1;
$rc->deleteListItem($library, $id);
} catch(SharePointException $e) {
// handle exceptions
}
This method returns true if the file was successfully deleted.
Note: The Entity Type of an Item can be fetched using the getListItem() method.
try {
$user = $rc->getCurrentUserProfile();
} catch(SharePointException $e) {
// handle exceptions
}
This method returns an Array with the User information.
Note: The User information Array will only contain: Account Name, Email, Name, URL, Picture URL and Title.
Attention: This method will throw an exception if the Access Token was generated/requested by an App only policy.
try {
$account = 'i:0#.f|membership|username@example.onmicrosoft.com';
$user = $rc->getUserProfile($account);
} catch(SharePointException $e) {
// handle exceptions
}
This method returns an Array with the User information.
Note: The User information Array will only contain: Account Name, Email, Name, URL, Picture URL and Title.
Note that extra properties of date or datetime types, will be returned as Carbon objects.