Search This Blog

Saturday, August 12, 2017

Get Folder and File Using JSOM Bind Dynamic Table

Call On Document ready Function
 
  //GetFolders("NGO",1);
 
function GetFolders(folderName)
{
                //checkPermissions(folderName,selectedLink);
                this.folderName=folderName;
                //this.selectedLink=selectedLink;
                var context = new SP.ClientContext.get_current();
                var web = context.get_web();
                var list = web.get_lists().getByTitle(LibraryNameForUpdate);
                var query = new SP.CamlQuery();
                query.set_folderServerRelativeUrl(folderName);
                var q = '<View><Query><OrderBy><FieldRef Name=\'ID\' Ascending=\'True\' /></OrderBy></Query></View>';
                query.set_viewXml(q);
                allItems = list.getItems(query);
                this.listInfoArray=context.load(allItems);
                context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success()
{
   //checkPermissions(this.folderName,this.selectedLink);
                var resultGridHtml='';
                var vrSrNo = 1;
                var noRecords='true';
                var ListEnumerator = this.allItems.getEnumerator();
               
                while(ListEnumerator.moveNext())
                {
                var currentItem = ListEnumerator.get_current();
                                var _contentType = currentItem.get_fileSystemObjectType();
                     if(currentItem.get_fileSystemObjectType() == '1')
                                {
                                                               
                                                                if(vrSrNo==1)
                                                                {
                                                                                resultGridHtml +='<table width="100%" id="tablepaging" class="table">';
                                                                                resultGridHtml +=FolderHeader();
                                                                               
                                                                }
                                                                noRecords='false';
                                                                var selectedLink=1;
                                                                var fieldUserValueCreatedBy = currentItem.get_item("Author");
                var fieldUserValueModifiedBy = currentItem.get_item("Editor");
                var dateCreated = new Date(currentItem.get_item('Created'));
                                                                resultGridHtml += '<tr>';
                resultGridHtml += '<td>'+ currentItem.get_item('Title')+'</td>';
                resultGridHtml += '<td>' + dateCreated.getDate() + "-" + monthNames[dateCreated.getMonth()] + "-" + dateCreated.getFullYear() +  '</td>';
                resultGridHtml += '<td>' + fieldUserValueCreatedBy.get_lookupValue() + '</td>';
                resultGridHtml += '<td>' + fieldUserValueModifiedBy.get_lookupValue() + '</td>';
                resultGridHtml += '</tr>';           
                vrSrNo =vrSrNo + 1;
                
 
                                                //FolderNames  += currentItem.get_item('Title')+ '\n';
                                }
                                if(currentItem.get_fileSystemObjectType() == '0')
                                {
                                                               
                                                                var dateCreated = new Date(currentItem.get_item('Created'));
                                                                var selectedLink=1;
                                                                var fieldUserValueCreatedBy = currentItem.get_item("Author");
                var fieldUserValueModifiedBy = currentItem.get_item("Editor");
                noRecords='false';
                                                     if(vrSrNo==1)
                                                                                {
                                                                    resultGridHtml +='<table width="100%" id="tablepaging" class="table">';
                                                                                resultGridHtml +=FileHeader();
                                                                    }
                                                                                resultGridHtml += '<tr>';
                                                                                resultGridHtml += '<td>'+ currentItem.get_item('Title')+'</td>';
                                                                                resultGridHtml += '<td>' + dateCreated.getDate() + "-" + monthNames[dateCreated.getMonth()] + "-" + dateCreated.getFullYear() + '</td>';                    
                                                                                resultGridHtml += '<td>' + fieldUserValueCreatedBy.get_lookupValue() + '</td>';
                                                                                resultGridHtml += '<td>' + fieldUserValueModifiedBy.get_lookupValue() + '</td>';
                                resultGridHtml += '</tr>';
                                vrSrNo =vrSrNo + 1;
               
            }
    }
   
                                                                if(noRecords=='true')
                                                                {
                                                                                resultGridHtml +='<table width="100%" id="tablepaging" class="table">';
                                                                                resultGridHtml +=FileHeader();
                                                                    resultGridHtml +='<tr><td colspan="9">No records found</td><tr>';
                                                                   
                                                                }                                                                                             
                resultGridHtml +='<table>';
    $('#responsiveGrid').html(resultGridHtml);
}
function failed(sender, args)
{
                //alert("failed. Message:" + args.get_message());
}
 
function FileHeader()
{
                var vHeaderFile='';
                vHeaderFile +='<thead>';
                vHeaderFile +='<tr  valign="top">';
    vHeaderFile +='<th align="left">Name</th>';
    vHeaderFile +='<th align="left">Created On</th>';
    vHeaderFile +='<th align="left">Created By</th>';
    vHeaderFile +='<th align="left">Modified By</th>';
    vHeaderFile +='</tr>';
    vHeaderFile +='</thead>';
    return vHeaderFile;
 
}
function FolderHeader()
{
                var vHeaderFolder='';
                vHeaderFolder +='<thead>';
                vHeaderFolder +='<tr valign="top">';
    vHeaderFolder +='<th align="left">Name</th>';
    vHeaderFolder +='<th  align="left">Created On</th>';
    vHeaderFolder +='<th  align="left">Created By</th>';
    vHeaderFolder +='<th  align="left">Modified By</th>';
    vHeaderFolder +='</tr>';
    vHeaderFolder +='</thead>';
    return vHeaderFolder;
}

No comments:

Post a Comment