1

I want to filter the SP list using created date so that list items created in last 8 days get displayed.

I am using:

private getCarouselListContent = () => {   
  try {
     let requestUrl = `${this.props.absoluteURL}/_api/web/Lists/GetByTitle('${this.props.listName}')/Items?$select=*$filter=Created eq '[Today]-8'`;
      this.props.spHttpClient.get(requestUrl, SPHttpClient.configurations.v1)
     .then((response: SPHttpClientResponse)=>{
       if(response.ok){

        response.json().then((responseJSON: any) => {
          // Log the response to the console
          console.log(responseJSON);

          this.setState(({  
            value: responseJSON.value            
          })); 
          return responseJSON;
        });
         
       } 
     }).then((item: any) => {      
       if (item!=null){ 
       try{            
             this.setState(({  
               value: item.value             
             })); 
                        
           }
           catch(err){      
            
           }
         }
       });
      } catch (error) {    
     console.log('error in service ', error);  
   }
 }

Can you help me with correct endpoint?

1

1 Answer 1

0

You cannot use [Today] in SharePoint REST API requests. Try using below:

let dCurrentDate = new Date();
let dStartDate = new Date(new Date().setDate(new Date().getDate()-8));
let sCurrentDate = dCurrentDate.getFullYear() + "-" + (dCurrentDate.getMonth()+1) + "-" + dCurrentDate.getDate() + "T00:00:00.000Z";
let sStartDate = dStartDate.getFullYear() + "-" + (dStartDate.getMonth()+1) + "-" + dStartDate.getDate() + "T00:00:00.000Z";

let requestUrl = `${this.props.absoluteURL}/_api/web/Lists/GetByTitle('${this.props.listName}')/items?$select=*&$filter=Created ge datetime'${sStartDate}' and Created le datetime'${sCurrentDate}'`;

Reference: Filter Created in SP REST API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.