Querying Projects Document from Site and Displaying them in PDP

I had requirement to get documents from Project site and display them in a PDP page, so user does not have to go to library and view document.

Place the following code in Script Editor web part and modify the document library path.

<div id="LeftNavForMySite">
   <table width="400">
            <td>File Name</td>
            <td> Last Modified</td>
               <a href="/PWA/Application%20Project/Shared%20Documents/Initiation/general.css">general.css</a></td>
            <td>2016-07-03 21:23 </td>

//We need to run our function only after sp.js file loads
ExecuteOrDelayUntilScriptLoaded(ViewItem, "sp.js");
function ViewItem()
 var projuid=  PDP_projUid;
 var url="/PWA/"+ PDP_projName;
var context = new SP.ClientContext(url);
var web = context.get_web();

var list = web.get_lists().getByTitle('Documents');
//For querying Nth level within the library
//var query = SP.CamlQuery.createAllItemsQuery();

//For querying to current library or folder if defined below
var query = new SP.CamlQuery();

//Leading forward slash was the pain in the ass in the below line
//Folder structure starts with library name and follows folder names.

allItems = list.getItems(query);
context.load(allItems, 'Include(Title, ContentType, File, Modified, FileSystemObjectType)');
context.executeQueryAsync(Function.createDelegate(this, this.success),   Function.createDelegate(this, this.failed));
function success()
 var table='<table width="400"><tr><td>File Name</td><td> Last Modified</td>';
 var modified="";
  var fileUrls = "";
  var ListEnumerator = this.allItems.getEnumerator();
// alert('d');
    var currentItem = ListEnumerator.get_current();
   //if(currentItem.get_fileSystemObjectType() == '1')
   //   alert(currentItem.get_item('Title'));
     var _contentType = currentItem.get_contentType();
     if(_contentType.get_name() != "Folder")
        var File = currentItem.get_file();
        if(File != null)
          modified=String.format("{0:yyyy}-{0:MM}-{0:dd} {0:HH}:{0:mm} ",new Date(currentItem.get_item('Modified')));
          fileUrls += File.get_serverRelativeUrl() + '\n';
            table=table+('<td><a href="'+fileUrls+'">'+File.get_name()+'</a></td><td>'+modified+'</td></tr>');
         //Getting the empty div tag
         //Assign the string formed to the div element's innerHTMl to display on page
 function failed(sender, args) {
  alert("failed. Message:" + args.get_message());

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s