SPFX Getting "undefined" when asking for author name.

Iron Contributor

Hi,

I am testing sp-pnp-js and for that I choose get information from the Site Pages library. 

More specific I am trying to get the "Description", "Title" and "Author" properties of the items in the Site Pages library.

I am getting the Title and Description but the Author is undefined and I don't know why.

 

I am using this basic code: 

 

export interface IPnpWebPartProps {

description: string;

}

export interface ISPList {

Title: string;

Author: string;

Description: string;

}

export default class PnpWebPart extends BaseClientSideWebPart<IPnpWebPartProps> {

private _getListData(): Promise<ISPList[]> {

return pnp.sp.web.lists.getByTitle("Site Pages").items.get().then((response) => {

return response;

});

}

private getListData(): void {

this._getListData()

.then((response) => {

this._renderList(response);

});

}

private _renderList(items: ISPList[]): void {

let html: string = '<table class="TFtable" border=1 width=100% style="border-collapse: collapse;">';

html += `<th>Title</th><th>Author</th><th>Content</th>`;

items.forEach((item: ISPList) => {

html += `

<tr>

<td>${item.Title}</td>

<td>${item.Author}</td>

<td>${item.Description}</td>

</tr>

`;

});

html += `</table>`;

const listContainer: Element = this.domElement.querySelector('#spGetListItems');

listContainer.innerHTML = html;

}

public render(): void {

this.domElement.innerHTML = `

<div class="parentContainer" style="background-color: lightgrey">

<div style="background-color: lightgrey" id="spGetListItems" />

</div>

`;

this.getListData();

}

protected get dataVersion(): Version {

return Version.parse('1.0');

}

protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {

return {

pages: [

{

header: {

description: strings.PropertyPaneDescription

},

groups: [

{

groupName: strings.BasicGroupName,

groupFields: [

PropertyPaneTextField('description', {

label: strings.DescriptionFieldLabel

})

]

}

]

}

]

};

}

}

But the author is always undefined.

 

Is it possible to get the Author name?

Best regards

Americo

2 Replies

Author is a field of type people or group. It is actually a kind of lookup column. So, to retrieve the value, you need "expand" it. 

Modify your REST endpoint as :

pnp.sp.web.lists.getByTitle("Site Pages").items.select("Title", "Description", "Author/Title","Author/Name").expand("Author").get().then((response) => {

console.log(response);

});

This will get the response as in the below screenshot, you need to make necessary change in your code:

Capture.PNG

Thanks! I will test it in the weekend :thumbs_up: