pnpjs
pnpjs copied to clipboard
`list` exposed on `IItem` obtained from `IFolder#getItem` returns parent folder, not its list.
Category
- [ ] Enhancement
- [x] Bug
- [ ] Question
- [ ] Documentation gap/issue
Version
Please specify what version of the library you are using: 3.7.0
Please specify what version(s) of SharePoint you are targeting: SharePoint Online
Setup
Say we have this folder tree
Shared Documents
|-- Folder
| |-- file1
|-- file2
Expected
const folderPath = "sites/dev/Shared Documents/Folder";
const folderItemIList: IList = await sp.web.getFolderByServerRelativePath(folderPath).getItem().then(item => item.list);
const folderItemList = await folderItemIList();
assert.equal(folderItemList['odata.type'], "SP.List"); // fail, its "SP.Folder"
assert.equal(folderItemList.ItemCount, 3); // fail, its 2
Observed Behavior
const parentFolder = await sp.web.getFolderByServerRelativePath("sites/dev/Shared Documents")();
assert.deepEqual(folderItemList, parentFolder); // succeeds
Attempting list operations of course fails.
await folderItemIList.items(); // "Cannot find resource for the request items."
So, you have actually found a bug but it's not what you're reporting. The bug is that item.list is getting the wrong url because it's thinking the parent url is the list url which it is not in this case... I will work to get that fixed.
Now to your issues:
assert.equal(folderItemList['odata.type'], "SP.List"); // fail, its "SP.Folder"
Your assumption is incorrect, you in fact asked for the list (you're even typing folderItemIList as type IList so the odata.type should be list. Essentially this line:
const folderItemList = await folderItemIList();
retrieves the list.
Your second item:
assert.equal(folderItemList.ItemCount, 3); // fail, its 2
is also incorrect because of the item above, you've retrieved the list and the list (based on your example) has 3 items in it so ergo that is the right answer.
If you're looking for information about the folder, then you shouldn't be doing the .then() on the folderItemIList call (in general that doesn't seem like a thing to do regardless, the await with the .then can lead to unexpected results I think you're going to want to split that into two lines)
const folder: IFolder = await sp.web.getFolderByServerRelativePath(folderPath).expand("files")();
const folderItems = folder["Files"].length; // this will be 1 based on your example
const folderOData = folder["odata.type"]; // this will be SP.Folder
My assertions would have been correct, had the runtime object returned from calling IItem#list
actually returned the list info. I.e. the "Documents" list.
const folderPath = "sites/dev/Shared Documents/Folder";
const folderIItem: IItem = await sp.web.getFolderByServerRelativePath(folderPath).getItem();
// IList with wrong url - the bug
const folderItemIList: IList = folderIItem.list;
// not the list but the parent folder, because of the bug
const folderItemList = await folderItemIList();
// therefor, these assertions fail
assert.equal(folderItemList['odata.type'], "SP.List"); // fail, its "SP.Folder"
assert.equal(folderItemList.ItemCount, 3); // fail, its 2
Apparently, because of the bug, its returning the parent's folder FolderInfo
.
Fixed and merged, will be in the next release. Thanks for letting us know!
This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.