Releases: vgrem/phpSPO
Version 2.5.1
Changelog
- SharePoint API: model updated to
16.0.21611.12002
version - SharePoint API: improved support for managing modern SharePoint sites
Example: Create a modern (communication) site
$credentials = new ClientCredential($ClientId, $ClientSecret);
$ctx = (new ClientContext($Url))->withCredentials($credentials);
$siteManager = new SPSiteManager($ctx);
$result = $siteManager->create("MyCommSite", $ownerEmail, "Low Business Impact");
$siteManager->executeQuery();
print("Site has been created: {$result->getValue()->SiteUrl} \n");
Version 2.5.0
Changelog
- introduced a support for Teams API
- performance improvements and better support for Fluent API syntax
- introduced
ClientRuntimeContext.executeQueryRetry
method to submit queries which supports transparently retrying a failed operation (retry pattern)
Working with Teams API
Example: create a Team
The following is an example of a minimal request to create a Team (via delegated permissions)
use Office365\GraphServiceClient;
use Office365\Runtime\Auth\AADTokenProvider;
use Office365\Runtime\Auth\UserCredentials;
function acquireToken()
{
$tenant = "{tenant}.onmicrosoft.com";
$resource = "https://graph.microsoft.com";
$provider = new AADTokenProvider($tenant);
return $provider->acquireTokenForPassword($resource, "{clientId}",
new UserCredentials("{UserName}", "{Password}"));
}
$client = new GraphServiceClient("acquireToken");
$teamName = "My Sample Team";
$newTeam = $client->getTeams()->add($teamName)->executeQuery();
Example: list all Teams
use Office365\GraphServiceClient;
use Office365\Runtime\Auth\AADTokenProvider;
use Office365\Runtime\Auth\UserCredentials;
function acquireToken()
{
$tenant = "{tenant}.onmicrosoft.com";
$resource = "https://graph.microsoft.com";
$provider = new AADTokenProvider($tenant);
return $provider->acquireTokenForPassword($resource, "{clientId}",
new UserCredentials("{UserName}", "{Password}"));
}
$client = new GraphServiceClient("acquireToken");
$teams = $client->getTeams()->getAll(array("displayName"))->executeQuery();
ClientRuntimeContext.executeQueryRetry
method usage
Once Team is created, it might not be immediately available due to replication delay and calling getting team endpoint could fail with a 404 error, the recommended pattern is to retry the get team call three times, with a 10 second delay between calls:
$team = $graphClient->getTeams()->getById($Id)->get()->executeQueryRetry();
Version 2.4.5
Changelog
-
#231: introduced support for
IPResolve
andForbidReuse
options forRequestOptions
class by @tommy2d -
various bug fixes, including #230
-
SharePoint API support for Taxonomy namespace, refer example 1 below
-
SharePoint API model has been updated to
16.0.21221.12006
-
fluent API improvements, a simplified way to initialize
GraphServiceClient
andOutlookClient
clients by passing acquire token function,AADTokenProvider
class which contains built-in support for Client credentials, Username/password flows, refer example 2 below
Examples
Example 1: export taxonomy data via SharePoint API
use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ClientContext;
use Office365\SharePoint\Taxonomy\TaxonomyService;
use Office365\SharePoint\Taxonomy\TermGroup;
$appPrincipal = new ClientCredential($clientId,$clientSecret);
$ctx = (new ClientContext($settings['Url']))->withCredentials($appPrincipal);
$taxSvc = new TaxonomyService($ctx);
$groups = $taxSvc->getTermStore()->getTermGroups()->get()->executeQuery();
$fp = fopen('./SiteTaxonomy.csv', 'w');
/** @var TermGroup $group */
foreach ($groups as $group){
fputcsv($fp, $group->toJson());
}
fclose($fp);
Example 2: Initialize Graph client
use Office365\Graph\GraphServiceClient;
use Office365\Runtime\Auth\AADTokenProvider;
use Office365\Runtime\Auth\UserCredentials;
function acquireToken()
{
$tenant = "{tenant}.onmicrosoft.com";
$resource = "https://graph.microsoft.com";
$provider = new AADTokenProvider($tenant);
return $provider->acquireTokenForPassword($resource, "{clientId}",
new UserCredentials("{UserName}", "{Password}"));
}
$client = new GraphServiceClient("acquireToken");
Version 2.4.4
Changelog
-
OData request headers adjustments (for compatibility with SharePoint 2013 REST Service implementation) #222 by @blizzz
-
#212:
GroupCollection.getByName
fix to safely address group by name by @mahmudz -
#213:
File.finishUpload
method fix by @ChangingTerry -
SharePoint API model has been updated to
16.0.21103.12002
version
Version 2.4.3
Changelog
-
Exclude non-essential files from dist #210 Credit goes to @rvitaliy
-
SharePoint API model has been updated to
16.0.20628.12006
version -
Error handling:
validate()
method for classResponse
introduced which throws an exception if the HTTP response was unsuccessful, usage:
$response = Requests::execute($request);
$response->validate();
Version 2.4.2
Changelog
- API support for Fluent interface which offers a more compact way of calling operations, for example, file could be downloaded like this:
$file = (new ClientContext($settings['Url']))->withCredentials($credentials)
->getWeb()->getFileByServerRelativePath(new SPResourcePath($fileUrl))->get()->executeQuery();
-
SharePoint API model has been updated to
16.0.20405.12008
version and introduced a new types and methods:Web.getFileByServerRelativePath($resourcePath)
Web.getFolderByServerRelativePath($resourcePath)
-
Support for addressing
Web
andFile
resources by absolute Url$fileAbsUrl = "https://contoso.sharepoint.com/sites/team/Shared Documents/sample.docx" $credentials = UserCredential(username, password) $fh = fopen($fileName, 'w+'); File::fromUrl($fileAbsUrl)->withCredentials($credentials)->download($fh)->executeQuery(); fclose($fh);
-
Support for simplified ways of authenticating against SharePoint
$credentials = new ClientCredential($clientId, $clientSecret); $ctx = (new ClientContext($url))->withCredentials($credentials);
Version 2.4.1
List of changes
-
SharePoint
(for version16.0.20106.12008
) andOutlookServices
models have been updated -
built-in support to retrieve large collections (or paged data) via lazy loading approach
-
support for upload "large" file (chunked upload)
Working with SharePoint large lists and libraries (which contains more than 5000
items)
Due to built-in support, nothing has changed in terms how list items are retrieved to preserve API simplicity, no matter how many items list contains (unless $top
query option is provided to restrict the amount of items to load)
Behind the scene, lazy loading approach is utilized to retrieve paged data, which offers optimal performance since the data is getting load only when requested.
$list = $ctx->getWeb()->getLists()->getByTitle("--Large List Title--");
$items = $list->getItems();
$ctx->load($items);
$ctx->executeQuery();
print $items->getCount() . PHP_EOL; //prints the actual amount of items in list/library
//iterate across all list items
foreach ($items as $index => $item){
print($index . ":" . $item->getProperty('Title') . PHP_EOL);
}
Upload large files
Note: Use this solution if you want to upload files that are larger than 2 MB to SharePoint.
$ctx = ClientContext::connectWithClientCredentials($Url, $ClientId, $ClientSecret);
$localPath = "../data/big_buck_bunny.mp4";
$targetLibraryTitle = "Documents";
$targetList = $ctx->getWeb()->getLists()->getByTitle($targetLibraryTitle);
$session = $targetList->getRootFolder()->getFiles()->createUploadSession($localPath, "big_buck_bunny.mp4",
function ($uploadedBytes) {
echo "Progress: $uploadedBytes bytes uploaded .." . PHP_EOL;
});
$ctx->executeQuery();
$targetFileName = $session->getFile()->getName();
echo "File $targetFileName has been uploaded.";
Version 2.4.0
- model has been updated from
16.0.20008.12009
API version - refactorings and root namespace changed from
Office365\PHP\Client\SharePoint
intoOffice365\SharePoint
- introduced a simplified way to initialize SharePoint client
a simplified way to initialize SharePoint client
from now on instead of
$authCtx = new AuthenticationContext($url); $authCtx->acquireTokenForUser($username,$password); $ctx = new ClientContext($url,$authCtx);
an authenticated client could be instantiated like this:
$ctx = ClientContext::connectWithUserCredentials($url, $username,$password);
and
$authCtx = new AuthenticationContext($url); $authCtx->acquireAppOnlyAccessToken($clientId,$clientSecret); $ctx = new ClientContext($url,$authCtx);
like this:
$ctx = ClientContext::connectWithClientCredentials($url, $clientId,$clientSecret);
Model has been generated from 16.0.19506.12022 version
introduced a support to generate SharePoint model (complex & entity types) from EDMX metadata
Introduced support for generating model from EDMX metadata and an option to set chunked transfer encoding
- introduced a partial support (complex types at the moment) for generating SharePoint model from EDMX metadata
- ability to specify chunked transfer encoding (by @Deuchnord)