phpSPO
phpSPO copied to clipboard
How can i get a specific file by file name?
I generate a word docx and upload to sharepoint, which is working amazing.
After that, I would like to get this uploaded docx. Is there any way to do this? Please help.
At the moment I have to get all files in the folder and loop through to find the docx I want...
If you know the path and filename, you could do this:
$ctx = ClientContext::connectWithClientCredentials($url, $client_id, $client_secret); // connect
$server_relative_path = " /sites/Folder/SubFolder/Filename.docx"; // the file you want to get
$targetFile = $ctx->getWeb()->getFileByServerRelativePath(new SPResourcePath($server_relative_path)); // get the file
$targetFile->getUniqueId(); // do something with the file
Not sure if that helps, hope it does.
the things you can do with a File
If you know the path and filename, you could do this:
$ctx = ClientContext::connectWithClientCredentials($url, $client_id, $client_secret); // connect $server_relative_path = " /sites/Folder/SubFolder/Filename.docx"; // the file you want to get $targetFile = $ctx->getWeb()->getFileByServerRelativePath(new SPResourcePath($server_relative_path)); // get the file $targetFile->getUniqueId(); // do something with the file
Not sure if that helps, hope it does.
the things you can do with a File
Thanks for the reply. Unfortunately I can't make it work. Also I tried getFileByServerRelativeUrl
without any luck :(
@Rain-YuXia Try this
$ctx = ClientContext::connectWithClientCredentials($url, $client_id, $client_secret); // connect
$server_relative_path = " /sites/Folder/SubFolder/Filename.docx"; // the file you want to get
$targetFile = $ctx->getWeb()->getFileByServerRelativeUrl($server_relative_path); // get the file
$ctx->load($targetFile);
$ctx->executeQuery();
$targetFile->getUniqueId(); // do something with the file
You missed $ctx->load($targetFile)
and $ctx->executeQuery()