1

I have a requirement to allow users to export data into a csv file from a few different document libraries. The data will be a select number of columns and filtered based on if the document is new or was modified that day. The user needs to be able to compose a csv file from each library individually, or all of the libraries.

What is the best way to fulfill this requirement utilizing javascript/jQuery? I am not able to run a Powershell script and unfortunately I do not have Visual Studio.

EDIT, based on the below answer, I was able to successfully make the rest call to get the data, but I am experiencing an "Uncaught TypeError: Cannot read property 'results' of undefined". I can't figure out why the results are undefined? See code below:

$(document).ready(function getAllLibraries(){
    $.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists",
    method: "GET",
    heders: {"Accept": "application/json; odata=verbose"},
    success: function(data){
alert("Success");
        var csv = "data:text/csv;charset=utf-8,";
        var keys;
        if (data.d.results.length > 0 ){
            keys = Object.keys(data.value[0]);
            csv += keys.join(",") + "\n";
        }
        for (var j = 0; j < data.d.results.length; j++){
            for (var k = 0; k < keys.length; k++){
                csv += data.value[j][keys[k]];
                csv += k < keys.length -1 ? "," : "";
            }
            csv += "\n";
        }

        },
    error: function(data){
        alert("Error"+ data);
    }

    });
var a = document.createElement("a");
a.setAttribute("href", encodeURI(csv));
a.setAttribute("download", "data.csv");
$("#ViewLists").appendChild(a);
a.click();

});

2
  • See the JavaScript responses (especially the HTML5 download tag) from sharepoint.stackexchange.com/questions/208355/… and convert each item to a CSV string Commented Feb 22, 2017 at 18:42
  • I didn't saw your update. Generally you can't do this that way. My code was just sample showing export from one list to CSV. To do this for all lists, you must first get all lists using _api/web/lists and then in success function loop through them and call my code for each list. Commented Feb 23, 2017 at 20:29

1 Answer 1

1

I have written sample code for you that gets list items from the list using REST and exports them to CSV. You can use it as base for your code.

var webUrl = "https://contoso.sharepoint.com";
var listName = "Test";
var fields = ["Id", "FileLeafRef", "Modified"];
var today = new Date();
today.setHours(0,0,0,0);
$.ajax({
    url: webUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items?$filter=Modified ge DateTime'" + today.toISOString() + "'&$select=" + fields.join(","),
    type: "GET",
    headers: {"Accept": "application/json; odata=nometadata"}
}).done(function(data) {
    var results = data.value;
    var csv = "data:text/csv;charset=utf-8," + fields.join(",") + "\n";
    for (var j = 0; j < results.length; j++) {
        for (var k = 0; k < fields.length; k++) {
            csv += results[j][fields[k]];
            csv += k < fields.length - 1 ? "," : "";
        }
        csv += "\n";
    }
    var a = document.createElement("a");
    a.setAttribute("href", encodeURI(csv));
    a.setAttribute("download", "data.csv");
    document.body.appendChild(a);
    a.click();
});

I have used JSON Light (odata=nometadata in Accept header). If your SharePoint doesn't support it (for example SharePoint 2013), you will have to change this to odata=verbose and modify code accordingly, because returned data structure will change.

UPDATE:

Sample code without JSON Light:

var webUrl = "https://contoso.sharepoint.com";
var listName = "Test";
var fields = ["Id", "FileLeafRef", "Modified"];
var today = new Date();
today.setHours(0,0,0,0);
$.ajax({
    url: webUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items?$filter=Modified ge DateTime'" + today.toISOString() + "'&$select=" + fields.join(","),
    type: "GET",
    headers: {"Accept": "application/json; odata=verbose"}
}).done(function(data) {
    var results = data.d.results;
    var csv = "data:text/csv;charset=utf-8," + fields.join(",") + "\n";
    for (var j = 0; j < results.length; j++) {
        for (var k = 0; k < fields.length; k++) {
            csv += results[j][fields[k]];
            csv += k < fields.length - 1 ? "," : "";
        }
        csv += "\n";
    }
    var a = document.createElement("a");
    a.setAttribute("href", encodeURI(csv));
    a.setAttribute("download", "data.csv");
    document.body.appendChild(a);
    a.click();
});
4
  • hey @PawelHawrylak, i was able to incorporate my needs w/ this code, but i am gettin gan uncaught type error in the success function.. any insights? Commented Feb 23, 2017 at 16:31
  • Please provide details about exception and in which line it occurs. Which SharePoint version do you use? I suspect that JSON Light is not supported in your SharePoint version. Commented Feb 23, 2017 at 18:33
  • I actually have the code I used up at the top of this post... The exception occurs in the success function. I am using SP2013 & am aware that i have to use odata=verbose. Commented Feb 23, 2017 at 18:57
  • Ok, I will post solution without JSON Light in few minutes. Commented Feb 23, 2017 at 19:00

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.