Thursday, November 20, 2014

Uploading multiple attachments to the lists using JSOM

In the previous blog post, we have seen how to attach a single file to the list item

In this blog post we will see how we can attach multiple attachments to a list item using JSOM. The following code will work in both SharePoint hosted apps and regular SPO web applications

The following technologies are referred/used in  the blog post

1. jquery.multifile.js
2. jquery
3. SPO client side libraries

Not much information is available in the internet to attach multiple files to the list items.

In your custom form, where you want to attach multiple files, add the following html tags which will facilitate the user to attach multiple files to the list item

As part of this post, we assume that, we have list called "MyList" and in that list we have two list columns called "FirstName" and "LastName"

When we insert data to this list, we also attach multiple list items. The below code snippet will show the simple html that has been created for the user to insert data into the list called "MyList"



As you can see in the above code snippet, I have two html text boxes for FirstName and LastName and a file input control.

To facilitate multi file upload control, I am leveraging "jquery.multifile.js" plugin. If we don't use that plugin, the user will select only one input file for upload

When the user selects multiple files, here is how the UI looks like

 
As you can, with the help of jquery plug in, we can select multiple files. The complete UI will look like this



I also have a submit button and on click of that button, I want to insert a new list item as well as attach as the files that user has selected

On click of btnSubmit, all the files that are selected by the user will be attached to the list item.

Here is the code snippet for the btnSubmit click event. I have written this click event in the jquery document load function



As a first step, loop all the attachments the user as selected and push that information into some javascript array. That javascript array function will just have the file information and not the contents of that file



Create another JSON array, to hold all the values the user has entered such as first name, last name etc. and including the file array that has been created above

data.push({"FirstName": $("#txtFirstName").val().trim(), "LastName": $("#txtLastName").val().trim(), "Files": fileArray});

After this, I have written some function will insert list item also attach the list items also



I am using regular JSOM code for creating the list items. Since attachments has to be inserted  to the newly created item, I need to know the ID to which I need to insert the attachments

So, first I will insert/create the data, get the list id and to that list id, we need to insert attachments


After the getting the list id, I will loop with all the attachments the user has selected. I wont resolve the function, till I insert all the list attachments. If all the attachments are inserted, I will resolve the function

loopFileUpload function need to called recursively till all the list attachments are inserted



As you can see in the above function, loopFileUpload has been called recursively till all the list attachments are inserted

With in the loopFileUpload function, I am calling another function called uploadFile which will associate the list attachment to the id that has been passed to that function



Within the uploadFile function, I am calling another function called, getFileBuffer, will will read the actual contents of the file and pass that information to uploadFile function

We are using REST based approach for inserting the list attachments as REST will allow to upload upto 2 GB of file

Here is the url that needs to be used to access the list item id to which we need to insert the list attachment




Here is the complete source code of the page that I used for this blog post



Here is the snapshot of the list item that got created using this code with multiple attachments
 

17 comments:

  1. Thanks for this nice article .

    ReplyDelete
  2. hi,

    This is great... I can upload the files and save the simple data using the above solution. but When I try to people picker data, this solution doesn't work.

    var requestorVal = $("#" + tempRequestor).text();
    data.push({"Requestor": context.get_web().ensureUser(requestorVal), "Files": fileArray});

    I can see the value in requestorVal but data.push line didn't execute properly. If I replace it with anyother fileds then it runs perfectly fine.

    Please let me know, If I am missing anything.

    Regards,
    Hargopal.

    ReplyDelete
    Replies
    1. that's cause context.get_web().ensureUser('user_login') need to be executed before you will push it to data array.

      for example:
      var user = web.ensureUser(requestorVal);
      context.load(user)
      context.executeQueryAsync(getUser(user), fail);

      and in getUser(user) use user.get_id() property to push it to your data array.

      Delete
  3. Hi,

    I tried with the same code. But i am the error as "Cannot read property '0' of null". Can you please help me on this.

    Maha

    ReplyDelete
  4. Hi,

    I tried with the same code. But i am getting the error as "Cannot read property '0' of null" while looping the files. Can you please help me on this.

    Maha

    ReplyDelete
  5. The code is working perfect.
    Thanks for sharing.

    ReplyDelete
  6. I tried with the same code.But if there are multiple pdf files as attachment,then it gives error like 409 conflict and doesn't add attachments

    ReplyDelete
  7. Did anyone implemented multiple files upload to document library with custom column values?

    ReplyDelete
  8. is dere a way to delete multiple attachemnts from sharepoint list after this method is completed

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Hi, i just wanna thank you for the code. It works like a charm!
    Greetings from Argentina.

    ReplyDelete
  11. Any idea about how to add an upload progress bar for large files?
    This example is working : http://jsfiddle.net/GvdSy/
    but i couldn't merge the code with yours :(
    Thanks for sharing!

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. I can't find jquery.MultiFile.js file

    ReplyDelete
  14. Hi,
    I tried this code but couldn't success. the code can read 2.file
    files but $.getScript cannot work for second files.
    Also i did not get any error. Do you have any clue?
    Thank You,

    ReplyDelete
  15. Hi I tried same code but it is not working for multifile slect,any clue

    ReplyDelete