本文介绍了JSOM Sharepoint Online,创建可重用的JSOM调用函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我目前正在开发一个自定义内联网,需要在不同的列表上至少进行5次jsom调用。 我已经成功创建了一个成功接收变量并返回信息的函数,但是当我尝试做其中两个时,我似乎遇到了冲突并且它失败了,只显示第二个叫做。 这是尝试重用该函数时出现的错误。 我已经创建了与REST API一起使用的类似函数,但在这里我们需要使用Publishing Images,因此需要JSOM。 你能帮我找到问题吗?请按下面的方法:) $ ( document )。 ready ( function (){解决方案 你可以像这样使用Jquery Deferred和Promise进行多次查询: (document).ready(function(){ ExecuteOrDelayUntilScriptLoaded(getMultipleListitems()," sp.js"); }); 函数getMultipleListitems(){ var siteUrl = _spPageContextInfo.siteAbsoluteUrl; var querystr1 ="< View>< Query>< OrderBy>< FieldRef Name =' Order0'Ascending ='TRUE'/>< / OrderBy>< / Query>< / View>" ;; var querystr2 ="< View>< Query>< Where>< ; Eq>< FieldRef Name ='DisplayAnnouncement'/>< Value Type ='Choice'>是< / Value>< / Eq>< / Where>< OrderBy>< FieldRef Name ='ID'Ascending ='FALSE'/>< / OrderBy>< / Query>< RowLimit> 5< / RowLimit>< / View> ;" retrieveListItems(siteUrl," Essential Links",querystr1).done( function(getEnuCollection){drawEssentialLinks(getEnuCollection);},function(getEnuCollection){console.log('fail在第一次打电话');}); retrieveListItems(siteUrl," Announcements",drawAnnouncements,querystr2).done( function(getEnuCollection){drawAnnouncements(getEnuCollection);},function(getEnuCollection){console.log('第二次电话失败');}); } 函数retrieveListItems(siteUrl,listTitle,Query){ //在此声明你的延期对象 var deferred = .Deferred(); var clientContext = new SP.ClientContext(siteUrl); var olist = clientContext.get_web()。get_lists()。getByTitle(listTitle); var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml(Query); this.collListItem = oList.getItems(camlQuery); clientContext.load(collListItem); clientContext.executeQueryAsync( Function.createDelegate(this,this.success), Function.createDelegate(this,this.failure)); //返回你的Promise对象 return deferred.promise(); }; 函数成功(sender,args){ //在此解析你的对象 deferred.resolve(ocamlItems.getEnumerator()); } 函数onQueryFailed(sender,args){ console.log(args); //拒绝你的对象 deferred.reject(ocamlItems.getEnumerator()); } 函数drawEssentialLinks(getEnuCollection){ var listItemEnumerator = getEnuCollection; while(listItemEnumerator.moveNext()){ var oListItem = listItemEnumerator.get_current(); var link = oListItem.get_item(" Hyperlink"); if(link){ link = oListItem.get_item(" Hyperlink")。get_url(); } else { link ="#" ;; } if(!isNaN(oListItem.get_item(" Order0"))){ if(oListItem.get_item(" OpenLinkInNewTab")=) ="是"){ listItems + ="< li>< a href ='" + link +"'target ='_ blank'>" + oListItem.get_item('Image ")+"<跨度>" + oListItem.get_item(QUOT;名称")+"< /跨度>< / A>< /立GT;" } else { listItems + ="< li>< a href ='" + link +"'>" + oListItem.get_item('Image')+" <跨度>" + oListItem.get_item(QUOT;名称")+"< /跨度>< / A>< /立GT;" } } } Hi All,I am currently working on a custom intranet that will have at least 5 jsom calls needing to be made on it to different lists.I have managed to create a function that successfully takes in the variables and returns the information, but when trying to do two of them I seem to get a conflict and it fails, only showing the second one called.Here is the error that appears when trying to reuse the function.I have have created similar functions for use with the REST API but in this we need to use Publishing Images, so JSOM is required.Can you help me find what is wrong with my approach below please :)$(document).ready(function(){ 解决方案 Hi,You could use Jquery Deferred and Promise like this for multiple query: (document).ready(function(){ ExecuteOrDelayUntilScriptLoaded(getMultipleListitems(),"sp.js");});function getMultipleListitems(){ var siteUrl = _spPageContextInfo.siteAbsoluteUrl; var querystr1 = "<View><Query><OrderBy><FieldRef Name='Order0' Ascending='TRUE' /></OrderBy></Query></View>"; var querystr2 = "<View><Query><Where><Eq><FieldRef Name='DisplayAnnouncement'/><Value Type='Choice'>Yes</Value></Eq></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy></Query><RowLimit>5</RowLimit></View>" retrieveListItems(siteUrl,"Essential Links",querystr1).done( function (getEnuCollection){drawEssentialLinks(getEnuCollection);},function (getEnuCollection){console.log('fail in first call');}); retrieveListItems(siteUrl,"Announcements",drawAnnouncements,querystr2).done( function (getEnuCollection){drawAnnouncements(getEnuCollection);},function (getEnuCollection){console.log('fail in second call');});}function retrieveListItems(siteUrl,listTitle,Query) { //Declare your deferred object here var deferred=.Deferred(); var clientContext = new SP.ClientContext(siteUrl); var olist = clientContext.get_web().get_lists().getByTitle(listTitle); var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml(Query); this.collListItem = oList.getItems(camlQuery); clientContext.load(collListItem); clientContext.executeQueryAsync( Function.createDelegate(this, this.success), Function.createDelegate(this, this.failure) ); //Return your Promise Object return deferred.promise();};function success(sender, args) { //Resolve your object here deferred.resolve(ocamlItems.getEnumerator());}function onQueryFailed(sender, args) { console.log(args); //Reject your object here deferred.reject(ocamlItems.getEnumerator());}function drawEssentialLinks(getEnuCollection) { var listItemEnumerator = getEnuCollection; while (listItemEnumerator.moveNext()) { var oListItem =listItemEnumerator.get_current(); var link =oListItem.get_item("Hyperlink"); if(link){ link = oListItem.get_item("Hyperlink").get_url(); }else{ link = "#"; } if(!isNaN(oListItem.get_item("Order0"))){ if(oListItem.get_item("OpenLinkInNewTab") =="Yes"){ listItems += "<li><a href='"+link+"' target='_blank'>"+oListItem.get_item('Image')+"<span>"+oListItem.get_item("Title")+"</span></a></li>" }else{ listItems += "<li><a href='"+link+"'>"+oListItem.get_item('Image')+"<span>"+oListItem.get_item("Title")+"</span></a></li>" } } } 这篇关于JSOM Sharepoint Online,创建可重用的JSOM调用函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 02:51