Uploading Attachments to SharePoint Lists Using SPServices

Easy!For years people have been asking me how they could upload files using SPServices. I’ve dodged the questions every time because I simply didn’t know how.

On a current client project,  I’m building a slick Single Page Application (SPA) to manage tasks in SharePoint 2010. It’s basically a veneer over the clunky out-of-the-box task list experience. Rather than hopping from page to page, the team using the application can accomplish everything they need to do on a single page.

Every project has specific needs, and for this one I’m using KnockoutJS. But the code snippets I give below are generic enough that you should be able to use them in any context with a little thinking.

It’s been going well, but I had been putting off implementing adding attachments because…well, as I said, I didn’t know how.

One of the benefits of the shift from all server side development to a much more significant focus on client side techniques is that some of the bright minds in the SharePoint development community have turned their eyes toward solving these things, too. Usually it’s on SharePoint 2013 or SharePoint Online at Office 365. However, at least now when I search for “SharePoint JavaScript xxx”, there are likely to be some great hits I can learn from.

In this case, there were two excellent posts, one from James Glading and one from Scot Hillier. (See the Resources section below for links.)

The first step is to enable HTML5 capabilities in IE10. This requires two small changes to the master page. This seems simple, but it can have effects that you don’t expect. In other words, don’t consider this just a “no brainer”. You need to plan for the change across your Site Collection and any impacts it may have.

The first change is to switch from using “XHTML 1.0” to “HTML” as the DOCTYPE. This is what “turns on” HTML5.

<!DOCTYPE html>

Then, in this project we set the content meta tag to IE=10 because we want to aim for IE10.

<meta http-equiv="X-UA-Compatible" content="IE=10"/>

If your browser base is different, you can, of course, set this differently. In this project, we will require all users of the application to be running IE10 or greater. Firefox and Chrome have supported the bits of HTML5 we need for so long, that there’s little concern about people who choose to use those browsers.

Once we have those two small changes in the master page (we are using a copy of v4.master with no other customizations at the moment), we “have HTML5”. It’s most obvious because some of the branding flourishes I’ve put in like rounded corners show up in IE10 now, rather than just square boxes.

Once we have HTML5 enabled, we can start to use the File API, a.k.a. the FileReader. This is such a simple little thing, that it’s hard to believe it gives us the capability it does.

<input type="file" id="attachment-file-name"/>

That’s it. That simple HTML gives us a file picker on the page. Depending on your browser, it will look something like this:

File Picker

When you make a file selection with this very familiar widget, you can query the element using jQuery.

var file = $("#attachment-file-name").files[0];

Note that we’re after a single file, so we grab the first object in the file list array. We get an object that looks something like this screen shot from Firebug:

File object from the File Picker

Once we have the file object, we can look at what the Lists SOAP Web Service needs to get in order to upload the file. Again, it’s pretty simple. Here is the list of inputs and output from the MSDN documentation page for the Lists.AddAttachment Method.


A string that contains either the title or the GUID for the list.
A string that contains the ID of the item to which attachments are added. This value does not correspond to the index of the item within the collection of list items.
A string that contains the name of the file to add as an attachment.
A byte array that contains the file to attach by using base-64 encoding.

Return Value

A string that contains the URL for the attachment, which can subsequently be used to reference the attachment.

The first three inputs are straightforward. We pass in the name of the SharePoint list, the ID of the list item, and the name of the file we want to attach. It’s that last input parameter called “attachment” that’s a bit tricky.

When we upload a file via an HTTP POST operation – which is what all of the SOAP Web Services use – we have to pass text. But the files we want to upload are as likely as not to be binary files. That’s where the Base64 format comes in. Believe me, you don’t need to know exactly what the format actually is, but you should probably understand that it enables us to send binary files as text bytes. Those bytes are then decoded on the server end so that the file can be stored in all of its original, pristine glory.

Here’s the code I ended up with, skinnied down as much as possible to make it a little clearer to follow. I draw heavily from Scot and James’ posts for this, but nuanced for SPServices. I’ve also stripped out all of the error handling, etc.

  • First, the getFileBuffer function reads the contents of the file into the FileReader buffer. readAsArrayBuffer is an asynchronous method, so we use a jQuery Deferred (promise) to inform the calling function that the processing is done.
  • The contents of the buffer are then converted from an ArrayBuffer – which is what the FileReader gives us – into a Base64EncodedByteArray. This works by passing the buffer through a Uint8Array along the way.
  • Finally, we use the toBase64String method to convert the SP.Base64EncodedByteArray to a Base64String.

