首页 > 代码库 > 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 列表操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。