Retrieve all fields in sharepoint list using jquery or javascript

venkatesh padmanabhan 181 Reputation points
2022-03-25T13:20:20.593+00:00

Hi.
I am trying to retrieve the contents of sharepoint custom list using javascript/jquery. The data should be moved to csv file.
I found an example, which uses below code to move data to csv, but has the fields hardcoded. Is it possible to achieve this with all fields and field names not being hardcoded.

var webUrl = "siteurl";
var listName = "listname";
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();
});
Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rob Windsor 2,001 Reputation points
    2022-03-25T14:05:30.33+00:00

    Try removing the $select parameter to get all the field values. If there's still one or more values missing from the response, trying change the end of the request URL to &$select=*,FieldValuesAsText&$expand=FieldValuesAsText. Sometimes FieldValuesAsText includes field values not returned by default by the REST API.

    Something like this, https://robwindsortest991.sharepoint.com/sites/Demo/_api/Web/Lists/GetByTitle('Products')/Items?$select=*,FieldValuesAsText&$expand=FieldValuesAsText

    Fiddler trace

    Fiddler trace


  2. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-03-28T07:10:51.867+00:00

    Hi @venkatesh padmanabhan ,
    I agree with RobWindsor-4651's answer. We can use FieldValuesAsText to get all fields. If you want to save the fields to array, we can use Object.keys(). You can refer to following code.

    			var webUrl = "https://xxx.sharepoint.com/sites/abc";  
    			var listName = "TestList";  
    			$.ajax({  
    				url: webUrl + "/_api/web/lists/GetByTitle('" + listName +  
    					"')/items?$select=*,FieldValuesAsText&$expand=FieldValuesAsText",  
    				type: "GET",  
    				headers: {  
    					"Accept": "application/json; odata=nometadata"  
    				}  
    			}).done(function(data) {  
    				var results = data.value;  
    				var FieldValues = results[0]["FieldValuesAsText"];  
    				var fields = Object.keys(FieldValues);  
    				console.log(fields);  
    				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();  
    			});  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.