I'm working with phpSPO library for work with sharePooint in a PHP app. https://github.com/vgrem/phpSPO
For example, I retrieve the files of an specific folder
try {
$authCtx = new AuthenticationContext($Settings['Url']);
$authCtx->acquireTokenForUser($Settings['UserName'],$Settings['Password']);
// conecction successfull
$folderUrl = "/sites/mySite/Tfts/eiic";
$url = $Settings['Url'] . "/_api/web/getFolderByServerRelativeUrl('{$folderUrl}')/Files";
$request = new RequestOptions($url);
$ctx = new ClientContext($url,$authCtx);
$data = $ctx->executeQueryDirect($request);
// HOW to Convert $data to FolderItem or Folder class ??
}
catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "
";
}
In the example above, $data is a JSON with the folder files but, How can I convert this JSON to a File or FileCollection for working with its properties?
Another Question, How to Upload a file to a specific folder ??
Thank u very much !
I would suggest to target entities such as File
and Folder
when dealing with SharePoint resources.
In that case the example for getting files under a specific folder could be converted to:
$files = $ctx->getWeb()->getFolderByServerRelativeUrl($parentFolderUrl)->getFiles();
$ctx->load($files);
$ctx->executeQuery();
//print files info
foreach ($files->getData() as $file) {
print "File name: '{$file->getProperty("ServerRelativeUrl")}'
";
}
Let's say your site has the following structure:
Documents (library)
|
--- Archive (folder)
|
--- 2001 (folder)
then the below example demonstrates how to upload file into 2001
sub folder:
$targetFolderUrl = "/Documents/Archive/2007";
$localPath = "./User Guide.docx";
$fileName = basename($localPath);
$fileCreationInformation = new FileCreationInformation();
$fileCreationInformation->Content = file_get_contents($localPath);
$fileCreationInformation->Url = $fileName;
$uploadFile = $ctx->getWeb()->getFolderByServerRelativeUrl($targetFolderUrl)->getFiles()->add($fileCreationInformation);
$ctx->executeQuery();