phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

Add attachments to the item after item updating

Open ztdan4ik opened this issue 2 years ago • 3 comments

Hi there,

Have an issue with attachments

Here is my sample:

...
$list = $client->getWeb()->getLists()->getByTitle("Title");
$items = $list->getItems()
              ->expand('AttachmentFiles,FieldValuesAsText');
$client->load($this->items);
$client->executeQuery();

$item = $items->getItem(0);
$item->setProperty('Title', 'Update 2');
$item->update();
$item->executeQuery();

$attCreationInformation = new AttachmentCreationInformation();
$attCreationInformation->setProperty('FileName', $values['value']['fileName']);
$attCreationInformation->setProperty('ContentStream', $values['value']['content']);

$item->getAttachmentFiles()->add($attCreationInformation);
$item->executeQuery();

After executing this I getting error from Share Point "Cannot find resource for the request AttachmentFiles."

I did some research and found that for AttachmentCollection->resourcePath->parrent is missing and thats why url looks like this: https://domain.sharepoint.com/sites/Site-Name/_api/AttachmentFiles/add(FileName='document.docx') The part web/lists/GetByTitle('ListName')/items(1) is missing

What I notice that If I don't expand with AttachmentFiles - all works fine.

Did I use it in wrong way? Or there is an issue with it?

Thanks in advance!

ztdan4ik avatar Feb 15 '22 20:02 ztdan4ik

Greetings, thank you for catching it and providing the detailed analysis!

Regarding:

I did some research and found that for AttachmentCollection->resourcePath->parrent is missing and thats why url looks like this: https://domain.sharepoint.com/sites/Site-Name/_api/AttachmentFiles/add(FileName='document.docx') The part web/lists/GetByTitle('ListName')/items(1) is missing

exactly the root cause here and apparently the bug with the library itself (sadly, including the latest 2.5.3 version). The forthcoming version (expected to be released later this week) will address this issue, meanwhile please consider to install the library from master branch.

vgrem avatar Feb 15 '22 21:02 vgrem

Thanks for quick reply. So, on master this issue is already resolved?

ztdan4ik avatar Feb 15 '22 21:02 ztdan4ik

It is, just verified it against the example: UploadAttachment.php, seems to be working as expected.

vgrem avatar Feb 15 '22 22:02 vgrem