I’m trying to use phpSPO to get the files from our sharepoint ‘My Sites’.
Getting the ‘Shared Documents’ is working with getFolderByServerRelativeUrl("Shared Documents"),
but when I try to use getFolderByServerRelativeUrl("sites/office") it is not retrieving any files/folders.
Here is a code sample I am using:
$parentFolderUrl="Shared Documents"; => this works
// $parentFolderUrl="sites/office"; => this does not work
// $parentFolderUrl="/sites/office"; => this does not work
function forEachFile(Folder $parentFolder, $recursive, callable $action, $level=0)
{
$files = $parentFolder->getFiles()->get()->executeQuery();
/** @var File $file */
foreach ($files as $file) {
$action($file, $level);
}
if ($recursive) {
/** @var Folder $folder */
$folders = $parentFolder->getFolders()->get()->executeQuery();
// foreach ($parentFolder->getFolders() as $folder) {
foreach ($folders as $folder) {
forEachFile($folder, $recursive, $action, $level++);
}
}
}
$rootFolder = $ctx->getWeb()->getFolderByServerRelativeUrl($parentFolderUrl);
forEachFile($rootFolder, true, function (File $file,$level){
echo $level, ":", $file->getServerRelativeUrl(), "<br>", PHP_EOL;
});
What am I doing wrong?
Thanks
2
Answers
I hate to answer my own question, but the reason was the initial connection.
After changing the url from 'https://.sharepoint.com' to 'https://.sharepoint.com/sites/office', I was able to display the files.
I hope this answer helps others
below is a revised version of your code with added logging and proper path handling:
The issue you’re facing likely stems from how the server-relative URL is being specified. The problem could also be related to permissions or incorrect folder paths.
Please try and let me know.