SharePoint 2013 - 使用 Javascript 获取内容类型名称

SharePoint 2013 - Get content type names with Javascript

正如标题所说,我想用 Javascript 检索列表项的内容类型名称。具体来说:用户打开列表 a 中的 "new form" 并使用 Javascript 和 CSR 应该会提醒列表 b 中列表项的内容类型名称。为此,我尝试了以下操作:

var collListItem = null;
var contentobject = null;
var ctx = null;
var oList = null;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
  OnPostRender: function() { 
  $(document).ready( function() {ExecuteOrDelayUntilScriptLoaded(loadConstants, "sp.js")});
  }
});
function loadConstants() {
ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
var listcol = web.get_lists();
ctx.load(listcol);
var oList = listcol.getByTitle('Aktionslisten');
var camlQuery = new SP.CamlQuery();
   camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/><Value Type=\'Number\'>1</Value></Geq></Where></Query></View>');
collListItem = oList.getItems(camlQuery);
   ctx.load(collListItem);
ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));
}

function onSuccess(sender, args) {
   var listInfo = '';
    var listEnumerator = collListItem.getEnumerator();
    while (listEnumerator.moveNext()){
        oList = listEnumerator.get_current();
        var ID = oList.get_id();
        contentobject = oList.get_contentType();
        ctx.load(contentobject);
        ctx.executeQueryAsync(function(){
            var value = contentobject.get_name();
            alert("VAL: "+value);
        },function(){alert("No Success");});


    }
}

function onFail(sender, args) {
    console.log("Errorlog: "+ args.get_message());
}

但是这段代码只给了我几次最后一项的内容类型。 我想我可能在 "executeQuery" 函数上做错了什么?

此致, 安德烈

更新(见下方评论)

代码的新尝试,也不起作用:

var collListItem = null;
var ctx = null;
var oList = null;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
  OnPostRender: function() { 
  $(document).ready( function() {ExecuteOrDelayUntilScriptLoaded(loadConstants, "sp.js")});
  }
});
function loadConstants() {
ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
var listcol = web.get_lists();
ctx.load(listcol);
var oList = listcol.getByTitle('Aktionslisten');
var camlQuery = new SP.CamlQuery();
   camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/><Value Type=\'Number\'>1</Value></Geq></Where></Query></View>');
collListItem = oList.getItems(camlQuery);
   ctx.load(collListItem);
ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));
}

function onSuccess(sender, args) {
   var listInfo = '';
    var listEnumerator = collListItem.getEnumerator();
    while (listEnumerator.moveNext()){
        oList = listEnumerator.get_current();
        getcontenttypetitle(oList.get_contentType(), ctx);
    }
}

function onFail(sender, args) {
    console.log("Errorlog: "+ args.get_message());
}

function getcontenttypetitle(contentobject,clientContext){
    this.object = contentobject;
    clientContext.load(object);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess2), Function.createDelegate(this,this.onFail2));
}

function onSuccess2 (sender,args){
 alert("VAL: "+ this.object.get_name());
}

function onFail2(sender,args){
    alert("fail");
}

//$(":input[title='Aktionsliste']").find('option:contains(Teammeetings)').remove();

它多次提醒同一件事的原因是,当执行对 executeQueryAsync 的回调时,闭包中的任何变量都引用该范围内的实际变量。也就是说,当您调用 executeQueryAsync 时,闭包会捕获变量 contentobject,而不是它的当前值。由于它引用了变量,它的回调值就是循环结束时的值。要修复它,您需要为每个内容对象创建一个单独的闭包。要了解这是如何完成的,请阅读有关此问题的答案:.

你更接近于你之前的修订。这是您需要更改的第一个版本的部分。 (注意代码中的注释。)

while (listEnumerator.moveNext()){
    oList = listEnumerator.get_current();
    var ID = oList.get_id();
    contentobject = oList.get_contentType();
    ctx.load(contentobject);
    ctx.executeQueryAsync(
        // !! Here we use an IIFE (immediately invoked function expression)
        // to create a new callback each time through the loop.
        // We pass in the contentobject into our IIFE,
        // which causes the closure to be around the variable 
        // inside the scope of our IIFE,
        // instead of inside the scope of the function containing the while loop.
        (function (contentobject) {
            return function () {
                var value = contentobject.get_name();
                alert("VAL: "+value);
            };
        }(contentobject)),
        function () {
            alert("No Success");
        }
    );
}