简体   繁体   English

如何从SharePoint列表中使用JavaScript显式加载列

[英]How to load explicitly columns with javascript from sharepoint list

The following code works perfect 以下代码非常完美

$(document).ready(function () {
    var scriptbase = "/_layouts/15/";
    $.getScript(scriptbase + "SP.Runtime.js");
    ExecuteOrDelayUntilScriptLoaded(ready, "SP.Runtime.js");
})

function ready() {
    var scriptbase = "/_layouts/15/";
    $.getScript(scriptbase + "SP.js", function(){
        var d = $.Deferred();
        var path = document.location.href
        console.log(path);          
        var context = new SP.ClientContext('http://myserver/centrodeprocesos/procesos');
        var oList = context.get_web().get_lists().getByTitle('ImagenesDeFondo');
        var camlQuery = SP.CamlQuery.createAllItemsQuery();
        var collTermListItem = oList.getItems(camlQuery);
        context.load(collTermListItem);         
        var o = {d: d, collTermListItem:collTermListItem};
        context.executeQueryAsync(
            Function.createDelegate(o, onQuerySucceeded),
            Function.createDelegate(o, onQueryFailed));
        return d.promise();
    });   
}


function onQuerySucceeded()
{
    var listItemEnumerator = this.collTermListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        console.log(oListItem.get_item('Title'));
        //console.log(oListItem.get_item('Url'));
    }
    this.d.resolve(oListItem);
}

function onQueryFailed() {
    this.d.reject("something bad happened");
}

However I need to also print the URL which is a field in the list 但是我还需要打印URL,它是列表中的一个字段

$(document).ready(function () {
    var scriptbase = "/_layouts/15/";
    $.getScript(scriptbase + "SP.Runtime.js");
    ExecuteOrDelayUntilScriptLoaded(ready, "SP.Runtime.js");
})

function ready() {
    var scriptbase = "/_layouts/15/";
    $.getScript(scriptbase + "SP.js", function(){
        var d = $.Deferred();
        var path = document.location.href
        console.log(path);          
        var context = new SP.ClientContext('http://myserver/centrodeprocesos/procesos');
        var oList = context.get_web().get_lists().getByTitle('ImagenesDeFondo');
        var camlQuery = SP.CamlQuery.createAllItemsQuery();
        var collTermListItem = oList.getItems(camlQuery);
        context.load(collTermListItem, 'Include(Url)');         
        var o = {d: d, collTermListItem:collTermListItem};
        context.executeQueryAsync(
            Function.createDelegate(o, onQuerySucceeded),
            Function.createDelegate(o, onQueryFailed));
        return d.promise();
    });   
}


function onQuerySucceeded()
{
    var listItemEnumerator = this.collTermListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        console.log(oListItem.get_item('Title'));
        console.log(oListItem.get_item('Url'));
    }
    this.d.resolve(oListItem);
}

function onQueryFailed() {
    this.d.reject("something bad happened");
}

The error is the property has not been initialized or need to be requested explicitly. 错误是属性尚未初始化或需要显式请求。

SP.ClientContext.load method allows to specify which properties have to be requested. SP.ClientContext.load方法允许指定必须请求哪些属性。 In the example you have provided, only Url property is requested: 在您提供的示例中,仅请求Url属性:

context.load(collTermListItem, 'Include(Url)');

but two properties are excepted as a result: 但结果排除了两个属性:

console.log(oListItem.get_item('Title'));  //<--this property is not initialized
console.log(oListItem.get_item('Url'));

So, the solution would be to request both properties: 因此,解决方案是同时请求两个属性:

context.load(collTermListItem, 'Include(Title,Url)'); 

Example

The following example demonstrates how to retrieve Title and Url properties from Images library: 下面的示例演示如何从Images库检索Title和Url属性:

var listTitle = 'Images';

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);   
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items,'Include(Title,FileRef)');       

ctx.executeQueryAsync(
    function () { 
        for(var i = 0; i < items.get_count();i++) {
            var item = items.getItemAtIndex(i);
            console.log(item.get_item('Title'));
            console.log(item.get_item('FileRef'));       
        }
    },
    function (sender, args) { 
        //Error handling goes here..
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM