【问题标题】:SharePoint newListItem Soap with img attachment带有 img 附件的 SharePoint newListItem Soap
【发布时间】:2015-05-23 18:57:40
【问题描述】:

我的 Apache Cordova 应用程序中有一个函数可以在共享点列表中创建一个新的列表项,我想知道是否可以将图像添加到这个新项中,这将作为“附件”出现在共享点列表。我添加新项目的功能如下所示:

function CreateItem(Title, Description) { 
var soapEnv =          
"<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +         
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +          
"xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" +      
"<soapenv:Body>" +                           
"<UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" +     
"<listName>LISTNAME</listName>" +                
"<updates>" +                          
"<Batch OnError=\"Continue\">" +           
"<Method ID=\"1\" Cmd=\"New\">" +            
"<Field Name=\"ID\">New</Field>" +            
"<Field Name=\"Title\">" + Title + "</Field>" +
"<Field Name=\"Description\">" + Description + "</Field>" + 
"</Method>" +   
"</Batch>" +                         
"</updates>" +                        
"</UpdateListItems>" +               
"</soapenv:Body>" +                  
"</soapenv:Envelope>";               
$.ajax({     
url: "URL",
type: "POST",                           
dataType: "xml",                        
data: soapEnv,                         
beforeSend: function (xhr) {             
xhr.setRequestHeader("SOAPAction",           
"http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"); 
},                           
complete: processCreateResultSuccess,    
contentType: "text/xml; charset=\"utf-8\"",    
error: processCreateResultError             
});                    

}

图像是使用 Cordova 应用程序拍摄的,ID 为“image”。有什么想法吗?

【问题讨论】:

    标签: javascript ajax cordova sharepoint soap


    【解决方案1】:

    SharePoint:AddAttachment SOAP Web 服务

    是的,您可以使用 SharePoint SOAP Web 服务将图像附件上传到列表。但是,有一些限制。

    我下面的演示使用Lists web serviceAddAttachment 操作。列出了所需的参数,并且可以针对您自己的环境进行修改。该演示只是添加了一个文本文件附件,但它也适用于图像和其他文件类型。也适用于 SP 2007-2013。

    限制是文件必须编码为 base-64 才能在 SOAP 信封内传输。在客户端,base-64 文件编码不是一项简单的任务。我已经使用FileReader 对象完成了它,但这仅在现代浏览器(IE10)中可用。移动设备可能还有其他选择,但我没有研究过。或者,您可以查看较新的REST API

    <html>
    <body>
    
    <script type='text/javascript'>
    
    function addAttachment(  ) {
    
        var webUrl = '',                     // base url when list in sub site
            listName = 'CustomList',         // list name or guid 
            listItemID = '1',                // list item id
            fileName = 'HelloWorld.txt',     // file name 
            attachment = 'SGVsbG8gV29ybGQ=', // base-64 encode file data "Hello Word!"
            xhr, soap;
    
        soap = (
            '<?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>'+
                    '<AddAttachment xmlns="http://schemas.microsoft.com/sharepoint/soap/">'+
                        '<listName>' + listName + '</listName>'+
                        '<listItemID>' + listItemID + '</listItemID>'+
                        '<fileName>' + fileName + '</fileName>'+
                        '<attachment>' + attachment + '</attachment>'+
                    '</AddAttachment>'+
                '</soap:Body>'+
            '</soap:Envelope>'
        );
    
        xhr = new XMLHttpRequest();
        xhr.open( 'POST', webUrl + '/_vti_bin/Lists.asmx', true );
        xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
        xhr.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/sharepoint/soap/AddAttachment');
        xhr.onreadystatechange = function() {
            if (xhr.readyState != 4) return;
            // do something - returns file path or error message
            console.info( xhr.status + '\n' + xhr.responseText );
        }
        xhr.send( soap );
    
    }
    
    </script>
    </body>
    </html>
    

    【讨论】:

    • 谢谢你,我已经有一段时间了,你为我清理了它;)
    猜你喜欢
    • 2011-07-17
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多