代码之家  ›  专栏  ›  技术社区  ›  Marco

从CSOM中的SP.User获取显示名或登录名

  •  0
  • Marco  · 技术社区  · 8 年前

    当我从文档库查询文件列表时,我试图获取SP2010中用户的显示名或登录名(如果不可用)。 (我知道该函数当前未返回任何内容。)

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

    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() clientContext.load(File.get_author(), "Title") 返回未定义的。

    因为我不知道正确的方法,所以我围绕这一页构建了我的方法: https://social.technet.microsoft.com/wiki/contents/articles/22156.sharepoint-2010-a-complete-list-of-spfile-operations-using-ecma-script.aspx

    将author和modifiedBy解析为其各自的SP.User属性的正确方法是什么。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Marco    8 年前

    当前作者&编辑器在ListItem上可用,而不是该ListItem的SPFile对象。使用 SPListItem.get_Item("propertyName") 然后 get_lookupValue() 成功了。

    以下代码段检索所有必需字段并返回延迟对象,以便可以链接调用:

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