SOLVED

SPFx how to get the current page title?

Iron Contributor

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

5 Replies
best response confirmed by Americo Perez (Iron Contributor)
Solution

Hi @Americo Perez 

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

Thanks Federico,

What is the 15 in the getById? 

Right now I am getting an 404 error :(

 

Regards!

Hi @Americo Perez 

listitemid (number), your this.props.context.pageContext.listItem.id

 

Cheers

Federico

I understand.

I did some changes and I am getting the corresponding tags. 

Thanks for your help!!

 

Regards!

welcome :)
Federico

1 best response

Accepted Solutions
best response confirmed by Americo Perez (Iron Contributor)
Solution

Hi @Americo Perez 

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

View solution in original post