代码之家  ›  专栏  ›  技术社区  ›  Scott Marcus

如何使用SharePoint JSOM枚举所有文档集文件属性

  •  -1
  • Scott Marcus  · 技术社区  · 6 年前

    以下代码成功地在特定文档库中的所有文件上循环,并输出每个文件的名称。但是,还有其他属性(自定义属性/列不是文档内容类型的一部分)需要枚举。 get_item() 不是上的有效方法 SP.File 键入,这样我就不能直接传入要查找的属性名。我想我需要通过 Include 对…的争论 .load() 方法,但我没有成功,因为它的错误告诉我,我已经包含的属性名称不存在。

      <script src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
      <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
      <script>
        // Load the required SharePoint libraries.
        $(function () {
          var ctx = null;
          var collListItem = null;
    
          // Function to retrieve a query string value.
          // For production purposes you may want to use
          // a library to handle the query string.
          function getQueryStringParameter(paramToRetrieve) {
            var params =
              document.URL.split("?")[1].split("&amp;");
            var strParams = "";
            for (var i = 0; i < params.length; i = i + 1) {
              var singleParam = params[i].split("=");
              if (singleParam[0] == paramToRetrieve)
                return singleParam[1];
            }
          }
    
          // Get the URI decoded URLs.
          hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    
          // The js files are in a URL in the form: web_url/_layouts/15/resource_file
          var scriptbase = hostweburl + "/_layouts/15/";
    
          // Load the js files and continue to the execOperation function.
          $.getScript(scriptbase + "SP.Runtime.js",
            function () {
              $.getScript(scriptbase + "SP.js", mainFunction);
            }
          );
    
          var siteUrl = "https://company.sharepoint.com/sites/theSite";
          var docSetUrl = "https://company.sharepoint.com/sites/theSite/docLibraryName/docSetName";
          var ctx = null;
          var files = null;
    
          function mainFunction() {
            getFiles(docSetUrl);
          }
    
          function getFiles(folderUrl) {
            ctx = new SP.ClientContext(siteUrl);
            files = ctx.get_web().getFolderByServerRelativeUrl(folderUrl).get_files();
            ctx.load(files);
            ctx.executeQueryAsync(success, error);
          }
    
          function success() {
            console.log("success");
            for (var i = 0; i < files.get_count(); i++) {
              var file = files.get_item(i);
              console.log(file.get_name());
            }
          }
    
          function error(sender, args) {
            console.log("error");
            console.log(args.get_message());
          }
    
        });
      </script>
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Tracy    6 年前

    你需要添加 ListItemAllFields 财产。

    复制自 this answer :

    using (ClientContext spClientContext = new ClientContext("http://whatever"))
    {
        var rootweb = spClientContext.Web;
    
        FolderCollection folderCollection =
            rootweb.GetFolderByServerRelativeUrl("/Shared Documents/test2").Folders;
    
        // Don't just load the folder collection, but the property on each folder too
        spClientContext.Load(folderCollection, fs => fs.Include(f => f.ListItemAllFields));
    
        // Actually fetch the data
        spClientContext.ExecuteQuery();
    
        foreach (Folder folder in folderCollection)
        {
            // This property is now populated
            var item = folder.ListItemAllFields;
    
            // This is where the dates you want are stored
            var created = (DateTime)item["Created"];
            var modified = (DateTime)item["Modified"];
        }
    }
    
        2
  •  0
  •   Vadim Gremyachev    6 年前

    关于

    对,找回 ListItem File 您需要指定以下表达式 'Include(ListItemAllFields)' 通过 SP.ClientContext.load method ,例如:

    ctx.load(files,'Include(ListItemAllFields)');    
    

    例子

    function getFiles(folderUrl) {
       var ctx = new SP.ClientContext(siteUrl);
       var files = ctx.get_web().getFolderByServerRelativeUrl(folderUrl).get_files();
       ctx.load(files,'Include(Name,ListItemAllFields)');
       ctx.executeQueryAsync(()=>{
    
          files.get_data().forEach(file =>{
    
             console.log(file.get_name()); //print file name
             console.log(file.get_listItemAllFields().get_fieldValues()); //print list item properties
    
          })
    
       },logError);
    }
    
    
    function logError(sender, args) {
       console.log(args.get_message());
    }