使用JQuery调用SharePoint Web Service
我不喜欢用SharePoint 默认的向导使用web service,因为生成的代码被转义字符替换了,不好维护。
我也不会写长篇的Ajax来使用SharePoint Web Service, 毕竟我不是一个专业的网页设计师,不了解代码兼容性等等一些花费时间的东西。
最终我选择了JQuery,很流行,很简单。
为了使用webservice, 首先需要知道soap的格式, 以listasmx 中的GetAttachmentCollection方法为例
访问 http://yoursite/_vti_bin/lists.asmx?op=GetAttachmentCollection
看到如下格式的格式:
POST /_vti_bin/lists.asmx HTTP/1.1 Host: sharepointasia Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/GetAttachmentCollection" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <GetAttachmentCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/"> <listName>string</listName> <listItemID>string</listItemID> </GetAttachmentCollection> </soap:Body> </soap:Envelope>
使用如下的两个基本方法就可以得到第一个Attachment的地址。
function GetAttachments(listName,listItemId) {
var soapEnv = '<?xml version="1.0" encoding="utf-8"?>' +
' <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
' <soap:Body>'+
' <GetAttachmentCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/">'+
' <listName>'+ listName +'</listName>'+
' <listItemID>'+ listItemId +'</listItemID>'+
' </GetAttachmentCollection>'+
' </soap:Body>'+
' </soap:Envelope>';
return $.ajax({
async: false,
url: "http://yoursite/_vti_bin/lists.asmx",
beforeSend: function(xhr) {
xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetAttachmentCollection");
},
type: "POST",
dataType: "xml",
data: soapEnv,
complete: ParseFirstAttachmentURL,
contentType: "text/xml; charset=\"utf-8\""
});
}
function ParseFirstAttachmentURL(xmlData, textStatus) {
//alert(xmlData.responseText);
imageURL = $(xmlData.responseXML).find("Attachment").eq(0).text();
}
在使用时候需要注意的一点就是SOAPAction需要beforeSend中设置,否则会出错。
更多可以参考:
http://weblogs.asp.net/jan/archive/2009/04/09/calling-the-sharepoint-web-services-with-jquery.aspx