Document library acts as the primary repository for the document collaboration in SharePoint. It provides the flexibility to version documents, check-out/check-in, implement retention policies and performs other collaboration options. We can upload the documents to the document library easily. However, we will be faced with the requirement to programmatically upload the documents into the SharePoint library as well. We get the dialog, as shown below, which can be used to browse and upload the documents.



Let’s see how we can implement the file upload to the document library, using the JavaScript object model. In order to provide browsing and selection of the documents, we will be using HTML 5 input control. Make sure you are using IE version greater than 8, else HTML 5 support won’t be available.

We can test this in SharePoint, by adding it to the content editor Web part, as shown below:

It gives us a UI, where we can upload the file to the document library.



Once we have browsed the required file, click Upload. It will upload the file to the document library.

Pitfall

Though we can upload the file to the document library using JavaScript object model, there is a pitfall that we should be aware of. JSOM has an inherent limitation that only files of sizes up to 5MB can be uploaded using this method. For bigger file size, we have to use REST to upload the file to the document library.