66namespace Office365 \SharePoint ;
77
88use Exception ;
9- use Office365 \Runtime \Actions \InvokeMethodQuery ;
109use Office365 \Runtime \Actions \InvokePostMethodQuery ;
1110use Office365 \Runtime \ClientResult ;
1211use Office365 \Runtime \ClientRuntimeContext ;
1615use Office365 \Runtime \Paths \ServiceOperationPath ;
1716use Office365 \Runtime \ResourcePath ;
1817use Office365 \Runtime \Types \Guid ;
18+ use Office365 \SharePoint \Internal \Paths \FileContentPath ;
1919use Office365 \SharePoint \WebParts \LimitedWebPartManager ;
2020
2121/**
@@ -177,17 +177,25 @@ public function recycle()
177177 $ this ->getContext ()->addQuery ($ qry );
178178 return $ this ;
179179 }
180+
181+
180182 /**
181183 * Opens the file
182- * @param ClientRuntimeContext $ctx
183- * @param $serverRelativeUrl
184+ * @param ClientContext $ctx
185+ * @param string $serverRelativeUrl
186+ * @param bool $usePath
184187 * @return mixed|string
185188 * @throws Exception
186189 */
187- public static function openBinary (ClientRuntimeContext $ ctx , $ serverRelativeUrl )
190+ public static function openBinary (ClientRuntimeContext $ ctx , $ serverRelativeUrl, $ usePath = true )
188191 {
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 ();
191199 $ options = new RequestOptions ($ url );
192200 $ options ->TransferEncodingChunkedAllowed = true ;
193201 $ response = $ ctx ->executeQueryDirect ($ options );
@@ -199,15 +207,21 @@ public static function openBinary(ClientRuntimeContext $ctx, $serverRelativeUrl)
199207 /**
200208 * Saves the file
201209 * 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
203211 * @param string $serverRelativeUrl
204212 * @param string $content file content
213+ * @param bool $usePath
205214 * @throws Exception
206215 */
207- public static function saveBinary (ClientRuntimeContext $ ctx , $ serverRelativeUrl , $ content )
216+ public static function saveBinary (ClientRuntimeContext $ ctx , $ serverRelativeUrl , $ content, $ usePath = true )
208217 {
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 ();
211225 $ request = new RequestOptions ($ url );
212226 $ request ->Method = HttpMethod::Post;
213227 $ request ->ensureHeader ('X-HTTP-Method ' , 'PUT ' );
@@ -259,7 +273,8 @@ public function getCheckOutType()
259273 */
260274 public function getListItemAllFields ()
261275 {
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 ())));
263278 }
264279 /**
265280 * Starts a new chunk upload session and uploads the first fragment
@@ -298,18 +313,23 @@ public function continueUpload($uploadId, $fileOffset, $content)
298313 */
299314 public function finishUpload ($ uploadId , $ fileOffset , $ content )
300315 {
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 );
302318 $ this ->getContext ()->addQueryAndResultObject ($ qry , $ this );
303319 return $ this ;
304320 }
305321 function setProperty ($ name , $ value , $ persistChanges = true )
306322 {
307323 parent ::setProperty ($ name , $ value , $ persistChanges );
308324 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 )) {
311328 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 ();
313333 }
314334 }
315335 return $ this ;
@@ -889,7 +909,9 @@ public function getModifiedBy()
889909 */
890910 public function getEffectiveInformationRightsManagementSettings ()
891911 {
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 ())));
893915 }
894916 /**
895917 * @return Web|null
0 commit comments