Search This Blog

Wednesday, August 31, 2016

SharePoint 2013 working with file: How to upload attachments to SharePoint list item using REST services

You just need to provide item id to which you want to attach file and attachment file name. Rest of the things are similar to how to upload the file.

Following is the complete code to upload the attachments:

<html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script>
function AddAttachments()
{
var digest = "";
$.ajax(
{
                url: "/_api/contextinfo",
                method: "POST",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose"
                },
                success: function (data) {
                digest = data.d.GetContextWebInformation.FormDigestValue;
                },
                error: function (data) {
                               
                }
}).done(function() {
                var fileInput = $('#uploadFile');
                var fileName = fileInput[0].files[0].name;
                var reader = new FileReader();
                reader.onload = function (e) {
                var fileData = e.target.result;
                                var res11 = $.ajax(
                                {                             
                                                url: "/_api/web/lists/getbytitle('testlist')/items(1)/AttachmentFiles/ add(FileName='" + fileName + "')",                                       
                                                method: "POST",
                                                binaryStringRequestBody: true,
                                                data: fileData,
                                                processData: false,
                                                headers: {
                                                                "ACCEPT": "application/json;odata=verbose",                                                                                                                                   
                                                                "X-RequestDigest": digest,
                                                                "content-length": fileData.byteLength
                                                },                                                                                                                            
                                                success: function (data) {                                            
                                                                                                               
                                                },
                                                error: function (data) {
                                                                alert("Error occured." + data.responseText);
                                                }
                                });                          
                };
                reader.readAsArrayBuffer(fileInput[0].files[0]);

});                                          
}
</script>
</head>
<body>
<div>
                <input id="uploadFile" type="file">
</div>

<div>
                <input type="submit" onclick="AddAttachments()" value="Add Attachments"> </input>
</div>
</body>
</html>

No comments:

Post a Comment