skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

    $siteUrl = "https://<subdomain>.sharepoint.com/sites/office";
    $credentials = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
    $ctx = (new ClientContext($siteUrl))->withCredentials($credentials);
    

    I hope this answer helps others


  2. 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.

     <?php
    
    use Office365SharePointFile;
    use Office365SharePointFolder;
    
    function forEachFile(Folder $parentFolder, $recursive, callable $action, $level = 0)
    {
        try {
            $files = $parentFolder->getFiles()->get()->executeQuery();
            foreach ($files as $file) {
                $action($file, $level);
            }
    
            if ($recursive) {
                $folders = $parentFolder->getFolders()->get()->executeQuery();
                foreach ($folders as $folder) {
                    forEachFile($folder, $recursive, $action, $level + 1);
                }
            }
        } catch (Exception $e) {
            echo "Error accessing folder: " . $e->getMessage();
        }
    }
    
    $parentFolderUrl = "/sites/office/Shared Documents"; // Adjust the path here
    
    try {
        $rootFolder = $ctx->getWeb()->getFolderByServerRelativeUrl($parentFolderUrl);
        $ctx->load($rootFolder);
        $ctx->executeQuery();
    
        forEachFile($rootFolder, true, function (File $file, $level) {
            echo $level, ":", $file->getServerRelativeUrl(), "<br>", PHP_EOL;
        });
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
    }
    

    Please try and let me know.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search