gpt4 book ai didi

javascript - JSOM/Javascript 获取所有其他列的内容类型名称

转载 作者:行者123 更新时间:2023-11-30 20:56:16 26 4
gpt4 key购买 nike

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

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

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

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

更新 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 属性获取内容类型名称。

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

之后,您可以在您的代码中使用它,如下所示:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com