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

时间:2016-02-24 08:50:25

标签: javascript sharepoint csom

就像标题所说我想用Javascript检索列表项的contenttype名称。具体来说:用户在列表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());
}

但是这段代码只给了我几次最后一项的内容类型。 我想我可能在“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();

1 个答案:

答案 0 :(得分:0)

它多次向同一事件发出警报的原因是,当执行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");
        }
    );
}