pnpjs icon indicating copy to clipboard operation
pnpjs copied to clipboard

`list` exposed on `IItem` obtained from `IFolder#getItem` returns parent folder, not its list.

Open gomain opened this issue 2 years ago • 1 comments

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

gomain avatar Oct 11 '22 05:10 gomain

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

juliemturner avatar Oct 14 '22 19:10 juliemturner

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.

gomain avatar Oct 15 '22 15:10 gomain

Fixed and merged, will be in the next release. Thanks for letting us know!

patrick-rodgers avatar Oct 21 '22 17:10 patrick-rodgers

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.

github-actions[bot] avatar Oct 24 '22 02:10 github-actions[bot]