• 使用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

    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

  • 相关阅读:
    ###第五次作业###
    第四次作业
    第三次作业
    jquery cookie插件
    jquery.form.js(ajax表单提交)
    jquery 中 $.map 用法
    jQuery中的$.grep()使用
    jquery-validation验证插件
    软件工程实践2017第一次作业
    jQuery UI dialog 的使用
  • 原文地址:https://www.cnblogs.com/lambertqin/p/1519396.html
Copyright © 2020-2023  润新知