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

SharePoint 2013 - Get content type names with Javascript

本文关键字:类型 获取 2013 使用 Javascript SharePoint      更新时间:2023-09-26

就像标题说的那样,我想用Javascript检索列表项的内容类型名称。 具体来说:用户在列表 A 中打开"新表单",使用 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());
}

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

此致敬意安德烈

更新(见下面的评论)

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

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 的回调时,闭包中的任何变量都会引用该范围内的实际变量。也就是说,闭包捕获变量 contentobject,而不是调用 executeQueryAsync 时的当前值。由于它引用变量,因此其回调值是循环末尾的值。要修复它,您需要为每个内容对象创建一个单独的闭包。要了解如何完成此操作,请阅读有关此问题的答案:https://stackoverflow.com/a/19324832/2407870。

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

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