4

I have to get the data from SharePoint(Online) List and put the data to JSON format and create a JSON file in Local drive or SharePoint list.

I am new to SharePoint online and JSON can anyone help me on this.Give me some code examples.

3 Answers 3

1

You can retrieve sharepoint list data with javascript object model (JSOM), the data is returned in JSON format

Introduction to JSOM in SharePoint

1

You can get the data from SharePoint List using REST API. It returns the data into array object and then you can convert into JSON. Refer below code:

//Retrieve list items from sharepoint using REST API 
 var  myJsonString; //To store the data into JSON format
    function GetSampleListItems() {  
        siteURL = _spPageContextInfo.siteAbsoluteUrl;   
        var apiPath = siteURL + "/_api/lists/getbytitle('samplelist')/items";  
        $.ajax({  
            url: apiPath,  
            headers: {  
                Accept: "application/json;odata=verbose"  
            },  
            async: false,  
            success: function(data) {  
                if(data.d.results.length>0)
                {
                  myJsonString = JSON.stringify(data);
                }
            },  
            eror: function(data) {  
                console.log("An error occurred. Please try again.");  
            }  
        }); 

You used the term "JSON file". Normally, the word "file" is used to refer to a physical file on some device (not a string you are dealing with in your code, or a JavaScript object). The browser has no access to physical files on your machine. It cannot read or write them. Actually, the browser does not even really have the notion of a "file". Thus, you cannot just read or write some JSON file on your local machine.

Edit: Create a JSON file using JavaScriptSerializer Class. JavaScriptSerializer class lies in System.Web.Script.Serialization namespace and this namespace found in System.web.Extensions.dll.

So to use JavaScriptSerializer class you must add the reference of System.web.Extensions.dll into your project.

// Pass the javascript object for conversion of object to JSON string  
string jsondata = new JavaScriptSerializer().Serialize(obj);  
string path = Server.MapPath("~/App_Data/");  
// Write that JSON to txt file,  
System.IO.File.WriteAllText(path + "output.json", jsondata);  

//but if you want to paste it on specific drive then you can use following code
System.IO.File.Copy("sourcePath", "\\machinename\DriveLetter$\folder name");
//e.g. System.IO.File.Copy("~\AppData\output.json", "\\machinename\E$\test\test.txt")
3
  • if we will use c# code its possible to save the JSON File in Shared Local drive ?
    – Elina
    Commented Sep 25, 2018 at 5:40
  • can you pls give me some c# code samples
    – Elina
    Commented Sep 25, 2018 at 5:41
  • Yes, it is possible with c#. I have added the code in c# to create and save the JSON file to shared drive. Commented Sep 25, 2018 at 11:46
0

you can try this tool that allows you to export SharePoint lists to JSON, XML, Collections Power Apps and more.

https://export-list-sharepoint.web.app/

enter image description here

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.