6
6
namespace Office365 \SharePoint ;
7
7
8
8
use Exception ;
9
- use Office365 \Runtime \Actions \InvokeMethodQuery ;
10
9
use Office365 \Runtime \Actions \InvokePostMethodQuery ;
11
10
use Office365 \Runtime \ClientResult ;
12
11
use Office365 \Runtime \ClientRuntimeContext ;
16
15
use Office365 \Runtime \Paths \ServiceOperationPath ;
17
16
use Office365 \Runtime \ResourcePath ;
18
17
use Office365 \Runtime \Types \Guid ;
18
+ use Office365 \SharePoint \Internal \Paths \FileContentPath ;
19
19
use Office365 \SharePoint \WebParts \LimitedWebPartManager ;
20
20
21
21
/**
@@ -177,17 +177,25 @@ public function recycle()
177
177
$ this ->getContext ()->addQuery ($ qry );
178
178
return $ this ;
179
179
}
180
+
181
+
180
182
/**
181
183
* Opens the file
182
- * @param ClientRuntimeContext $ctx
183
- * @param $serverRelativeUrl
184
+ * @param ClientContext $ctx
185
+ * @param string $serverRelativeUrl
186
+ * @param bool $usePath
184
187
* @return mixed|string
185
188
* @throws Exception
186
189
*/
187
- public static function openBinary (ClientRuntimeContext $ ctx , $ serverRelativeUrl )
190
+ public static function openBinary (ClientRuntimeContext $ ctx , $ serverRelativeUrl, $ usePath = true )
188
191
{
189
- $ serverRelativeUrl = rawurlencode ($ serverRelativeUrl );
190
- $ url = $ ctx ->getServiceRootUrl () . "/web/getfilebyserverrelativeurl(' {$ serverRelativeUrl }')/ \$value " ;
192
+ $ file = new File ($ ctx );
193
+ if ($ usePath )
194
+ $ file ->setProperty ("ServerRelativePath " ,new SPResourcePath ($ serverRelativeUrl ));
195
+ else
196
+ $ file ->setProperty ("ServerRelativeUrl " ,$ serverRelativeUrl );
197
+ $ contentPath = new FileContentPath ($ file ->getResourcePath ());
198
+ $ url = $ ctx ->getServiceRootUrl () . $ contentPath ->toUrl ();
191
199
$ options = new RequestOptions ($ url );
192
200
$ options ->TransferEncodingChunkedAllowed = true ;
193
201
$ response = $ ctx ->executeQueryDirect ($ options );
@@ -199,15 +207,21 @@ public static function openBinary(ClientRuntimeContext $ctx, $serverRelativeUrl)
199
207
/**
200
208
* Saves the file
201
209
* Note: it is supported to update the existing file only. For adding a new file see FileCollection.add method
202
- * @param ClientRuntimeContext $ctx
210
+ * @param ClientContext $ctx
203
211
* @param string $serverRelativeUrl
204
212
* @param string $content file content
213
+ * @param bool $usePath
205
214
* @throws Exception
206
215
*/
207
- public static function saveBinary (ClientRuntimeContext $ ctx , $ serverRelativeUrl , $ content )
216
+ public static function saveBinary (ClientRuntimeContext $ ctx , $ serverRelativeUrl , $ content, $ usePath = true )
208
217
{
209
- $ serverRelativeUrl = rawurlencode ($ serverRelativeUrl );
210
- $ url = $ ctx ->getServiceRootUrl () . "/web/getfilebyserverrelativeurl(' {$ serverRelativeUrl }')/ \$value " ;
218
+ $ file = new File ($ ctx );
219
+ if ($ usePath )
220
+ $ file ->setProperty ("ServerRelativePath " ,new SPResourcePath ($ serverRelativeUrl ));
221
+ else
222
+ $ file ->setProperty ("ServerRelativeUrl " ,$ serverRelativeUrl );
223
+ $ contentPath = new FileContentPath ($ file ->getResourcePath ());
224
+ $ url = $ ctx ->getServiceRootUrl () . $ contentPath ->toUrl ();
211
225
$ request = new RequestOptions ($ url );
212
226
$ request ->Method = HttpMethod::Post;
213
227
$ request ->ensureHeader ('X-HTTP-Method ' , 'PUT ' );
@@ -259,7 +273,8 @@ public function getCheckOutType()
259
273
*/
260
274
public function getListItemAllFields ()
261
275
{
262
- return $ this ->getProperty ("ListItemAllFields " , new ListItem ($ this ->getContext (), new ResourcePath ("ListItemAllFields " , $ this ->getResourcePath ())));
276
+ return $ this ->getProperty ("ListItemAllFields " ,
277
+ new ListItem ($ this ->getContext (), new ResourcePath ("ListItemAllFields " , $ this ->getResourcePath ())));
263
278
}
264
279
/**
265
280
* Starts a new chunk upload session and uploads the first fragment
@@ -298,18 +313,23 @@ public function continueUpload($uploadId, $fileOffset, $content)
298
313
*/
299
314
public function finishUpload ($ uploadId , $ fileOffset , $ content )
300
315
{
301
- $ qry = new InvokePostMethodQuery ($ this , "finishupload " , array ('uploadId ' => $ uploadId ->toString (), 'fileOffset ' => $ fileOffset ), null , $ content );
316
+ $ qry = new InvokePostMethodQuery ($ this , "finishupload " ,
317
+ array ('uploadId ' => $ uploadId ->toString (), 'fileOffset ' => $ fileOffset ), null , $ content );
302
318
$ this ->getContext ()->addQueryAndResultObject ($ qry , $ this );
303
319
return $ this ;
304
320
}
305
321
function setProperty ($ name , $ value , $ persistChanges = true )
306
322
{
307
323
parent ::setProperty ($ name , $ value , $ persistChanges );
308
324
if ($ name === "UniqueId " ) {
309
- $ this ->resourcePath = new ResourcePath ("GetFileById(guid' {$ value }') " , new ResourcePath ("Web " ));
310
- } else {
325
+ $ this ->resourcePath = $ this ->getParentWeb ()->getFileById ($ value )->getResourcePath ();
326
+ }
327
+ if (is_null ($ this ->resourcePath )) {
311
328
if ($ name === "ServerRelativeUrl " ) {
312
- $ this ->resourcePath = new ResourcePath ("GetFileByServerRelativeUrl(' {$ value }') " , new ResourcePath ("Web " ));
329
+ $ this ->resourcePath = $ this ->getParentWeb ()->getFileByServerRelativeUrl ($ value )->getResourcePath ();
330
+ }
331
+ elseif ($ name === "ServerRelativePath " ) {
332
+ $ this ->resourcePath = $ this ->getParentWeb ()->getFileByServerRelativePath ($ value )->getResourcePath ();
313
333
}
314
334
}
315
335
return $ this ;
@@ -889,7 +909,9 @@ public function getModifiedBy()
889
909
*/
890
910
public function getEffectiveInformationRightsManagementSettings ()
891
911
{
892
- return $ this ->getProperty ("EffectiveInformationRightsManagementSettings " , new EffectiveInformationRightsManagementSettings ($ this ->getContext (), new ResourcePath ("EffectiveInformationRightsManagementSettings " , $ this ->getResourcePath ())));
912
+ return $ this ->getProperty ("EffectiveInformationRightsManagementSettings " ,
913
+ new EffectiveInformationRightsManagementSettings ($ this ->getContext (),
914
+ new ResourcePath ("EffectiveInformationRightsManagementSettings " , $ this ->getResourcePath ())));
893
915
}
894
916
/**
895
917
* @return Web|null
0 commit comments