To modify a SharePoint List, Folder, Item or File, an X-RequestDigest header must be set for every API request.
Among other things, the SPContextInfo class has a formDigest attribute, exactly for that purpose.
There are two ways to instantiate a SPContextInfo object.
<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
$site->createSPAccessToken();
$site->createSPContextInfo();
$contextInfo = $site->getSPContextInfo();
} catch (SPRuntimeException $e) {
// Handle exceptions
}<?php
require 'vendor/autoload.php';
use Http\Adapter\Guzzle6\Client as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory as MessageFactory;
use Impensavel\Spoil\Exception\SPRuntimeException;
use Impensavel\Spoil\SPContextInfo;
use Impensavel\Spoil\SPSite;
try {
// SharePoint Site configuration
$config = [
// ...
];
// Instantiate SharePoint Site
$site = new SPSite('https://example.sharepoint.com/sites/mySite/', $config, new HttpClient, new MessageFactory);
$site->createSPAccessToken();
$contextInfo = SPContextInfo::create($site);
$site->setSPContextInfo($contextInfo);
} catch (SPRuntimeException $e) {
// Handle exceptions
}Retrieve an array representation of the SPContextInfo object.
Example:
var_dump($contextInfo->toArray());Output:
array(5) {
["library_version"]=>
string(14) "16.0.1234.5678"
["schema_versions"]=>
array(2) {
[0]=>
string(8) "14.0.0.0"
[1]=>
string(8) "15.0.0.0"
}
["form_digest"]=>
string(157) "0x79EAB4CE687BD3DE6B9A87177CC6430759744CDED8C2605..."
["form_digest_expiration"]=>
object(Carbon\Carbon)#26 (3) {
["date"]=>
string(26) "2000-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/London"
}
["extra"]=>
array(0) {
}
}Get the library version number.
Example:
echo $contextInfo->getLibraryVersion();Output:
16.0.1234.5678Get an array with the REST/CSOM schema versions.
Example:
var_dump($contextInfo->getSchemaVersions());Output:
array(2) {
[0]=>
string(8) "14.0.0.0"
[1]=>
string(8) "15.0.0.0"
}Get the Form Digest attribute value.
Example:
echo $contextInfo->getFormDigest();Output:
0x79EAB4CE687BD3DE6B9A87177CC6430759744CDED8C2605...Check if the formDigest attribute has expired.
if ($contextInfo->formDigestHasExpired()) {
// It's time to get a new one
} else {
// Looking good
}Get the expiration date of the formDigest attribute in the form of a Carbon object.
Example:
$carbon = $contextInfo->formDigestExpirationDate();
echo $carbon->diffForHumans();Output:
29 minutes from nowThe SPContextInfo class implements the Serializable interface.
This allows saving the Context Info to use at a later time, avoiding extra requests to the SharePoint API each time something needs doing.
// Serialize the Context Info
$serialized = serialize($contextInfo);
// Store it in a database
// When needed, get it back
// Unserialize the data
$oldContextInfo = unserialize($serialized);
// Check if the `formDigest` attribute is still valid
if ($oldContextInfo->formDigestHasExpired()) {
// Time to get a new Context Info
}
// Do something