pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
750 stars 306 forks source link

Iterate over list content types (IContentTypes) #2897

Closed MarkusLanger closed 8 months ago

MarkusLanger commented 8 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

21.0

Target environment

SharePoint Framework

Additional environment details

SPFx 1.18.2 node 18.17.1

Question/Request

How can I iterate over all list content types?

I get the content types: const ct: IContentTypes | undefined = await (await sp?.web.lists.ensureSitePagesLibrary())?.contentTypes();

And I also get a response with an array of content types but how can I iterate over the array? image

patrick-rodgers commented 8 months ago

You can iterate arrays in JS many ways - a for loop for example? But perhaps I am not understanding your question?

MarkusLanger commented 8 months ago

If I use my code in one line as above I don't get the "map" function on the array. I found out that when I split my code it works fine.

            const list = await sp?.web.lists.ensureSitePagesLibrary();
            if (list) {
                const ct = await list.contentTypes();
                const ret = ct.map(
                    ({ StringId, Name }): IContentType => ({
                        id: StringId,
                        title: Name,
                    })
                );
                console.log('🚀 ~ fetchContentTypes ~ ret:', ret);
            }
bcameron1231 commented 8 months ago

Glad you were able to come to a resolution! This happens sometimes, but it's not an issue with the library but an issue with IDE's ability to handle intellisense. Sometimes it's able to properly infer the return types, and other times it's not. For example, I had your issue when I first tried to reproduce... and then I closed and re-opened VS Code and it started working.

image

github-actions[bot] commented 8 months ago

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.