我有以下方法可以从共享点查询中正确获取列表项,但是内容类型需要特殊处理,并且不会返回。

function getListItems(listId, camlQueryString, selectProperties){
                        var deferred = $q.defer();
                        var context = SP.ClientContext.get_current();
                        var web = context.get_web();

                        var list = web.get_lists().getById(listId);

                        var camlQuery = new SP.CamlQuery();
                            camlQuery.set_viewXml(camlQueryString);

                        var listItems = list.getItems(camlQuery);

                        //var includesString = "Include(ID,Title,AssignedTo, WorkflowOutcome, ApproverComments, FileRef, WorkflowItemId, Created, Modified)"; // + selectProperties.join(", ") +  ")";
                        if(selectProperties) {
                            var includesString = convertSelectPropertiesToIncludesString(selectProperties);
                            context.load(listItems, includesString);
                        } else {
                            context.load(listItems);
                        }
                        context.executeQueryAsync(
                           function() {
                                $log.info("Successfully retrieved list item result");

                                deferred.resolve(listItems);
                           },
                           function(error, errorInfo) {
                                $log.warn("Retrieving list item result failed");

                                deferred.reject(errorInfo);
                           }
                        );
                        return deferred.promise;
                    }


我需要在与另一个字段相同的listitems数组中返回内容类型名称。

我已经找到了一些示例,但是我不确定如何将该代码集成到我的代码中,因为似乎每个项目都需要两次调用。

更新1

我尝试从此使用上述方法:

function GetRelatedBillingDocumentsFromList(selectProperties, currentBillCyclePath, clientCode, jobCodes, engagementCode, enhanceFunctions) {
                $log.info("Retrieving related billing documents for bill cycle with name [" + currentBillCyclePath + "]");
                var deferred = $q.defer();
                var webUrl = _spPageContextInfo.webAbsoluteUrl;
                var viewFields = spService.ConvertSelectPropertiesToViewFields(selectProperties);
                // query must return the documents for the same client but in other bill cycles not the current one
                var camlQuery = '<View>' +   viewFields +
                        '<Query>' +
                            '<Where>' +
                                //'<And>' +
                                    '<Eq>' +
                                        '<FieldRef Name="ClientCode" />' +
                                        '<Value Type="Text">'+ clientCode + '</Value>' +
                                    '</Eq>' +
                                //  '<Neq>' +
                                //      '<FieldRef Name="ContentType" />' +
                                //      '<Value Type="Computed">Bill Cycle</Value>' +
                                //  '</Neq>' +
                                //'</And>' +
                            '</Where>' +
                            //'<QueryOptions>' +
                            //  '<ViewAttributes Scope="RecursiveAll" />' +
                            //'</QueryOptions>' +
                        '</Query>' +
                    '</View>';

                var billCyclesListId = "{c23bbae4-34f7-494c-8f67-acece3ba60da}";
                spService.GetListItems(billCyclesListId, camlQuery, selectProperties)
                .then(function(listItems) {
                    var listItemsWithValues = [];
                    if(listItems) {
                        var enumerator = listItems.getEnumerator();
                        while (enumerator.moveNext()) {
                            var listItem = enumerator.get_current();
                            var listItemValues = [];
                            selectProperties
                            .forEach(function(propertyName) {
                                if(propertyName==='ContentType'){
                                    var value = listItem.get_item('ows_ContentType');
                                }else{
                                    var value = listItem.get_item(propertyName);
                                }
                                listItemValues[propertyName] = value;
                            });

                            listItemsWithValues.push(listItemValues);
                        }
                    }

                    // Create the full item url
                    listItemsWithValues.forEach(function(listItem) {
                        var fileDirRef = listItem["FileRef"];
                        var id = listItem["ID"];
                        var serverUrl = _spPageContextInfo.webAbsoluteUrl.replace(_spPageContextInfo.webServerRelativeUrl,"");
                        var dispFormUrl = serverUrl + fileDirRef + "/DispForm.aspx?ID=" + id;
                        listItem["FileRef"] = dispFormUrl;
                    });

                    var enhancedListItemValues = spService.SpSearchQuery.EnhanceSearchResults(listItemsWithValues, enhanceFunctions);
                    deferred.resolve(listItemsWithValues);
                })
                .catch (function (message) {
                    deferred.reject();
                });

                return deferred.promise;
            }


如您所见,我需要一种以简单的方式在相同值数组中返回内容类型名称的方法

最佳答案

您可以使用ows_ContentType属性获取内容类型名称。

因此,在ows_ContentType变量或includesString参数中添加selectProperties

之后,可以在代码中按如下方式使用它:

context.executeQueryAsync(
   function() {
        var listItemEnumerator = listItems.getEnumerator();
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            console.log(oListItem.get_item('ows_ContentType'));
        }
   },
   function(error, errorInfo) {
        console.log(errorInfo);
   }
);


更新-完整代码

var context = SP.ClientContext.get_current();
var web = context.get_web();

var list = web.get_lists().getByTitle("Test");

var includesString = "Include(ID,Title,Created, Modified, ows_ContentType)";

var camlQuery = SP.CamlQuery.createAllItemsQuery();

var listItems = list.getItems(camlQuery);

context.load(listItems, includesString);

context.executeQueryAsync(
   function() {
        var listItemEnumerator = listItems.getEnumerator();
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            console.log(oListItem.get_item('ows_ContentType'));
        }
   },
   function(error, errorInfo) {
        console.log(errorInfo);
   }
);

关于javascript - JSOM/Javascript获取所有其他列的内容类型名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47629912/

10-12 20:12