Search code examples
javascriptsharepointsharepoint-2010

Getting the displayName or loginName from SP.User in CSOM


I am trying to get the display name or, if not available, the loginname of a user in SP2010, while I am querying a list of files from a document library. (I am aware that this function is currently returning nothing.)

getEvidenceDocuments = function (relativePath) {
    var clientContext = new SP.ClientContext("/documents");
    var oList = clientContext.get_web().get_lists().getByTitle('Documents');
    var query = SP.CamlQuery.createAllItemsQuery();
    query.set_folderServerRelativeUrl(relativePath);
    var allItems = oList.getItems(query);
    clientContext.load(allItems, 'Include(Title, ContentType, File, Author, Editor)');
    clientContext.executeQueryAsync(Function.createDelegate(this, function () {
        var ListEnumerator = allItems.getEnumerator();
        var fileCollection = [];
        while (ListEnumerator.moveNext()) {
            var currentItem = ListEnumerator.get_current();
            var _contentType = currentItem.get_contentType();
            if (_contentType.get_name() !== "Folder") {
                var File = currentItem.get_file();
                if (File !== null) {
                    var obj = {
                        title: File.get_title(),
                        name: File.get_name(),
                        author: clientContext.load(File.get_author(), "Title"),
                        modifiedBy: File.get_modifiedBy(),
                        modified: File.get_timeLastModified()
                    };

                    fileCollection.push(obj);
                }
            }
        }
        console.log(fileCollection);
    }), Function.createDelegate(this, function () { console.log("ohoh"); }));
};

The key part is essentially this:

var obj = {
    title: File.get_title(),
    name: File.get_name(),
    author: clientContext.load(File.get_author(), "Title"),
    modifiedBy: File.get_modifiedBy(),
    modified: File.get_timeLastModified()
};

File.get_modifiedBy() return an SP.User object, while clientContext.load(File.get_author(), "Title") returns undefined.

Since I don't know the correct way to do this, I was building my approach around this page: https://social.technet.microsoft.com/wiki/contents/articles/22156.sharepoint-2010-a-complete-list-of-spfile-operations-using-ecma-script.aspx

What would be the correct approach, to resolve author and modifiedBy to its respective SP.User properties.


Solution

  • The current Author & Editor are available on the ListItem, not the SPFile object of that listitem. Using SPListItem.get_Item("propertyName") followed by get_lookupValue() does the trick.

    The following snippet retrieves all the required fields and returns a deferred object, so that the call can be chained:

    getEvidenceDocuments = function (relativePath) {
        var deferred = $.Deferred();
        var clientContext = new SP.ClientContext("/documents");
        var oList = clientContext.get_web().get_lists().getByTitle('Documents');
        
        var query = SP.CamlQuery.createAllItemsQuery();
        query.set_folderServerRelativeUrl(relativePath);
        
        var allItems = oList.getItems(query);
        clientContext.load(allItems, 
            'Include(Id, Title, ContentType, File, Author, Editor)');
        
        clientContext.executeQueryAsync(
            Function.createDelegate(this, function () {
                var ListEnumerator = allItems.getEnumerator();
                var fileCollection = [];
                
                while (ListEnumerator.moveNext()) {
                    var currentItem = ListEnumerator.get_current();
                    var _contentType = currentItem.get_contentType();
                    
                    if (_contentType.get_name() !== "Folder") {
                        var File = currentItem.get_file();
                        if (File !== null) {
                            var obj = {
                                id        : currentItem.get_id(),
                                title     : File.get_title(),
                                name      : File.get_name(),
                                createdBy : currentItem.get_item("Author").get_lookupValue(),
                                created   : File.get_timeCreated(),
                                // author : clientContext.load(File.get_author(), "Title"),
                                modifiedBy: currentItem.get_item("Editor").get_lookupValue(),
                                modified  : File.get_timeLastModified()
                            };
                            
                            fileCollection.push(obj);
                        }
                    }
                }
                
                deferred.resolve(fileCollection);
            }), 
            Function.createDelegate(this, function () {deferred.reject(); })
        );
        
        return deferred.promise();
    };