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
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
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
No comments:
Post a Comment