0

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.

2 Answers 2

2

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

-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");</br>

   var fh = fso.CreateTextFile("c:\\Test.txt", true); </br>
   fh.WriteLine("Some text goes here..."); 
   fh.Close(); 
}
3
  • 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 Commented Jan 7, 2015 at 7:42
  • ActiveX has a lot of downsides i.e. only works in IE, security issues etc. Commented Jan 7, 2015 at 8:56
  • Bad solution. You should never use ActiveX, not only does this not work with all Versions of IE anything other than IE just doesnt work. SharePoint has a build in logger which could be used for this, example is well Microsoft: msdn.microsoft.com/en-us/library/office/… Commented May 20, 2016 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.