Forum Discussion
SPFx how to get the current page title?
Hi,
I am trying to use SPFx and @pnp/sp to get the title of the current news page, I mean when I open a news post I would like to retrieve the title of the news post. Actually the site pages library has one custom column called Tags showing tags applied to the news post (It, Economy, etc...). My goal is show the corresponding tags when I open a news post.
I have tried with this:
this.props.context.pageContext.listItem['Tags'].get().then((items: any[]) => {
console.log(items);
});
the context is webPartContext.
but in the console I get "undefined"
How should I use the pageContext?
Best regards
Americo
You can try in this way, using pnpjs and listitemid (number)
var resultData: any = await sp.web.lists.getByTitle("Site Pages") .items.getById(15) .select("Tags") .get(); return await resultData.Tags;
Cheers
Federico
5 Replies
You can try in this way, using pnpjs and listitemid (number)
var resultData: any = await sp.web.lists.getByTitle("Site Pages") .items.getById(15) .select("Tags") .get(); return await resultData.Tags;
Cheers
Federico
- Americo PerezIron Contributor
Thanks Federico,
What is the 15 in the getById?
Right now I am getting an 404 error :-(
Regards!
listitemid (number), your this.props.context.pageContext.listItem.id
Cheers
Federico