Skip to content

Commit

Permalink
SharePoint API: File.openBinary method fix, examples updated
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Jul 6, 2022
1 parent 8810e14 commit 778517b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
6 changes: 3 additions & 3 deletions examples/SharePoint/Files/DownloadFileAsContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Office365\SharePoint\ClientContext;


$settings = include('../../Settings.php');
require_once '../vendor/autoload.php';
require_once '../../vendor/autoload.php';
$settings = include('../../../tests/Settings.php');

$sourceFileUrl = rawurlencode("/Shared Documents/SharePoint User Guide.docx");
$sourceFileUrl = "/Shared Documents/SharePoint User Guide.docx";
$targetPath = "../data/SharePoint User Guide.docx";

$appCreds = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
Expand Down
7 changes: 3 additions & 4 deletions examples/SharePoint/Files/DownloadFileAsStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use Office365\SharePoint\ClientContext;
use Office365\SharePoint\File;

$settings = include('../../Settings.php');
require_once '../vendor/autoload.php';
require_once '../../vendor/autoload.php';
$settings = include('../../../tests/Settings.php');

$appCreds = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
$ctx = (new ClientContext($settings['Url']))->withCredentials($appCreds);
Expand All @@ -20,8 +20,7 @@
try {
$fileName = join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $file->getName()]);
$fh = fopen($fileName, 'w+');
$file->download($fh);
$ctx->executeQuery();
$file->download($fh)->executeQuery();
fclose($fh);
print "File {$fileName} has been downloaded successfully\r\n";
} catch (Exception $e) {
Expand Down
4 changes: 2 additions & 2 deletions src/SharePoint/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function recycle()
public static function openBinary(ClientRuntimeContext $ctx, $serverRelativeUrl)
{
$serverRelativeUrl = rawurlencode($serverRelativeUrl);
$url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('{$serverRelativeUrl}')/\$value";
$url = $ctx->getServiceRootUrl() . "/web/getfilebyserverrelativeurl('{$serverRelativeUrl}')/\$value";
$options = new RequestOptions($url);
$options->TransferEncodingChunkedAllowed = true;
$response = $ctx->executeQueryDirect($options);
Expand All @@ -210,7 +210,7 @@ public static function openBinary(ClientRuntimeContext $ctx, $serverRelativeUrl)
public static function saveBinary(ClientRuntimeContext $ctx, $serverRelativeUrl, $content)
{
$serverRelativeUrl = rawurlencode($serverRelativeUrl);
$url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('{$serverRelativeUrl}')/\$value";
$url = $ctx->getServiceRootUrl() . "/web/getfilebyserverrelativeurl('{$serverRelativeUrl}')/\$value";
$request = new RequestOptions($url);
$request->Method = HttpMethod::Post;
$request->ensureHeader('X-HTTP-Method', 'PUT');
Expand Down

0 comments on commit 778517b

Please sign in to comment.