首页 > 代码库 > Sharepoint JSCOM 列表操作

Sharepoint JSCOM 列表操作

  SP.SOD.executeFunc(‘sp.js‘, ‘SP.ClientContext‘, retrieveListItemsInclude); //确保js文件加载,再执行方法
    function retrieveListItemsInclude() {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle(‘背景图片‘);
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(‘<View><RowLimit>100</RowLimit></View>‘);
        this.collListItem = oList.getItems(camlQuery);

        clientContext.load(collListItem, ‘Include(ServerUrl)‘); //ServerUrl的路径类似---/DocLib/123.jpg;
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded),
            Function.createDelegate(this, this.onQueryFailed)
        );
    }
    
//成功回调函数
function onQuerySucceeded(sender, args) { this.BackgroundImg = []; var listItemEnumerator = collListItem.getEnumerator(); while (listItemEnumerator.moveNext()) { var oListItem = listItemEnumerator.get_current();

/**这里需要注意!对于某些属性有自带的获取方法,比如:get_id(),get_title();
*但对于大多数的属性都必须使用get_item("属性名")来获取值,
**/而且”属性名“要跟 Include("属性名")中的属性名保持大小写一致!!

       this.BackgroundImg.push(oListItem.get_item("ServerUrl")); } if (this.BackgroundImg.length > 0) { var index = Math.floor((Math.random() * this.BackgroundImg.length)); //随机数 $("#s4-workspace").css("background-image", "url(" + this.BackgroundImg[index] + ")"); } }
//失败回调函数
function onQueryFailed(sender, args) { alert(‘Request failed. ‘ + args.get_message() + ‘\n‘ + args.get_stackTrace()); }

要查看列表字段的名称(Title)和对应的内部名称(InternalName),可以参考官方文档:

function retrieveAllListsAllFields(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    var collList = oWebsite.get_lists();

    this.listInfoArray = clientContext.loadQuery(collList, 
        ‘Include(Title,Fields.Include(Title,InternalName))‘);

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this._onQueryFailed)
    );
}

function onQuerySucceeded() {
    var listInfo = ‘‘;

    for (var i = 0; i < this.listInfoArray.length; i++) {
        var oList = this.listInfoArray[i];
        var collField = oList.get_fields();
        var fieldEnumerator = collField.getEnumerator();
            
        while (fieldEnumerator.moveNext()) {
            var oField = fieldEnumerator.get_current();
            var regEx = new RegExp(‘name‘, ‘ig‘);
            
            if (regEx.test(oField.get_internalName())) {
                listInfo += ‘\nList: ‘ + oList.get_title() + 
                    ‘\n\tField Title: ‘ + oField.get_title() + 
                    ‘\n\tField Name: ‘ + oField.get_internalName();
            }
        }
    }
    alert(listInfo);
}

function onQueryFailed(sender, args) {
    alert(‘Request failed. ‘ + args.get_message() + 
        ‘\n‘ + args.get_stackTrace());
}

 参考:https://msdn.microsoft.com/zh-cn/library/office/jj163201.aspx

Sharepoint JSCOM 列表操作