REST API: Add a plain text file as an attachment to a list item
By Anatoly Mironov
SharePoint 2013 REST API has been enhanced and extended. The old _vti_bin/listdata.svc is still there, but the new api for working with lists and list items is much more and obviously a part of a bigger api: _api/web/lists Yesterday I saw an interesting question on SharePoint StackExchange:
The instructions in the MSDN resource are not so detailed, the cannot be. The guy who asked the question did as it stood in the examples. But sometimes solutions for SharePoint need some small adjustments :) Here is the simplest code to create an attachment in plain text for a list item 1 in the list called List1 in the root web. That’s it. But it works: [sourcecode language=“javascript”] var content = “Hello, this text is inside the file created with REST API”; var digest = $("#__REQUESTDIGEST").val(); var composedUrl = “/_api/web/lists/GetByTitle(‘List1’)/items(1)/AttachmentFiles/add(FileName=‘readme.txt’)”; $.ajax({ url: composedUrl, type: “POST”, data: content, headers: { “X-RequestDigest”: digest } }) [/sourcecode] This example is of course just for demonstration. It uses only hard-coded values. But it shows how simple it is to create a list item attachment using SharePoint 2013 REST API and “upload” plain text asynchronously to the server.
Not only plain text (update 2013-03-05)
Of course, uploading only a plain text isn’t enough. The same person Fedor Shihantsov came with an additional question and solved it: Upload a non-text file. Somehow jQuery ajax didn’t work. SP.RequestExecutor worked: [sourcecode language=“javascript”] $(document).ready( dofunc ); function dofunc() { var control = document.getElementById(“ufile”); control.addEventListener(“change”, fdocattach, false); } var file; var contents; function fdocattach(event) { var i = 0, files = event.srcElement.files, len = files.length; for (; i < len; i++) { console.log(“Filename: " + files[i].name); console.log(“Type: " + files[i].type); console.log(“Size: " + files[i].size + " bytes”); } if (files.length > 0) { file = files[0]; fileName = file.name; var reader = new window.FileReader(); reader.onload = fonload; reader.onerror = function(event) { console.error(“File reading error " + event.target.error.code); }; reader.readAsArrayBuffer(file); } return false; } function _arrayBufferToBase64(buffer) { var binary = ‘’; var bytes = new window.Uint8Array(buffer); var len = bytes.byteLength; for (var i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return binary; } function fonload(event) { contents = event.target.result; $.getScript(”/_layouts/15/SP.RequestExecutor.js”, fonload2); } function fonload2() { var contents2 = _arrayBufferToBase64(contents); var createitem = new SP.RequestExecutor(”/"); createitem.executeAsync({ url: “/_api/web/lists/GetByTitle(‘List1’)/items(1)/AttachmentFiles/add(FileName=’” + file.name + “’)”, method: “POST”, binaryStringRequestBody: true, body: contents2, success: fsucc, error: ferr, state: “Update” }); function fsucc(data) { alert(‘success’); } function ferr(data) { alert(’error\n\n’ + data.statusText + “\n\n” + data.responseText); } } [/sourcecode] I found another example of SP.RequestExecutor: Calling SharePoint search using REST (e.g. from JavaScript or an app)
Comments from Wordpress.com
Justin Cooney - Apr 0, 2013
Reblogged this on Justin Cooney - Programming Tips.
How should I update the attachment in list item ?. I dont want to delete and then add the attachment. Thanks
thank you!
Thanks! You Rock!
gives me a popup saying error,undefined,not found
how to copy attachments from one list to another list
Dear Hasan I am looking same solutition.if you find any solution please pass