Yeah, that’s a lot to swallow, but again, you don’t really need to understand how it works. You just need to know that it does work.

Finally, we call Lists.AddAttachment using SPServices to do the actual upload.

/* From Scot Hillier's post:
http://www.shillier.com/archive/2013/03/26/uploading-files-in-sharepoint-2013-using-csom-and-rest.aspx */
var getFileBuffer = function(file) {

  var deferred = $.Deferred();
  var reader = new FileReader();

  reader.onload = function(e) {

  reader.onerror = function(e) {


  return deferred.promise();

getFileBuffer(file).then(function(buffer) {
  var bytes = new Uint8Array(buffer);
  var content = new SP.Base64EncodedByteArray(); //base64 encoding
  for (var b = 0; b < bytes.length; b++) {

    operation: "AddAttachment",
    listName: "Tasks",
    listItemID: taskID,
    fileName: file.name,
    attachment: content.toBase64String()


Very cool! And as I tweeted yesterday, far easier than I ever would have expected. Yes, it took me a good chunk of a day to figure out, but it definitely works, and pretty fast, too.

If you use this example as a base, you could fairly easily build out some other file uploading functions. Combined with the other attachment-oriented methods in the Lists Web Services, you can also build the other bits of the attachment experience:

  • GetAttachmentCollection – Returns a list of the lit item’s attachments, providing the full path to each that you can use in a list of links.
  • DeleteAttachment – Once you’ve uploaded an attachment and realized it was the wrong one, this method will allow you to delete it.

Moral of the story: Fear not what you do not know. Figure it out.


What’s the Story for HTML5 with SharePoint 2010? by Joe Li

Uploading Files Using the REST API and Client Side Techniques by James Glading

Uploading Files in SharePoint 2013 using CSOM and REST by Scot Hillier

Lists.AddAttachment Method

This article was also published on IT Unity on Jun 02, 2014. Visit the post there to read additional comments.

Update 2014-05-28 11:55 GMT-5

Hugh Wood (@HughAJWood) took one look at my code above and gave me some optimizations. Hugh could optimize just about anything; he is *very* good at it. I *think* I can even see the difference with larger files.

getFileBuffer(file).then(function(buffer) {
  var binary = "";
  var bytes = new Uint8Array(buffer);
  var i = bytes.byteLength;
  while (i--) {
    binary = String.fromCharCode(bytes[i]) + binary;
    operation: "AddAttachment",
    listName: "Tasks",
    listItemID: taskID,
    fileName: file.name,
    attachment: btoa(binary)

Feminism in Tech – Be a Part of the Solution

My friend (and client) Dan Antion (@dantion) tweeted a link to an excellent article this morning entitled An Open Letter on Feminism In Tech. The subtitle is “We are tired of pretending this stuff doesn’t happen.” I’m right there with this.

My role models about feminism were my mother Christina Bellamy and my paternal Grandfather Denton (of all people!).
Mom and Me18800
Their type of feminism wasn’t the radical feminism of the early 70s with all of it’s harsh demands and in-your-face rhetoric. It was exactly the type of feminism described in this excellent article by a group of women technologists.

Feminism is not a dirty word. Feminism is the radical notion that women are people, and that we want to be treated as equals.

As more eloquently said in the article, we can’t allow the sorts of bad behavior that occur in tech, or anywhere else. In the article, the writers point out that these tenets should apply to *any* group within tech that isn’t the mainstream, white male group that has dominated the industry historically.

But it goes much further than that.

At our son Calder’s school they have a zero tolerance rule about bullying. It causes some uncomfortable moments, but the effect is virtually zero bullying, creating a nurturing, healthy learning environment. Here’s a Wordle that shows many of the words that describe what this leads to. Take out the obviously unaligned ones, and I’d say that this is how we should make tech feel to women, or anyone else.

Runkle Olweus

Each of us should be a part of the solution. In SharePointlandia (aka the SharePoint Community), we have far less of the bad behavior than what I’ve seen in other technologies. So on some levels, we’re lucky. Working with SharePoint to some degree means a sort of self-selection for people who see collaboration and group work as a useful, productive thing. The characteristics of who is a part of those activities becomes less important. But we aren’t perfect either.  There is always room for improvement and collective enlightenment. (Yeah, that sounds a little sappy, but even when you get really good at something, you can strive to be great.)

To borrow a phrase, “If you see something, say something”. Let’s make sure that SharePointlandia is a land of openness and opportunity for all of us, no matter who or what we are.

Addendum: I would be remiss if I didn’t mention Women in SharePoint, which “is dedicated to helping women working as SharePoint professionals reach their career objectives through a variety of community, training and mentoring programs.”

SharePoint Conference .ORG Reston 2014 Wrap Up

I want to thank everyone in the SharePoint practice at Protiviti for another stellar SharePoint Conference .ORG. This was one of the first “big time” conferences at which I was asked to speak four years ago, and it’s always one of my favorites. The Protiviti folks are some of the nicest and most talented people in SharePointLandia.

On Sunday, Kyle Schaeffer (@kyleschaeffer) and I did a workshop on SharePoint 2013 Branding & Responsive Design. Just being in the room with Kyle is an honor; having the opportunity to teach with him is truly a joy. We’ll be repeating our act at the Dallas incarnation of the conference in October.

For the main part of the conference, I talked about Creating a Great User Experience in SharePoint. My slides for the session are available on SlideShare, and also embedded below.

Finally, here’s a photo of the five SharePoint MVPs who spoke at the conference. I’m in some truly esteemed company here, from left to right: Liam Cleary (@helloitsliam), me, Benjamin Naiulin (@bniaulin), Dan Usher (@binarybrewery),  and Doug Hemminger (@DougHemminger).

All 5 #SharePoint MVPs from @SPC_ORG 2014!

All 5 #SharePoint MVPs from @SPC_ORG 2014!

Programmer? Interrupted?

My friend Andrew Connell (@andrewconnell) posted a very interesting article called Programmer Interrupted to Facebook today, saying:

…all developers should share this with the one you love or non-developers you work with. Help them understand why you get so pissed when they come into your office to ask the simplest questions. Many times I feel like I’m holding a castle made of cards up with one finger, holding my breath when suddenly there’s this tiny puff of air that brings everything organized to complete chaos Smile

I do think that these effects vary considerably across individuals, and I don’t think it has to be a permanent condition.

When I was a young programmer (my second job in the mid-80s), I was in a highly interrupt-driven environment at Bain & Co., the management consulting company. I did a bunch of different things during my tenure there, but a lot of it was what we called “programming” at the time. Because I was interrupted extremely frequently – sometimes half a dozen times an hour, by my recollection – I simply learned to be better at it over the six years I was there.

At a later job around 1999-2002 at a company called ArsDigita, I was amazed at how defensive the young developers sometimes were with their time. They occasionally could be almost hostile at interruptions, which in a way ensured that they couldn’t learn to deal with them. I think the headphone thing actually made it worse, since an interruption was more jarring to their memory patterns, as it often required a physical incursion to get their attention. They interrupted each other, too, by the way; I’m not basing this just on my observations from interrupting them myself.

I can’t say that I’m great at managing interruptions, but I do think that I learned to be better at it than some people are. I don’t suggest that everyone embrace interruption – or even encourage it as I sometimes do – but I do suggest that everyone should consider how their work environment actually functions most of the time to consider how best to adapt. If you are likely to be interrupted, work to identify how you can best work inside that construct.

Techies often get a bad rap because they are too defensive and say no too often. Part of that bad rap can come from the impression of “my work right now is more important than whatever you are here for” that comes from reacting unpleasantly to interruptions from the very people for whom we are supposed to be doing the work.

Don’t just try to avoid interruption, treating it as an evil, but welcome it on some level so that you can be a better IT professional. You’ll have a happier user base, and you might just learn some new tricks.

SPTechCon SFO 2014 Wrap Up

SPTechConLogoAnother splendid time was had by all at the latest rendition of SPTechCon, this time in sunny (mostly) San Francisco at the Hilton Union Square. The BZMedia folks who put on SPTechCon are great people and both the San Francisco and Boston versions are on my list of favorite SharePoint events.

Announcing SPTechCon BostonIf you weren’t able to attend this one, you may still be interested in my slides. Face it, though: it’s much more fun to go to these events to watch me make a fool of myself in person. Consider, nay decide that you will be, attending the Boston SPTechCon coming up September 16-19, 2014.

I’ve posted them to SlideShare for your fun and enjoyment. Even though several of the sessions I did were repeats of previous performances, they continually evolve, so every deck always has some new goodies in it.