访问位于SharePoint文档库中的多个文件夹并在网页上显示其内容

时间:2019-12-21 17:38:26

标签: javascript sharepoint

我可以访问位于SharePoint文档库中的单个文件夹,并将其内容显示在网页上。我可以访问同一SharePoint文档库中的第二个(不同)文件夹并显示其内容。我弄清楚如何做到这一点的唯一方法是复制脚本并将脚本放在各自独立的函数中。我通过回调函数来调用其中一个函数。以下是显示我在做什么的脚本。

我正在使用的过程会实现我希望达到的效果不理想,有时在作品中;表示有时会显示其中一个文件夹内容,有时会同时显示两个文件夹内容(命中或未命中)。有没有更好的方法可以实现这一目标并获得一致的结果?一致地在网页上显示两个文件夹的内容。任何帮助将不胜感激。

我正在使用的脚本:

 memoColumn(regColumn);

//Left Column List
function memoColumn(regulations) {
 jQuery(document).ready(function() {

  var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
  $.getScript(scriptbase + "SP.Runtime.js", function() {
   $.getScript(scriptbase + "SP.js", function() {
    $.getScript(scriptbase + "SP.DocumentManagement.js", createDocumentSet);
   });
  });
 });
 var docSetFiles;

 function createDocumentSet() {
  //Get the client context,web and library object.   
  clientContext = new SP.ClientContext.get_current();
  oWeb = clientContext.get_web();
  var oList = oWeb.get_lists().getByTitle("Legacy_DSS_Files");
  clientContext.load(oList);
  //Get the root folder of the library   
  oLibraryFolder = oList.get_rootFolder();
  var documentSetFolder = "/sites/dcsa/ep/Legacy_DSS/Memos";
  //Get the document set files using CAML query   
  var camlQuery = SP.CamlQuery.createAllItemsQuery();
  camlQuery.set_folderServerRelativeUrl(documentSetFolder);
  docSetFiles = oList.getItems(camlQuery);
  //Load the client context and execute the batch   
  clientContext.load(docSetFiles, 'Include(File)');
  clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
 }

 function QuerySuccess() {

  var csv = 'Document Name, URL Name, Created Date, Modified Date\n';

  var docSetFilesEnumerator = docSetFiles.getEnumerator();
  while (docSetFilesEnumerator.moveNext()) {
   var oDoc = docSetFilesEnumerator.get_current().get_file();

   csv += oDoc.get_name() + ',' + 'https://intelshare.intelink.gov' + oDoc.get_serverRelativeUrl() + ',' + oDoc.get_timeCreated() + ',' + oDoc.get_timeLastModified(); //if more cloumns
   csv += "\n";

   var link = document.getElementById("prvMinutes1");

   link.innerHTML += '<li class="linkData" style="padding-left: 10px; padding-right: 10px;"><a href="' + 'https://intelshare.intelink.gov' + oDoc2.get_serverRelativeUrl() + '" target="_blank">' + oDoc2.get_name().replace(/\.[^/.]+$/, "") + '</a></li><br>';

  }
 }


 function QueryFailure() {
  console.log('Request failed - ' + args.get_message());
 }

 regulations();
} ///End of Memo Column Function


//Right Column List
function regColumn() {
 jQuery(document).ready(function() {

  var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
  $.getScript(scriptbase + "SP.Runtime.js", function() {
   $.getScript(scriptbase + "SP.js", function() {
    $.getScript(scriptbase + "SP.DocumentManagement.js", createDocumentSet);
   });
  });
 });
 var docSetFiles;

 function createDocumentSet() {
  //Get the client context,web and library object.   
  clientContext = new SP.ClientContext.get_current();
  oWeb = clientContext.get_web();
  var oList = oWeb.get_lists().getByTitle("Legacy_DSS_Files");
  clientContext.load(oList);
  //Get the root folder of the library   
  oLibraryFolder = oList.get_rootFolder();
  var documentSetFolder = "/sites/dcsa/ep/Legacy_DSS/Regulations";
  //Get the document set files using CAML query   
  var camlQuery = SP.CamlQuery.createAllItemsQuery();
  camlQuery.set_folderServerRelativeUrl(documentSetFolder);
  docSetFiles = oList.getItems(camlQuery);
  //Load the client context and execute the batch   
  clientContext.load(docSetFiles, 'Include(File)');
  clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
 }

 function QuerySuccess() {

  var csv = 'Document Name, URL Name, Created Date, Modified Date\n';

  var docSetFilesEnumerator = docSetFiles.getEnumerator();
  while (docSetFilesEnumerator.moveNext()) {
   var oDoc2 = docSetFilesEnumerator.get_current().get_file();

   csv += oDoc2.get_name() + ',' + 'https://intelshare.intelink.gov' + oDoc2.get_serverRelativeUrl() + ',' + oDoc2.get_timeCreated() + ',' + oDoc2.get_timeLastModified(); //if more cloumns
   csv += "\n";

   var link2 = document.getElementById("prvMinutes2");

   link2.innerHTML += '<li class="linkData" style="padding-left: 10px; padding-right: 10px;"><a href="' + 'https://intelshare.intelink.gov' + oDoc2.get_serverRelativeUrl() + '" target="_blank">' + oDoc2.get_name().replace(/\.[^/.]+$/, "") + '</a></li><br>';

  }


 }

 function QueryFailure() {
  console.log('Request failed - ' + args.get_message());
 }

} ///End of Regulations Columns Function

1 个答案:

答案 0 :(得分:1)

我建议您使用REST API来实现它。以下代码供您参考。

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    getFilesFromFolder("/sites/dcsa/ep/Legacy_DSS/Memos").done(function(data){
        $.each(data.d.results,function(i,item){
            $("#prvMinutes1").append('<li class="linkData" style="padding-left: 10px; padding-right: 10px;"><a href="' + 'https://intelshare.intelink.gov' + item.ServerRelativeUrl + '" target="_blank">' + item.Name.replace(/\.[^/.]+$/, "") + '</a></li><br>');
        });
    });
    getFilesFromFolder("/sites/dcsa/ep/Legacy_DSS/Regulations").done(function(data){
        $.each(data.d.results,function(i,item){
            $("#prvMinutes2").append('<li class="linkData" style="padding-left: 10px; padding-right: 10px;"><a href="' + 'https://intelshare.intelink.gov' + item.ServerRelativeUrl + '" target="_blank">' + item.Name.replace(/\.[^/.]+$/, "") + '</a></li><br>');
        });
    });
})
function getFilesFromFolder(serverRelativeUrlToFolder){
    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('"+serverRelativeUrlToFolder+"')/files",
        method: "GET",
        async:false,
        headers: { "Accept": "application/json; odata=verbose" }
    });
}
</script>

<div>
<ul id="prvMinutes1"></ul>
<ul id="prvMinutes2"></ul>
</div>

参考Working with folders and files with REST