douaonong7807 2017-03-27 13:34
浏览 60

使用SharePoint phpSPO库处理文件夹

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 !

  • 写回答

1条回答 默认 最新

  • doujing2017 2017-04-16 12:55
    关注

    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();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线