Skip to content

Commit

Permalink
SharePoint API: download file improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Jul 7, 2022
1 parent 95ea13e commit fccca6c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 44 deletions.
33 changes: 33 additions & 0 deletions examples/SharePoint/Files/DownloadFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ClientContext;
use Office365\SharePoint\File;


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

$creds = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
$ctx = (new ClientContext($settings['TeamSiteUrl']))->withCredentials($creds);

$lib_title = "Documents";
$lib = $ctx->getWeb()->getLists()->getByTitle($lib_title);
$folder = $lib->getRootFolder()->expand("Files")->get()->executeQuery();

/** @var File $file */
foreach ($folder->getFiles() as $file) {
try {
$localPath = join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $file->getName()]);
$fh = fopen($localPath, 'w+');
$file->download($fh)->executeQuery();
fclose($fh);
print "File: {$file->getServerRedirectedUrl()} has been downloaded into {$localPath}\r\n";
} catch (\Throwable $th) {
print "Error {$th->getCode()} - File download failed: {$th->getMessage()}";
}
}



36 changes: 0 additions & 36 deletions src/SharePoint/ChangeItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public function setActivityType($value)
*/
public function getContentTypeId()
{
if (!$this->isPropertyAvailable("ContentTypeId")) {
return null;
}
return $this->getProperty("ContentTypeId");
}
/**
Expand All @@ -67,9 +64,6 @@ public function setContentTypeId($value)
*/
public function getEditor()
{
if (!$this->isPropertyAvailable("Editor")) {
return null;
}
return $this->getProperty("Editor");
}
/**
Expand All @@ -88,9 +82,6 @@ public function setEditor($value)
*/
public function getEditorEmailHint()
{
if (!$this->isPropertyAvailable("EditorEmailHint")) {
return null;
}
return $this->getProperty("EditorEmailHint");
}
/**
Expand All @@ -109,9 +100,6 @@ public function setEditorEmailHint($value)
*/
public function getEditorLoginName()
{
if (!$this->isPropertyAvailable("EditorLoginName")) {
return null;
}
return $this->getProperty("EditorLoginName");
}
/**
Expand All @@ -131,9 +119,6 @@ public function setEditorLoginName($value)
*/
public function getFileSystemObjectType()
{
if (!$this->isPropertyAvailable("FileSystemObjectType")) {
return null;
}
return $this->getProperty("FileSystemObjectType");
}
/**
Expand All @@ -154,9 +139,6 @@ public function setFileSystemObjectType($value)
*/
public function getFileType()
{
if (!$this->isPropertyAvailable("FileType")) {
return null;
}
return $this->getProperty("FileType");
}
/**
Expand Down Expand Up @@ -194,9 +176,6 @@ public function setHashtag($value)
*/
public function getHidden()
{
if (!$this->isPropertyAvailable("Hidden")) {
return null;
}
return $this->getProperty("Hidden");
}
/**
Expand All @@ -219,9 +198,6 @@ public function setHidden($value)
*/
public function getItemId()
{
if (!$this->isPropertyAvailable("ItemId")) {
return null;
}
return $this->getProperty("ItemId");
}
/**
Expand Down Expand Up @@ -249,9 +225,6 @@ public function setItemId($value)
*/
public function getListId()
{
if (!$this->isPropertyAvailable("ListId")) {
return null;
}
return $this->getProperty("ListId");
}
/**
Expand All @@ -277,9 +250,6 @@ public function setListId($value)
*/
public function getListTemplate()
{
if (!$this->isPropertyAvailable("ListTemplate")) {
return null;
}
return $this->getProperty("ListTemplate");
}
/**
Expand All @@ -299,9 +269,6 @@ public function setListTemplate($value)
*/
public function getListTitle()
{
if (!$this->isPropertyAvailable("ListTitle")) {
return null;
}
return $this->getProperty("ListTitle");
}
/**
Expand All @@ -321,9 +288,6 @@ public function setListTitle($value)
*/
public function getServerRelativeUrl()
{
if (!$this->isPropertyAvailable("ServerRelativeUrl")) {
return null;
}
return $this->getProperty("ServerRelativeUrl");
}
/**
Expand Down
13 changes: 5 additions & 8 deletions src/SharePoint/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,21 @@ public static function fromUrl($absUrl)
public function download($handle)
{
$this->ensureProperty("ServerRelativeUrl", function () use($handle) {
$this->constructDownloadQuery($this->getServerRelativeUrl(), $handle);
$this->constructDownloadQuery($this, $handle);
});
return $this;
}
/**
* @param string $url
* @param File $file
* @param resource $handle
* @return InvokeMethodQuery
*/
private function constructDownloadQuery($url, $handle)
private function constructDownloadQuery($file, $handle)
{
$url = rawurlencode($url);
$qry = new InvokeMethodQuery($this->getParentWeb(), "getFileByServerRelativeUrl('{$url}')/\$value");
$this->getContext()->addQuery($qry);
$file->getContext()->load($file);
$this->getContext()->getPendingRequest()->beforeExecuteRequestOnce(function ($request) use($handle) {
$request->Url .= "/\$value";
$request->StreamHandle = $handle;
});
return $qry;
}
/**
* Checks out the file from a document library based on the check-out type.
Expand Down
5 changes: 5 additions & 0 deletions src/SharePoint/SPResourcePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function __construct($decodedUrl=null)
parent::__construct();
}

public function __toString()
{
return $this->DecodedUrl;
}

/**
* @var string
*/
Expand Down

0 comments on commit fccca6c

Please sign in to comment.