SharePoint JSOM:获取SPFolder自定义字段值

时间:2014-10-23 08:09:44

标签: javascript sharepoint sharepoint-2013 sharepoint-clientobject

我们有

  1. SharePoint 2013文档库
  2. 基于标准“文件夹”内容类型添加到库中的自定义内容类型;添加到内容类型的新“DisplayName”文本字段
  3. 在文档库中创建了一些自定义内容类型的文件夹
  4. 我尝试创建一个JavaScript控件来可视化库中的文件夹结构。但我无法获得自定义“DisplayName”字段值

    this.clientContext = SP.ClientContext.get_current();
    
    var web = this.clientContext.get_web();
    this.clientContext.load(web);
    
    this.clientContext.executeQueryAsync(function(sender, args) {
    
        for (var i = 0; i < this.topLevelFoldersUrl.length; i++) {
    
            var contextParams = {};
    
            contextParams.folderUrl = web.get_serverRelativeUrl() + "/" + this.topLevelFoldersUrl[i];
    
            // folder
            contextParams.topLevelFolder = web.getFolderByServerRelativeUrl(contextParams.folderUrl);
            this.clientContext.load(contextParams.topLevelFolder, 'Include(DisplayName)');
    
            // folder list item
            contextParams.folderListItem = contextParams.topLevelFolder.get_listItemAllFields();
            this.clientContext.load(contextParams.folderListItem);
    
            // subfolders
            contextParams.folderCollection = contextParams.topLevelFolder.get_folders();
            this.clientContext.load(contextParams.folderCollection);
    
            this.clientContext.executeQueryAsync(
    
                function (sender, args) {
    
                    var folderName = contextParams.topLevelFolder.get_name();
    
                    // EXCEPTION goes here: "property has not been initialized"
                    var folderDisplayName = contextParams.folderListItem.get_item("DisplayName");
    
                    // visualization code...
    
                },
                this.onQueryFailed
            );
        }
    }, this.onQueryFailed);
    

    是否有特殊技术来获取文件夹自定义字段?我已经使用Google搜索所有代码示例来获取列表项及其自定义字段,但样本在我的情况下不起作用。

1 个答案:

答案 0 :(得分:1)

&#34; DisplayName的&#34;似乎是SharePoint 2013 JSOM中的保留标识符。将字段的名称更改为&#34; ShownName&#34;我的代码工作:

this.clientContext = SP.ClientContext.get_current();

var web = this.clientContext.get_web();
this.clientContext.load(web);

this.clientContext.executeQueryAsync(function (sender, args) {

    for (var i = 0; i < this.topLevelFoldersUrl.length; i++) {

        var contextParams = {};

        contextParams.folderUrl = web.get_serverRelativeUrl() + "/" + this.topLevelFoldersUrl[i];

        contextParams.topLevelFolder = web.getFolderByServerRelativeUrl(contextParams.folderUrl);
        this.clientContext.load(contextParams.topLevelFolder);

        contextParams.folderListItem = contextParams.topLevelFolder.get_listItemAllFields();
        this.clientContext.load(contextParams.folderListItem, 'ShownName');

        contextParams.folderCollection = contextParams.topLevelFolder.get_folders();
        this.clientContext.load(contextParams.folderCollection);                        

        this.clientContext.executeQueryAsync(

            function(sender, args) {

                var folderName = folder.get_name(); 
                var folderDisplayName = folderItem.get_item("ShownName");

                // visualization code...
            },
            this.onQueryFailed
        );
    }
}, this.onQueryFailed);