使用SharePoint phpSPO库处理文件夹

时间:2017-03-27 13:34:28

标签: php sharepoint

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

例如,我检索特定文件夹的文件

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(), "\n";
}

在上面的示例中,$ data是带有文件夹文件的JSON,但是,如何将此JSON转换为File或FileCollection以使用其属性?

另一个问题,如何将文件上传到特定文件夹?

非常感谢你!

1 个答案:

答案 0 :(得分:0)

我建议在处理SharePoint资源时定位FileFolder等实体。

在这种情况下,获取特定文件夹下的文件的示例可以转换为:

$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")}'\r\n";
}

假设您的网站具有以下结构:

Documents (library) 
|
---   Archive (folder)
      |
      --- 2001 (folder)      

然后下面的示例演示了如何将文件上传到2001子文件夹:

$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();