简体   繁体   中英

SharePoint newListItem Soap with img attachment

I have a function in my Apache Cordova application to create a new list item inside a sharepoint list, and I was wondering if it was possible to add an image to this new item, this would come as an 'attachment' in the sharepoint list. My function to add a new item looks like this:

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             
});                    

}

The image is taken with the Cordova app and has the ID "image". Any thoughts?

SharePoint: AddAttachment SOAP Web Service

Yes, you can use SharePoint SOAP web services to upload an image attachment to a list. However, there are some limitations.

My demo below uses the AddAttachment action of the Lists web service . The required parameters are listed and can be modified for your own environment. The demo simply adds a text file attachment, but it works with images and other file types too. Also works with SP 2007-2013.

The limitation is that files must be encoded as base-64 for transfer within the SOAP envelope. On the client side, base-64 file encoding is not a trivial task. I've done it using the FileReader object, but that is only available in modern browsers (IE10). There might be other options with mobile devices, but I've not researched it. Alternatively, you might look at the newer 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM