8

I have this code below:

jQuery.ajax({
           url: res3 + "/_api/web/lists('" + normlistid + "')/items('" + normid +"')?$select=ID,Title,Created,QuemLeu/Id&$expand=QuemLeu",
           method: "GET",
           headers: {
                    "Accept": "application/json; odata=verbose" 
                     },
           success: function (data) {
                    if(data.d){
                            if(data.d.QuemLeu.results){
                                for (var i = 0; i < data.d.QuemLeu.results.length; i++){
                                    if(data.d.QuemLeu.results[i].Id == userId){
                                            document.getElementById(itemHTML).style.visibility = 'hidden';
                                            console.log("User Found.");
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    console.log("User Not Found");
                                }
                            }
                        },
                        error: function (error) {
                            console.log(error);
                        }
                    });

How can I filter if the created field (Sharepoint column) is greater than "2018/10/05"?

1
  • 1
    I don't understand you question but if you want to check if Created column is grater then '2018/10/05' (year/month/day?) usenew Date(data.d.Created) > new Date("2018-10-05"). Commented Jan 10, 2019 at 12:25

4 Answers 4

8

SharePoint REST API endpoints take ISO formatted date string for filtering.

Try using below query for filtering on Date Field:

  1. To check greater than 2018/10/05:
https://[tentat]/sites/[site]/Normativo/ComunicacoesDeNegocio/_api/web/lists('e97f928b-34e4-34e2-c2c7-150ea469fac3')/items?$select=ID,Title,Created,QuemLeu/Id&$expand=QuemLeu&$filter=(Created gt datetime'2018-10-05T00:00:00Z')
  1. To check greater than or equal to 2018/10/05:
https://[tentat]/sites/[site]/Normativo/ComunicacoesDeNegocio/_api/web/lists('e97f928b-34e4-34e2-c2c7-150ea469fac3')/items?$select=ID,Title,Created,QuemLeu/Id&$expand=QuemLeu&$filter=(Created ge datetime'2018-10-05T00:00:00Z')

Answer For Updated Question:

Add one more IF statement inside if(data.d) like given below:

if(data.d){
    if(new Date(data.d.Created) > new Date("2018-10-05")) {
        console.log("Items is created after '2018-10-05'");
    } else {
        console.log("Items is created before '2018-10-05'");
        //Add your other code here
    }
} 
7
  • Does this works for you? Commented Jan 10, 2019 at 12:08
  • The thing is I need /items('193')/ to my code working. There is another way?
    – KmDroid
    Commented Jan 10, 2019 at 12:10
  • Actually /items('193')/ gives you the single item from list. So you cannot apply filter on such endpoint. Can you tell us what is the exact requirement of yours?? Commented Jan 10, 2019 at 12:13
  • I edited my post
    – KmDroid
    Commented Jan 10, 2019 at 12:20
  • Ok, I will do that.
    – KmDroid
    Commented Jan 10, 2019 at 12:25
3

Just add

&$filter=Created gt '2018-10-05'

At the end of your endpoint

For example:

https://[tentat]/sites/[site]/Normativo/ComunicacoesDeNegocio/_api/web/lists('e97f928b-34e4-34e2-c2c7-150ea469fac3')/items('193')?$select=ID,Title,Created,QuemLeu/Id&$expand=QuemLeu&$filter=Created gt '2018-10-05'

Gt operator means "greater than"

And also you can check all operators below:

Lt (less than)

Le (less than or equal)

Gt (greater than)

Ge (greater than or equal)

Eq (equal to)

Ne (not equal to)
1
  • 1
    Hello, your filter is not working, is giving me the error "the query is not valid."
    – KmDroid
    Commented Jan 10, 2019 at 11:35
3

Add $filter to your query and use Created gt datetime'2018-10-05T00:00:00' or Created gt '2018-10-05'

/_api/web/lists('guid')/items?$select=ID,Title,Created,QuemLeu/Id&$expand=QuemLeu&$filter=Created gt '2018-10-05'
6
  • 2
    Hello, your expression is not valid.
    – KmDroid
    Commented Jan 10, 2019 at 11:38
  • Yes, you are right. I blindly copied your URL. The issue was with /items('193')/ it has to be only /items/. Commented Jan 10, 2019 at 11:45
  • Hmm, interesting. I did fixed it first and get down vote, awesome :D Commented Jan 10, 2019 at 12:09
  • The thing is I need /items('193')/ to my code working. There is another way?
    – KmDroid
    Commented Jan 10, 2019 at 12:10
  • So what do you want to filter? Using /items(193)/ will give you only one item, so there is nothing to filter. Update your question please, because apparently you want something different. Commented Jan 10, 2019 at 12:12
1

Use filter property $filter=Created gt '2018-10-17'

7
  • 1
    Hello, your expression seems to be not valid.
    – KmDroid
    Commented Jan 10, 2019 at 11:36
  • Now, is giving me "the query is not valid".
    – KmDroid
    Commented Jan 10, 2019 at 11:39
  • You use this filter under Items: /_api/web/lists('guid')/items?$filter=Created gt '2018-10-17' Commented Jan 10, 2019 at 11:48
  • The thing is I need /items('193')/ to my code working. There is another way?
    – KmDroid
    Commented Jan 10, 2019 at 12:11
  • 2
    So you updated your post and now as I wroted, you need IF statement, cause you only need to check if Created is GT DATE. Thats everything. Commented Jan 10, 2019 at 12:29

Your Answer

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

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