1

I have created a list, and the list has many items. I have to retrieve a single list item based on current date. I would appreciate if some one could help me how to retrieve that particular item from the list, using current or today's date using Spfx.

Thank You in advance.

2

I think above will not work because it is taking into consideration time component, you have to ignore time component and compare only date to get value. There is no straight forward way to do this. You have to use below

Logic is to compare Today date with current date(starting from first second) and tommorow(starting from first second)

Add below function to create custom addDays

 Date.prototype.addDays = function(days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + days);
        return date;
    }
    var today = new Date();

    var Startdate = today.toISOString().substring(0,10) + "T00:00:00.000Z";
    var Enddate = today.addDays(1).toISOString().substring(0,10) + "T00:00:00.000Z";

var queryString = "$filter=Today le datetime'" + Enddate + "'and Today ge datetime'" + Startdate + "'";

    jQuery.ajax({
            url: encodeURI(_spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?" + queryString),
            type: "GET",
            headers: {
                "Accept": "application/json; odata=verbose",
                "Content-Type": "application/json;odata=verbose",
            }
        }).done(function (data) { console.log(data.d.results})

Add &$Top=1 to queryString if you want to retrieve only 1 single item(please note it will return top item only with order by created date ascending order. You can specify order by desc to retrieve last item.

  • private _getListData(): Promise<ISPLists> { Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } Is this how i have to add the code? Iam getting an error in class Promise<T> interface Promise<T> namespace Promise Represents the completion of an asynchronous operation A function whose declared type is neither 'void' nor 'any' must return a value.ts(2355) – Selvin Jun 25 at 4:55
  • class Promise<T> interface Promise<T> namespace Promise Represents the completion of an asynchronous operation A function whose declared type is neither 'void' nor 'any' must return a value.ts..... This is the error – Selvin Jun 25 at 4:59
  • Iam getting errors. could you please help me according to my context? – Selvin Jun 25 at 5:58
  • Can you add code in question so that it is formatted – Siddharth Vaghasia Jun 25 at 6:24
  • _spPageContextInfo Cannot find name '_spPageContextInfo'. This is the error iam getting – Selvin Jun 25 at 7:01
0

Try this, with queryString= "$filter= yourDateField eq date'"+new Date().toISOString()"'" (eq = equal) Or queryString= "$filter= yourDateField ge date'"+new Date().toISOString()"'" (ge = greater or equal) In your response can be more than one item (data.d.results)

jQuery.ajax({
        url: encodeURI(_spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?" + queryString),
        type: "GET",
        headers: {
            "Accept": "application/json; odata=verbose",
            "Content-Type": "application/json;odata=verbose",
        }
    }).done(function (data) { console.log(data.d.results})
  • private _getListData(): Promise<ISPLists> { var today = new Date(); var d = today.toISOString().substring(0,10) + "T00:00:00"; return this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + /_api/web/lists/GetByTitle('Thoughtfortheday')/Items queryString= "$filter= Today eq date'"+new Date().toISOString()"'" , SPHttpClient.configurations.v1) .then((response:SPHttpClientResponse) => { debugger; return response.json(); Is this the correct syntax what i have written? Today is the field name in the list. Iam new to spfx. – Selvin Jun 24 at 6:45
  • The URL is: /_api/web/lists/GetByTitle('Thoughtfortheday')/Items?$filter=Today eq date'"+new Date().toISOString()"'" – Bálint Jun 24 at 7:02
0

public _getListData() { var today = new Date(); var Startdate = today.toISOString().substring(0, 10) + "T00:00:00.000Z"; var Enddate = today.toISOString().substring(0, 10) + "T23:00:00.000Z"; var dynamicHtml = "Today's Thought"; var filterQuery = "?$top=1&$filter=Created le datetime'" + Enddate + "' and Created ge datetime'" + Startdate + "'"; $.ajax({ url: encodeURI(this.context.pageContext.web.absoluteUrl + "/_api/web/lists/getbytitle('Thoughtfortheday')/items" + filterQuery), type: "GET", headers: { "Accept": "application/json; odata=verbose", "Content-Type": "application/json;odata=verbose", }, // tslint:disable-next-line: no-function-expression success: function (data) {

    var allData = data.d.results[0];
    var datalen = data.d.results.length;
    if (datalen == 0) {
      dynamicHtml += "<tr>";
      dynamicHtml += "<td  colspan='2' style='text-align: center;'>Record not found.</td>";
      dynamicHtml += "</tr>";
      dynamicHtml += "</table>";
      $("#tableData").html(dynamicHtml);
    }
    else {
      dynamicHtml += "<tr>";
      dynamicHtml += "<td colspan='2' style='text-align: center; font-size:large; '>" +"<b>" + allData.Quote+ " </td>";
      dynamicHtml += "</tr>";
      dynamicHtml += "<tr>";
      dynamicHtml += "<td style='text-align: right;'>" +"-" + allData.Authors+ "</td>";
      dynamicHtml += "</tr>";
      dynamicHtml += "</table>";
      $("#tableData").html(dynamicHtml);
    }
  },

// tslint:disable-next-line: no-function-expression error: function (err) { console.log(JSON.stringify(err)); } }); }

The list item is not retrieved according to the date automatically. Whats the correction for that in the code. And it should be automatically updated by midnight 12.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.