phpSPO
phpSPO copied to clipboard
Add item to Document Library
i have a document library as list. how can i to add new row to document library?
i tried this:
$web = $this->connect->getWeb(); $conn = $this->connect; $list = $web->getLists()->getByTitle($list); $itemProperties = $item; $item = $list->addItem($itemProperties); $conn->executeQuery();
but its fail, "use SPFileCollection.Add()".
You need to get the FileCollection of your list and add your file with your FileCreationInformation.
Something like this:
// ADD FILECREATION INFO
$fileCreationInformation = new FileCreationInformation();
$fileCreationInformation->Url = $fileName;
$fileCreationInformation->Content = $fileContent;
$fileCreationInformation->Overwrite = $overwriteFlag;
// ADD FILE
$file = $list->getRootFolder()->getFiles()->add($fileCreationInformation);
As soon as you added a file to your document library (a new row basically) you can add your item properties (columns defined in the document library list view).
Therefore you need to get all fields of that list item (file).
$listItem = $file->getListItemAllFields();
// ADD LISTITEM PROPERTIES AND UPDATE LISTITEM
foreach($itemProperties as $propertyName => $itemProperty) {
$listItem->setProperty($propertyName, $itemProperty, true);
}
$listItem->update();
$conn->executeQuery();