sunkezai 发表于 2015-9-29 13:06:20

使用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=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<soap:Envelope xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;>
<soap:Body>
<GetAttachmentCollection xmlns=&quot;http://schemas.microsoft.com/sharepoint/soap/&quot;>
<listName>string</listName>
<listItemID>string</listItemID>
</GetAttachmentCollection>
</soap:Body>
</soap:Envelope>

使用如下的两个基本方法就可以得到第一个Attachment的地址。






function GetAttachments(listName,listItemId) {

    var soapEnv = '<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>' +

                  '<soap:Envelope xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;>' +

                  '    <soap:Body>'+

                  '      <GetAttachmentCollection xmlns=&quot;http://schemas.microsoft.com/sharepoint/soap/&quot;>'+

                  '      <listName>'+ listName +'</listName>'+

                  '      <listItemID>'+ listItemId +'</listItemID>'+

                  '      </GetAttachmentCollection>'+

                  '    </soap:Body>'+

                  '</soap:Envelope>';            

    return $.ajax({

      async: false,

      url: &quot;http://yoursite/_vti_bin/lists.asmx&quot;,

      beforeSend: function(xhr) {

               xhr.setRequestHeader(&quot;SOAPAction&quot;, &quot;http://schemas.microsoft.com/sharepoint/soap/GetAttachmentCollection&quot;);

      },

      type: &quot;POST&quot;,

      dataType: &quot;xml&quot;,

      data: soapEnv,

      complete: ParseFirstAttachmentURL,

      contentType: &quot;text/xml; charset=\&quot;utf-8\&quot;&quot;

    });

}

   

function ParseFirstAttachmentURL(xmlData, textStatus) {

   //alert(xmlData.responseText);

   imageURL = $(xmlData.responseXML).find(&quot;Attachment&quot;).eq(0).text();

}
  
  在使用时候需要注意的一点就是SOAPAction需要beforeSend中设置,否则会出错。
  
  更多可以参考:
  http://weblogs.asp.net/jan/archive/2009/04/09/calling-the-sharepoint-web-services-with-jquery.aspx
  http://weblogs.asp.net/jan/archive/2009/05/25/quot-the-security-validation-for-this-page-is-invalid-quot-when-calling-the-sharepoint-web-services.aspx
页: [1]
查看完整版本: 使用JQuery调用SharePoint Web Service