【问题标题】:Output SharePoint Document Library Files to CSV File Using JavaScript使用 JavaScript 将 SharePoint 文档库文件输出到 CSV 文件
【发布时间】:2019-10-21 13:19:09
【问题描述】:

我正在寻找如何将 SharePoint 文档库文件输出到 csv 文件。我找到了几乎可以让我到达那里的脚本,但我不知道如何更新代码以将信息导出到 csv 文件,而不是导出到 console.log() 或 alert()。我尝试的一切都会破坏代码。我回顾了其他显示如何添加到 CSV 的 JavaScript 概念,但我再次脚本概念破坏了我正在尝试修改的代码。我正在使用的脚本。此外,脚本输出文件名。我想获得有关如何不仅输出文件名的帮助,而且我喜欢输出、修改日期、创建日期和文件链接。我希望这是可能的,我感谢任何帮助实现这个概念。我使用的脚本如下。

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("Fact Sheets & Agreements");
clientContext.load(oList);
//Get the root folder of the library   
oLibraryFolder = oList.get_rootFolder();
var documentSetFolder = "sites/nbib/ep/Fact%20Sheets/";
//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() {
//Loop through the document set files and get the display name   
var docSetFilesEnumerator = docSetFiles.getEnumerator();
while (docSetFilesEnumerator.moveNext()) {
    var oDoc = docSetFilesEnumerator.get_current().get_file();
    alert("Document Name : " + oDoc.get_name());
    console.log("Document Name : " + oDoc.get_name());
   }
}

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


} 

【问题讨论】:

    标签: javascript jquery sharepoint


    【解决方案1】:

    chrome 中的示例测试脚本。

    function QuerySuccess() {
                //Loop through the document set files and get the display name
                var csv = 'Document Name\n';
                var docSetFilesEnumerator = docSetFiles.getEnumerator();
                while (docSetFilesEnumerator.moveNext()) {
                    var oDoc = docSetFilesEnumerator.get_current().get_file();
                    //alert("Document Name : " + oDoc.get_name());
                    //console.log("Document Name : " + oDoc.get_name());
                    csv += oDoc.get_name();//+','   if more cloumns
                    csv += "\n";
                }
                var hiddenElement = document.createElement('a');
                hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
                hiddenElement.target = '_blank';
                hiddenElement.download = 'DocumentList.csv';
                hiddenElement.click();
            }
    

    【讨论】:

    • 这很好用。我希望完成的事情。我需要做哪些调整才能使其在 IE 中也能正常工作。另外,您对如何访问文档的 url 并将它们输出到 CSV 有任何想法吗?我试图做到这一点,但没有快乐。非常感谢!
    • 等待其他列输出 oDoc.get_name()+','+oDoc.get_serverRelativeUrl();
    • 我得到了一切。我还能够构建您提供的脚本以在 IE(工作时使用的默认浏览器)中下载 CSV。非常感谢您的帮助。惊人的!!!我会投票赞成这个答案。
    • 我有一个简单的问题,我认为这很简单,但我的大脑可以做对。要添加具有“创建”和“修改”日期的列,它将是 oDoc.get_created() 和 oDoc.get_modified() 吗?添加此内容时出现错误,Object 不支持属性或方法“get_created() 或 get_modified()。将此信息添加到 CSV 是否不正确?
    • 我在使用 console.log 过滤数据后计算出了日期。要更改日期,名称如下:oDoc.get_timeCreated() 和 oDoc.get_timeLastModified()。现在我拥有了用户所需的一切。我相信这个问题很接近。感谢过去的帮助。
    猜你喜欢
    • 2011-08-25
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多