phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

Add item to Document Library

Open maromp opened this issue 7 years ago • 1 comments

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()".

maromp avatar May 17 '17 09:05 maromp

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

mwe-code avatar Aug 05 '20 14:08 mwe-code