vgrem / phpSPO

Microsoft 365 Library for PHP.
MIT License
360 stars 117 forks source link

upload file to document library and add properties #308

Closed wanchankein closed 1 year ago

wanchankein commented 2 years ago

Hello, I need to upload a file to a sharepoint document library and set text properties like emailFrom, EmailTo, Language... I've read all the codes but i dont' know how to upload the file and set this properties.

The document library is named "ColaEnvios" i want to upload pdf files and set this properties: emailFrom, EmailTo, Language

This is the code i use. It works and uploads all the pdf files, but i don't know how to add the properties or if this is not the way, please let me know how to do it.

try {
    $credentials = new UserCredentials($user, $pass);
    $ctx = (new ClientContext($url))->withCredentials($credentials);

    $localPath = "/archivos/";
    $targetLibraryTitle = "ColaEnvios";
    $targetList = $ctx->getWeb()->getLists()->getByTitle($targetLibraryTitle);

    $searchPrefix = $localPath . '*.pdf';

    foreach( glob($searchPrefix ) as $filename) {

        $uploadFile = $targetList->getRootFolder()->uploadFile(basename($filename),file_get_contents($filename));
        $ctx->executeQuery();
    }

} catch (Exception $e) {
    echo 'Error: ',  $e->getMessage(), "\n".PHP_EOL;
}
julianlangweb commented 1 year ago

struggeling with this aswell

vgrem commented 1 year ago

The example demonsrates how to upload file and set its properties:

$localPath =  "../../data/SharePoint User Guide.docx";
$libraryTitle = "Documents";
$lib = $ctx->getWeb()->getLists()->getByTitle($libraryTitle);
$uploadFile = $lib->getRootFolder()->uploadFile(basename($localPath),file_get_contents($localPath))->executeQuery();
$listItem = $uploadFile->getListItemAllFields();
$listItem->setProperty("Title", "Uploaded")->update()->executeQuery(); // update file metadata

where