sharepoint-online - 如何使用 SharePoint JSOM 枚举所有文档集文件属性
问题描述
下面的代码成功地遍历了我在特定文档库中的所有文件,并输出了每个文件的名称。但是,我需要枚举其他属性(不属于 Document 内容类型的自定义属性/列)。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("&");
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>
解决方案
您需要添加ListItemAllFields属性。
从这个答案复制:
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"];
}
}
推荐阅读
- geolocation - 从最近到最远订购 Firestore GeoHash 查询?
- favicon - 如何将 favicon 添加到 Next.js 静态站点?
- javascript - iOS Safari 视频/相机无法在移动设备上运行 - Javascript 和 Html
- ios - 如何使变量可以从不同的 swift 文件中访问?
- angular - ng new 错误:EPERM: operation not allowed syscall unlink
- jquery - 数据更改时如何在制表器中突出显示单个单元格
- javascript - 修复 on_click 函数以更改文本
- php - Laravel Consolestvs\charts - 在图表中显示属于登录的特定用户的数据
- javascript - 循环遍历包含对象的数组对象
- c# - Gradle 启动失败:意外令牌:...@ 第 83 行,第 7 列