Skip to content

Commit fccca6c

Browse files
committed
SharePoint API: download file improvements
1 parent 95ea13e commit fccca6c

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
4+
use Office365\Runtime\Auth\ClientCredential;
5+
use Office365\SharePoint\ClientContext;
6+
use Office365\SharePoint\File;
7+
8+
9+
require_once '../../vendor/autoload.php';
10+
$settings = include('../../../tests/Settings.php');
11+
12+
$creds = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
13+
$ctx = (new ClientContext($settings['TeamSiteUrl']))->withCredentials($creds);
14+
15+
$lib_title = "Documents";
16+
$lib = $ctx->getWeb()->getLists()->getByTitle($lib_title);
17+
$folder = $lib->getRootFolder()->expand("Files")->get()->executeQuery();
18+
19+
/** @var File $file */
20+
foreach ($folder->getFiles() as $file) {
21+
try {
22+
$localPath = join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $file->getName()]);
23+
$fh = fopen($localPath, 'w+');
24+
$file->download($fh)->executeQuery();
25+
fclose($fh);
26+
print "File: {$file->getServerRedirectedUrl()} has been downloaded into {$localPath}\r\n";
27+
} catch (\Throwable $th) {
28+
print "Error {$th->getCode()} - File download failed: {$th->getMessage()}";
29+
}
30+
}
31+
32+
33+

src/SharePoint/ChangeItem.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ public function setActivityType($value)
4444
*/
4545
public function getContentTypeId()
4646
{
47-
if (!$this->isPropertyAvailable("ContentTypeId")) {
48-
return null;
49-
}
5047
return $this->getProperty("ContentTypeId");
5148
}
5249
/**
@@ -67,9 +64,6 @@ public function setContentTypeId($value)
6764
*/
6865
public function getEditor()
6966
{
70-
if (!$this->isPropertyAvailable("Editor")) {
71-
return null;
72-
}
7367
return $this->getProperty("Editor");
7468
}
7569
/**
@@ -88,9 +82,6 @@ public function setEditor($value)
8882
*/
8983
public function getEditorEmailHint()
9084
{
91-
if (!$this->isPropertyAvailable("EditorEmailHint")) {
92-
return null;
93-
}
9485
return $this->getProperty("EditorEmailHint");
9586
}
9687
/**
@@ -109,9 +100,6 @@ public function setEditorEmailHint($value)
109100
*/
110101
public function getEditorLoginName()
111102
{
112-
if (!$this->isPropertyAvailable("EditorLoginName")) {
113-
return null;
114-
}
115103
return $this->getProperty("EditorLoginName");
116104
}
117105
/**
@@ -131,9 +119,6 @@ public function setEditorLoginName($value)
131119
*/
132120
public function getFileSystemObjectType()
133121
{
134-
if (!$this->isPropertyAvailable("FileSystemObjectType")) {
135-
return null;
136-
}
137122
return $this->getProperty("FileSystemObjectType");
138123
}
139124
/**
@@ -154,9 +139,6 @@ public function setFileSystemObjectType($value)
154139
*/
155140
public function getFileType()
156141
{
157-
if (!$this->isPropertyAvailable("FileType")) {
158-
return null;
159-
}
160142
return $this->getProperty("FileType");
161143
}
162144
/**
@@ -194,9 +176,6 @@ public function setHashtag($value)
194176
*/
195177
public function getHidden()
196178
{
197-
if (!$this->isPropertyAvailable("Hidden")) {
198-
return null;
199-
}
200179
return $this->getProperty("Hidden");
201180
}
202181
/**
@@ -219,9 +198,6 @@ public function setHidden($value)
219198
*/
220199
public function getItemId()
221200
{
222-
if (!$this->isPropertyAvailable("ItemId")) {
223-
return null;
224-
}
225201
return $this->getProperty("ItemId");
226202
}
227203
/**
@@ -249,9 +225,6 @@ public function setItemId($value)
249225
*/
250226
public function getListId()
251227
{
252-
if (!$this->isPropertyAvailable("ListId")) {
253-
return null;
254-
}
255228
return $this->getProperty("ListId");
256229
}
257230
/**
@@ -277,9 +250,6 @@ public function setListId($value)
277250
*/
278251
public function getListTemplate()
279252
{
280-
if (!$this->isPropertyAvailable("ListTemplate")) {
281-
return null;
282-
}
283253
return $this->getProperty("ListTemplate");
284254
}
285255
/**
@@ -299,9 +269,6 @@ public function setListTemplate($value)
299269
*/
300270
public function getListTitle()
301271
{
302-
if (!$this->isPropertyAvailable("ListTitle")) {
303-
return null;
304-
}
305272
return $this->getProperty("ListTitle");
306273
}
307274
/**
@@ -321,9 +288,6 @@ public function setListTitle($value)
321288
*/
322289
public function getServerRelativeUrl()
323290
{
324-
if (!$this->isPropertyAvailable("ServerRelativeUrl")) {
325-
return null;
326-
}
327291
return $this->getProperty("ServerRelativeUrl");
328292
}
329293
/**

src/SharePoint/File.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,21 @@ public static function fromUrl($absUrl)
5454
public function download($handle)
5555
{
5656
$this->ensureProperty("ServerRelativeUrl", function () use($handle) {
57-
$this->constructDownloadQuery($this->getServerRelativeUrl(), $handle);
57+
$this->constructDownloadQuery($this, $handle);
5858
});
5959
return $this;
6060
}
6161
/**
62-
* @param string $url
62+
* @param File $file
6363
* @param resource $handle
64-
* @return InvokeMethodQuery
6564
*/
66-
private function constructDownloadQuery($url, $handle)
65+
private function constructDownloadQuery($file, $handle)
6766
{
68-
$url = rawurlencode($url);
69-
$qry = new InvokeMethodQuery($this->getParentWeb(), "getFileByServerRelativeUrl('{$url}')/\$value");
70-
$this->getContext()->addQuery($qry);
67+
$file->getContext()->load($file);
7168
$this->getContext()->getPendingRequest()->beforeExecuteRequestOnce(function ($request) use($handle) {
69+
$request->Url .= "/\$value";
7270
$request->StreamHandle = $handle;
7371
});
74-
return $qry;
7572
}
7673
/**
7774
* Checks out the file from a document library based on the check-out type.

src/SharePoint/SPResourcePath.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public function __construct($decodedUrl=null)
1717
parent::__construct();
1818
}
1919

20+
public function __toString()
21+
{
22+
return $this->DecodedUrl;
23+
}
24+
2025
/**
2126
* @var string
2227
*/

0 commit comments

Comments
 (0)