PHP 将 phpSPO 与 Sharepoint 结合使用;如何在子站点中创建文件夹

PHP using phpSPO with Sharepoint ; How to create folder in a subsite

我正在使用 phpSPO 库在 PHP 应用程序中使用 sharePooint。 https://github.com/vgrem/phpSPO

我正在使用以下代码在 Sharepoint 上的共享文档文件夹中成功创建文件夹:

<?php

    spl_autoload_register(function ($classname) {
        $classname = ltrim($classname, "\");
        preg_match('/^(.+)?([^\\]+)$/U', $classname, $match);
        $classname = './'.str_replace("\", "/", $match[1])
            .str_replace(array("\", "_"), "/", $match[2])
            .".php";
        include_once $classname;
    });

    use Office365\Runtime\Auth\ClientCredential;
    use Office365\SharePoint\ClientContext;
    use Office365\SharePoint\Web;

    $credentials = new ClientCredential("MyClientId", "MyClientSecret");
    $ctx = (new ClientContext("https://MyDomain.sharepoint.com"))->withCredentials($credentials);

    $folderName = "TestFolder_" . rand(1, 100000);
    $rootFolder = $ctx->getWeb()->getFolderByServerRelativeUrl("Shared Documents");
    $newFolder = $rootFolder->getFolders()->add($folderName)->executeQuery();
    print($newFolder->getServerRelativeUrl());

?>

这会在 'Shared Documents' 文件夹中创建文件夹。但是,我的客户创建了一个名为 'Enquiries' 的子站点。我不知道如何在子网站的 'Shared Documents' 文件夹中创建文件夹。

我这辈子都找不到根路径。

我不确定我是否遗漏了一些关于 Sharepoint 的基本知识(这是我第一次接触 Sharepoint)

如何找到添加它的路径?

在 phpSPO 框架中是否有另一种方法可以将文件夹添加到特定 site/subsite?

答案(最终)很简单: 更改 ClientContext 以包括站点 Name/Subsite 名称。

在这种情况下,我的子网站名为 'enquiries'。

<?php

    spl_autoload_register(function ($classname) {
        $classname = ltrim($classname, "\");
        preg_match('/^(.+)?([^\\]+)$/U', $classname, $match);
        $classname = './'.str_replace("\", "/", $match[1])
            .str_replace(array("\", "_"), "/", $match[2])
            .".php";
        include_once $classname;
    });


    use Office365\Runtime\Auth\ClientCredential;
    use Office365\SharePoint\ClientContext;
    use Office365\SharePoint\SiteUrl;
    use Office365\SharePoint\Web;

    $credentials = new ClientCredential("MyClientId", "MyClientSecret");
    $ctx = (new ClientContext("https://MyDomain.sharepoint.com/enquiries"))->withCredentials($credentials);


    $folderName = "Spencer_" . rand(1, 100000);
    $rootFolder = $ctx->getWeb()->getFolderByServerRelativeUrl("Shared Documents");
    $newFolder = $rootFolder->getFolders()->add($folderName)->executeQuery();
    print($newFolder->getServerRelativeUrl());

?>

我一直试图从 Root 中找到相对路径,但总是出错。 答案是改变根。