0

Very new to SharePoint Rest API's, I got the rest to work filtering the first Column but now I am running into issues with showing the multiple columns for example. I am trying to filter out the modified field within the API so I can have the date posted and the date modified in the rest data.

Demo List

Date--Country--Price--Modified

Jan-16 Brazil 3.56 About an hr ago

Jan-16 USA 4.56 Yesterday

Jan-16 Mexico 5.56 couple minutes ago


$(Document).ready(function () {
    retrieveListItems();
});


function retrieveListItems() {

var siteUrl = _spPageContextInfo.webAbsoluteUrl;

var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('Demo List')/items";


    console.log(siteUrl);
    console.log(fullUrl); 


    $.ajax({
        url: fullUrl,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose", // setting the api for JSON data
            "content-type": "application/json;odata=verbose",
        },
        success: onQuerySucceeded,
        error: onQueryFailed
    });
}

function onQuerySucceeded(data) {

    var listItemInfo = '';

    $.each(data.d.results, function (key, value) {
        listItemInfo += '<strong>ID: </strong> ' + value.Id + ' ' +
           ' <strong>Date Posted:</strong> ' + value.Title + ' ' +
           '<br />';
    });

    $("#divGetListData").html(listItemInfo);
}

function onQueryFailed(sender, args) {
    alert('Error!');
}
  • It's not quite clear what you are asking - are you saying you want to show the created date and the modified date? Or is "Date Posted" a different column and not the created date? – Dylan Cristy Apr 11 at 16:05
  • In my Rest API I only want to show ID, Date posted, and date modified. If that makes sense, sorry still new to the terms. – hittle08 Apr 11 at 16:06
  • Right now when I run the API using /items I can only see the date column and nothing else – hittle08 Apr 11 at 16:07
  • Is "Date posted" the created date, or is that a custom column? If it is a custom column, what is the internal name of that column? – Dylan Cristy Apr 11 at 16:08
  • If you call the API using just /items you should be able to see all the columns on each item when you loop though them, on your value variable. – Dylan Cristy Apr 11 at 16:09
0

Unless you use a $select in your query, you should be seeing all the columns from your list returned in your result data. Just as you are accessing the Id and Title properties of the list items, you can access the Created and Modifed properties as well. Keep in mind that date fields come back as ISO formatted strings (i.e. 2019-04-10T22:41:55Z), so in order to get a nice readable format, you will have to convert them to Date objects and then convert to a readable string.

So in your code example, you could do something like:

$.each(data.d.results, function (key, value) {

    // inside this loop, "value" is each individual list item,
    // so "value" will have all of your list columns on it

    listItemInfo += '<strong>ID: </strong> ' + value.Id + ' ' +
       ' <strong>Title:</strong> ' + value.Title + ' ' +
       ' <strong>Date posted:</strong> ' + new Date(value.Created).toLocaleDateString() + ' ' +
       ' <strong>Date modified:</strong> ' + new Date(value.Modified).toLocaleDateString() + ' ' +
       '<br />';
});
  • This worked thank you! the value.created/value.modified is what I needed thank you! – hittle08 Apr 11 at 16:44
0

Run the following code into your browser's console:

var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Demo List')/items";
$.ajax({
    url: url,
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        var items = data.d.results;

        // Loop thru items in array
        for (var i = 0; i < items.length; i++) {
            var creadtedDate = items[i].Created;
            var modifiedDate = items[i].Modified;
            var itemId = items[i].Id;
            var itemTitle = items[i].Title;

        }
    },
    error: function (data) {
        console.log(data);
    }
});

You can see all the columns returned by the query in the Response tab or Network tab within console. If you need to select only the particular columns, you can do so using the $select statement in your REST query.

var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Category')/items?$select=Title,Modified,Created";

Additionally, you can use moment.js plugin to format the dates as per your need.

  • When I run it this way I get a 401 error at items – hittle08 Apr 11 at 16:33

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.