Mar 01 2019 02:09 PM
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
Mar 02 2019 03:12 AM - edited Mar 02 2019 04:21 AM
SolutionYou 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
Mar 02 2019 10:00 AM
Thanks Federico,
What is the 15 in the getById?
Right now I am getting an 404 error :(
Regards!
Mar 02 2019 10:05 AM
listitemid (number), your this.props.context.pageContext.listItem.id
Cheers
Federico
Mar 02 2019 01:10 PM
I understand.
I did some changes and I am getting the corresponding tags.
Thanks for your help!!
Regards!
Mar 02 2019 03:12 AM - edited Mar 02 2019 04:21 AM
SolutionYou 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