Sign up ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Can some one please help me out in writing the sharepoint list items to a text file separated by the delimiter '|'. I need it in javascript or jquery as we are working on the cloud(office 365 sites). I am able to get the items using context.load. But not sure how to i write the resulted collection to a text file.

share|improve this question

2 Answers 2

You need to use ActiveX objects, you can write to text or excel files using ActiveX objects. Check out the below code for text files. The below JavaScript function shows an example of the same.For excel different ActiveX object is used.

function WriteFile() { var fso = new ActiveXObject("Scripting.FileSystemObject");

var fh = fso.CreateTextFile("c:\Test.txt", true); fh.WriteLine("Some text goes here..."); fh.Close(); }

share|improve this answer
    
Hi Akarsh, I am getting Permission denied error. Is there a way i can run the script specifyng the user credentails which have access.? Please suggest –  user38432 Jan 7 at 7:42
    
ActiveX has a lot of downsides i.e. only works in IE, security issues etc. –  Mx. Jan 7 at 8:56

Iterate over all list items and collect the information.

var siteUrl = '/sites/MySiteCollection';

function retrieveListItems() {

var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
    '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

}

function onQuerySucceeded(sender, args) {

var listItemInfo = '';

var listItemEnumerator = collListItem.getEnumerator();

while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nID: ' + oListItem.get_id() + 
        '\nTitle: ' + oListItem.get_item('Title') + 
        '\nBody: ' + oListItem.get_item('Body');
}

alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Source

And here is a example to export a csv file (you should use a csv instead)

    function exportToCsv() {
        var myCsv = "Col1,Col2,Col3\nval1,val2,val3";

        window.open('data:text/csv;charset=utf-8,' + escape(myCsv));
    }

Source

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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