Show PDF File from Document Library in a SharePoint Page

<div id="DocumentFrame" >
</div>
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(loaddocuments, 'sp.js');
function loaddocuments() {
    //fetch the list of items using the client object model
    var context = new SP.ClientContext.get_current();
    //get the current website
    var web = context.get_web();
    //get the Document Library list
    var list = web.get_lists().getByTitle('Documents');
    //create the query to get all items
    //var query = SP.CamlQuery.createAllItemsQuery();
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'ID\' Ascending=\'FALSE\'/></OrderBy></Query><RowLimit>1</RowLimit></View>');
    //get all items from the query
    docdatas = list.getItems(camlQuery);
    //load the context
    context.load(docdatas, 'Include(FileLeafRef,FileDirRef)');
    //execute the query in async mode
    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 = dir + '/' + filename;
    }
     var newHtml = '';
  newHtml += '<object><embed src="' + filename + '" height="600" width="1130" type="application/pdf"></embed></object>'
     $('#DocumentFrame').html(newHtml);
}
function failed(sender, args) {
    $('#sliderFrame').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