我正在使用jQuery通过GetListItems方法访问Sharepoint 2007的SOAP接口来读取自定义公告列表,以便每分钟刷新一次该列表(如果列表的所有者添加了新内容,则新的内容变得可见而没有最终用户刷新他们的分享点屏幕)。我想要做的不仅是刷新列表,我希望列表中的每个项目一次循环一次(也许每个项目保持可见10秒,然后下一个项目将加载到该空间中。SharePoint SOAP GetListItems VS jQuery - 如何使用Ajax循环自定义列表项以及Ajax刷新列表内容?
这里是我的代码至今:
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/SiteCollectionDocuments/jquery.timers-1.0.0.js" ></script>
<script type="text/javascript">
$(document).ready(function() {
// Create the SOAP request
// NOTE: we need to be able to display list attachments to users, hence the addition of the
// <queryOptions> element, which necessitated the addition of the <query> element
var soapEnv =
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
<soapenv:Body> \
<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listName>testlist</listName> \
<viewFields> \
<ViewFields> \
<FieldRef Name='Title' /> \
<FieldRef Name='Body' /> \
<FieldRef Name='ID' /> \
<FieldRef Name='Attachments' /> \
</ViewFields> \
</viewFields> \
<query> \
<Query /> \
</query> \
<queryOptions> \
<QueryOptions> \
<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls> \
</QueryOptions> \
</queryOptions> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>";
// call this SOAP request every 20 seconds
$("#tasksUL").everyTime(20000,function(i){
// our basic SOAP code to hammer the Lists web service
$.ajax({
url: "http://ourdomain.net/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
error: printError,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\""
});
});
});
// basic error display that will pop out SOAP errors, very useful!
function printError(XMLHttpRequest, textStatus, errorThrown)
{
alert("There was an error: " + errorThrown + " " + textStatus);
alert(XMLHttpRequest.responseText);
}
// main method that will cycle through the SoAP response nodes
function processResult(xData, status)
{
$(xData.responseXML).find("z\\:row").each(function()
{
// resets display element
$("#tasksUL").empty();
// gets attachments array - if there is more than one attachment,
// they get seperated by semi-colons in the response
// they look like this natively (just an example):
// ows_Attachments = ";#http://server/Lists/Announcements/Attachments/2/test.txt;
// #http://server/Lists/Announcements/Attachments/2/UIP_Setup.log;#"
var mySplitResult = $(this).attr("ows_Attachments").split(";");
// set up storage for later display of images
var notice_images = "";
// processes attachments - please forgive the kludge!
for(i = 0; i < mySplitResult.length; i++)
{
// check to see the proper link URL gets chosen
if (i % 2 != 0 && i != 0)
{
// strips out pound sign
mySplitResult[i] = mySplitResult[i].replace("#", "");
// (possibly redundant) check to make sure element isn't simply a pound sign
if (mySplitResult[i] != "#")
{
// adds an img tag to an output container
notice_images = notice_images + "<img src='" + mySplitResult[i] + "' border='0' align='right' style='float:right;' /><br />";
}
}
}
// create final output for printing
var liHtml = "<h3>" + $(this).attr("ows_Title") + "</h3><p>" + notice_images + $(this).attr("ows_Body") + "</p>";
// assign output to DIV tags
$("#tasksUL").html(liHtml);
});
}
</script>
<div id="tasksUL"/> </div>
这是很简单的东西,所以到目前为止(虽然寻找体面的文件为你能与GetListItem SOAP请求做的是一项艰巨的)里面的。我遍历返回的行(PprocessResult函数)的块,我重置了分配给DIV块的HTML,以便只有一行显示为输出。代码设置的方式,这意味着只有最后一行排入我的自定义列表将可见,因为我没有代码暂停迭代。
我的想法一直缠绕定时器周围的代码块:
$(xData.responseXML).find("z\\:row").each(MYTIMER(10000, function(){...
但是我遇到了零或混合的结果。
我的问题给你所有的是:什么是最好的方式来设置我的当前代码来刷新源列表数据,就像它现在一样,并循环查询从列表中的结果一次一个(最好用每个项目上的小暂停,以便人们可以阅读它)?
看起来很热的圣牛!我没有考虑将清单项目的循环与用于刷新数据集的AJAX代码分开。我真的很喜欢这种方法,我可以看到在我的应用程序的多个区域使用它。非常感谢!!! – 2009-10-19 14:19:26
好的,我遇到了这个问题。无论我如何链接到循环库中,我得到一个错误: 错误:$(“#tasksUL”)周期是不是一个函数 这里是连接语句: <脚本类型=“文本/ javascript“src =”http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.2.72.js“> 这里是我写的代码添加到上面的代码: (script).ready(function(){('#tasksUL').cycle({ fx:'fade' }); }); 上面的脚本现在为每个列表行创建一个seoperate div ... – 2009-10-20 03:55:10
查看某种测试页面会非常有帮助。说出这里发生的事情有点难。 – 2009-10-20 15:42:54