question

johnjohn-0472 avatar image
0 Votes"
johnjohn-0472 asked MichaelHan-MSFT commented

Get the current Page title >> then filter list items based on it inisde my SPFX web part

I have a custom list named CompanyNews which contain these fields:-

1) Title -->Free Text

2) Topic ---> choice which allow multiple selections

Where inside the custom list, we will be adding the pages titles such as "Home","CEO Message", etc.. and the related Topics. Then we will add the web part to a page >> so the web part should get the current page title >> then query the custom list by matching the Pagetitle with the list's Title field and get the Topics, then show the Topics inside the web part. if nothing matches to show nothing inside the web part.

here is my code:-

 import * as React from 'react';
 import { SPHttpClient, SPHttpClientResponse } from "@microsoft/sp-http";
    
 import styles from './RelatedTopics.module.scss';
 import { IRelatedTopicsProps } from './IRelatedTopicsProps';
 import Title from "../../../shared/components/Title/Title";
    
 export interface IRelatedTopicsState {
   labels: string[];
 }
    
 export default class RelatedTopics extends React.Component<IRelatedTopicsProps, IRelatedTopicsState> {
   private newLabelsArray = [];
   private client: SPHttpClient = this.props.context.spHttpClient;
    
   constructor(props: IRelatedTopicsProps) {
     super(props);
     this.state = {
       labels: []
     };
   }
    
   public componentDidMount() {
     this.getListItems();
   }
    
   private getListItems() {
     let url = `${this.props.context.pageContext.web.absoluteUrl}/_api/lists/getbytitle('CompanyNews')/items`;
    
     RelatedTopics.getSPData(this.client, url).then(data => {
       const labels = data.value.map(label => label.label).toString().split(',');
    
       this.setState({
         labels: labels
       });
     });
   }
    
   private static async getSPData(client: SPHttpClient, url: string): Promise<any> {
     let resp: SPHttpClientResponse = await client.get(url, SPHttpClient.configurations.v1);
     return resp.json();
   }
    
   private removeRepeatedEntryElementsArray() {
     this.state.labels.map((label, i) => {
       if(!this.newLabelsArray.includes(label)){
         this.newLabelsArray.push(label);
       }
     });
   }
    
   public render(): React.ReactElement<IRelatedTopicsProps> {
     this.removeRepeatedEntryElementsArray();
    
     return (
         <>
           <Title title={'Related Topics'} />
           <div className={styles.relatedTopics}>
             {this.newLabelsArray.map(label =>
                   <a href={****} className={styles.relatedTopics__label}>{label}</a>
             )}
           </div>
         </>
     );
   }
 }

but currently when i add the web part to any page it will show all the topics regardless of the page title. any advice?

office-sharepoint-onlinesharepoint-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered MichaelHan-MSFT commented

Hi @johnjohn-0472,

In your code, you are displaying all the items in the list. You could use the $filter parameter in the rest api endpoint to filter your list to contain only items that match the Pagetitle.

You should apply the filter on the Title field like this:

 /_api/lists/getbytitle('CompanyNews')/items?$filter=Title  eq 'PageTitle'


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MichaelHan-MSFT ok but how i can get the Page title?
Thanks

0 Votes 0 ·

@johnjohn-0472,

You also need to use rest api to get the current page title. You can get the list title and current page item id like this:

  let list=this.props.context.pageContext.list.title;
  let pageId=this.props.context.pageContext.listItem.id;

Then you could use rest api to get the metadata for the page, the page title is in the Title property.



0 Votes 0 ·

@johnjohn-0472,
I’m checking how the things are going on about this issue.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

0 Votes 0 ·