Sharepoint JSCOM 列表操作

Posted lishidefengchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 列表操作的主要内容,如果未能解决你的问题,请参考以下文章

Power Automate 创建 SharePoint Online 列表项目

Power Automate 创建 SharePoint Online 列表项目

使用 SPFx 扩展将自定义操作部署到 SharePoint 2019 列表

Google地图无法使用IE11(sharepoint 2010)

C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据

如何通过代码在线开发sharepoint中的列表