Find the Latest Document from Document Library and display the content using Javascript

<script type="text/javascript">
SP.SOD.executeOrDelayUntilScriptLoaded(loaddocuments,'SP.js');
function loaddocuments() {
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle('Documents');
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'Modified\' Ascending=\'FALSE\'/></OrderBy></Query><RowLimit>1</RowLimit></View>');
    docdatas = list.getItems(camlQuery);
    context.load(docdatas, 'Include(FileLeafRef,FileDirRef)');
    context.executeQueryAsync(
        Function.createDelegate(this, this.success),
        Function.createDelegate(this, this.failed));
}
function success(sender, args) {
    var enumerator = this.docdatas.getEnumerator();
    while(enumerator.moveNext()) {
        var currentItem = enumerator.get_current();
        var filename = currentItem.get_item('FileLeafRef');
        /*filename = filename.replace('.', '_');
        filename += '.jpg';*/
        var dir = currentItem.get_item('FileDirRef');
        filename = "http://" + window.location.host +  dir + '/' + filename;
    }
  var doc = new ActiveXObject("Word.Application"); 
  doc.Visible=false; 
  doc.Documents.Open(filename);               
  var txt;
  txt = doc.Documents(filename).Content;
  $('#documentdata').html('');
  $('#documentdata').html("<p>" + txt + "</p>");
  doc.quit(0);
}
function failed(sender, args) {
    $('#documentdata').html(args.get_message());
}
</script>

Comments

Popular posts from this blog

Activating a SharePoint Feature on Multiple Sites or Site Collections using PowerShell

Managed Path with WildCard and Explicit Inclusion

List All SharePoint 2010 PowerShell Commands