Kashif Nizam Qureshi

Querying Projects Document from Site and Displaying them in PDP

Leave a comment

<div id="LeftNavForMySite">
   <table width="400">
      <tbody>
         <tr>
            <td>File Name</td>
            <td> Last Modified</td>
         </tr>
         <tr>
            <td>
               <a href="/PWA/Application%20Project/Shared%20Documents/Initiation/general.css">general.css</a></td>
            <td>2016-07-03 21:23 </td>
         </tr>
      </tbody>
   </table>
</div>
<script type="text/javascript">
 
//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');
 
//CamlQuery()
//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.
query.set_folderServerRelativeUrl('Shared%20Documents/Initiation');

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()
{
 //alert('s');
 var table='<table width="400"><tr><td>File Name</td><td> Last Modified</td>';
 var modified="";
  var fileUrls = "";
  var ListEnumerator = this.allItems.getEnumerator();
   while(ListEnumerator.moveNext())
   {
// 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)
          {
          //alert(currentItem.get_item('Modified'));
          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+'<tr>';
            table=table+('<td><a href="'+fileUrls+'">'+File.get_name()+'</a></td><td>'+modified+'</td></tr>');
          }
       }
    //}
  }
  
         //Getting the empty div tag
         fieldNameElement=document.getElementById('LeftNavForMySite');
 
         //Assign the string formed to the div element's innerHTMl to display on page
         fieldNameElement.innerHTML=table+"</table>";
 }
 function failed(sender, args) {
  alert("failed. Message:" + args.get_message());
 }
  
</script> 
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s