diff --git a/examples/SharePoint/Files/DownloadFiles.php b/examples/SharePoint/Files/DownloadFiles.php new file mode 100644 index 00000000..a5cc7e61 --- /dev/null +++ b/examples/SharePoint/Files/DownloadFiles.php @@ -0,0 +1,33 @@ +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()}"; + } +} + + + diff --git a/src/SharePoint/ChangeItem.php b/src/SharePoint/ChangeItem.php index 27fef9ef..6197026f 100644 --- a/src/SharePoint/ChangeItem.php +++ b/src/SharePoint/ChangeItem.php @@ -44,9 +44,6 @@ public function setActivityType($value) */ public function getContentTypeId() { - if (!$this->isPropertyAvailable("ContentTypeId")) { - return null; - } return $this->getProperty("ContentTypeId"); } /** @@ -67,9 +64,6 @@ public function setContentTypeId($value) */ public function getEditor() { - if (!$this->isPropertyAvailable("Editor")) { - return null; - } return $this->getProperty("Editor"); } /** @@ -88,9 +82,6 @@ public function setEditor($value) */ public function getEditorEmailHint() { - if (!$this->isPropertyAvailable("EditorEmailHint")) { - return null; - } return $this->getProperty("EditorEmailHint"); } /** @@ -109,9 +100,6 @@ public function setEditorEmailHint($value) */ public function getEditorLoginName() { - if (!$this->isPropertyAvailable("EditorLoginName")) { - return null; - } return $this->getProperty("EditorLoginName"); } /** @@ -131,9 +119,6 @@ public function setEditorLoginName($value) */ public function getFileSystemObjectType() { - if (!$this->isPropertyAvailable("FileSystemObjectType")) { - return null; - } return $this->getProperty("FileSystemObjectType"); } /** @@ -154,9 +139,6 @@ public function setFileSystemObjectType($value) */ public function getFileType() { - if (!$this->isPropertyAvailable("FileType")) { - return null; - } return $this->getProperty("FileType"); } /** @@ -194,9 +176,6 @@ public function setHashtag($value) */ public function getHidden() { - if (!$this->isPropertyAvailable("Hidden")) { - return null; - } return $this->getProperty("Hidden"); } /** @@ -219,9 +198,6 @@ public function setHidden($value) */ public function getItemId() { - if (!$this->isPropertyAvailable("ItemId")) { - return null; - } return $this->getProperty("ItemId"); } /** @@ -249,9 +225,6 @@ public function setItemId($value) */ public function getListId() { - if (!$this->isPropertyAvailable("ListId")) { - return null; - } return $this->getProperty("ListId"); } /** @@ -277,9 +250,6 @@ public function setListId($value) */ public function getListTemplate() { - if (!$this->isPropertyAvailable("ListTemplate")) { - return null; - } return $this->getProperty("ListTemplate"); } /** @@ -299,9 +269,6 @@ public function setListTemplate($value) */ public function getListTitle() { - if (!$this->isPropertyAvailable("ListTitle")) { - return null; - } return $this->getProperty("ListTitle"); } /** @@ -321,9 +288,6 @@ public function setListTitle($value) */ public function getServerRelativeUrl() { - if (!$this->isPropertyAvailable("ServerRelativeUrl")) { - return null; - } return $this->getProperty("ServerRelativeUrl"); } /** diff --git a/src/SharePoint/File.php b/src/SharePoint/File.php index aa6d2af6..a83b923b 100644 --- a/src/SharePoint/File.php +++ b/src/SharePoint/File.php @@ -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. diff --git a/src/SharePoint/SPResourcePath.php b/src/SharePoint/SPResourcePath.php index 90809eb5..5d1d4be2 100644 --- a/src/SharePoint/SPResourcePath.php +++ b/src/SharePoint/SPResourcePath.php @@ -17,6 +17,11 @@ public function __construct($decodedUrl=null) parent::__construct(); } + public function __toString() + { + return $this->DecodedUrl; + } + /** * @var string */