0
votes

We have

  1. a SharePoint 2013 document library
  2. a custom content type added to the library, based on standard "Folder" content type; a new "DisplayName" text field added to the content type
  3. a few folders of that custom content type were created in the document library

I try to make a JavaScript control to visualize folder structure from the library. But I can't get the custom "DisplayName" field value

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);

Is there a special technique to get folder custom fields? I've googled all the code samples to get list items and their custom fields, but the samples don't work in my case.

1

1 Answers

1
votes

"DisplayName" seems to be a reserved identifier in SharePoint 2013 JSOM. After changing field's name to "ShownName" my code worked:

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);