Uploading an Html File to Sharepoint 2016
-
- Updated date Jun 08, 2016
- 77.6k
- 0
In this article, you will larn how to upload files to document library in SharePoint 2016 / Office 365 JavaScript object model.
Document library acts as the primary repository for the document collaboration in SharePoint. Information technology provides the flexibility to version documents, check-out/check-in, implement retentiveness policies and performs other collaboration options. We can upload the documents to the document library easily. Even so, we volition be faced with the requirement to programmatically upload the documents into the SharePoint library besides. Nosotros get the dialog, as shown below, which can be used to scan and upload the documents.
Allow's see how nosotros tin can implement the file upload to the document library, using the JavaScript object model. In club to provide browsing and pick of the documents, we volition be using HTML 5 input control. Make sure you are using IE version greater than eight, else HTML five support won't be bachelor.
- Add reference to jQuery file.
- <script language= "javascript" blazon= "text/javascript" src= "//ajax.googleapis.com/ajax/libs/jquery/1.8.one/jquery.min.js" ></script>
- <script language="javascript" blazon= "text/javascript" >
- Within a certificate, phone call SP.SOD.executeFunc, and then equally to load script SP.js. Since nosotros are trying to upload a file from the local machine, nosotros will utilize HTML 5 input control to read the file contents and upload it as an attachment.
Telephone call the primary starting indicate function, say registerClick.
- SP.SOD.executeFunc( 'sp.js' , 'SP.ClientContext' , registerClick);
- Instantiate FileReader object and read the contents of the local file.
- varreader = new FileReader();
- reader.onload = function(e)
- {
- uploadFile(eastward.target.result, fileName);
- }
- reader.onerror = office(e)
- {
- alarm(e.target.mistake);
- }
- reader.readAsArrayBuffer(file);
- Instantiate the client context and get the list case.
- varclientContext= new SP.ClientContext();
- varoWeb= clientContext.get_web();
- varoList= oWeb.get_lists().getByTitle('Documents' );
- Catechumen the file contents into base64 information.
- varbytes = new Uint8Array(arrayBuffer);
- vari, length,out = '' ;
- for (i = 0, length = bytes.length; i < length; i += 1)
- {
- out += String.fromCharCode(bytes[i]);
- }
- varbase64 = btoa(out );
- Create FileCreationInformation object, using the read file data and add the certificate to the library.
- varcreateInfo= new SP.FileCreationInformation();
- createInfo.set_content(base64);
- createInfo.set_url(fileName);
- varuploadedDocument= oList.get_rootFolder().get_files().add(createInfo)
- Load the client context and execute the batch which will transport the request to the Server and perform the unabridged JavaScript object model operations as a batch.
Full Code:- clientContext.load(uploadedDocument);
- clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
- <html>
- <head>
- <script language="javascript" type= "text/javascript" src= "//ajax.googleapis.com/ajax/libs/jquery/one.8.1/jquery.min.js" ></script>
- <script linguistic communication="javascript" type= "text/javascript" >
- varfileInput;
- $(document).ready(function()
- {
- fileInput = $("#getFile" );
- SP.SOD.executeFunc('sp.js' , 'SP.ClientContext' , registerClick);
- });
- function registerClick()
- {
- $("#addFileButton" ).click(readFile);
- }
- vararrayBuffer;
- function readFile()
- {
- varelement = document.getElementById("getFile" );
- varfile = element.files[0];
- varparts = element.value.separate("\\" );
- varfileName = parts[parts.length - i];
- varreader =new FileReader();
- reader.onload = role(e)
- {
- uploadFile(e.target.outcome, fileName);
- }
- reader.onerror = part(e)
- {
- alert(e.target.error);
- }
- reader.readAsArrayBuffer(file);
- }
- varattachmentFiles;
- role uploadFile(arrayBuffer, fileName)
- {
- varclientContext =new SP.ClientContext();
- varoWeb = clientContext.get_web();
- varoList = oWeb.get_lists().getByTitle('Documents' );
- varbytes =new Uint8Array(arrayBuffer);
- vari, length,out = '' ;
- for (i = 0, length = bytes.length; i < length; i += 1)
- {
- out += String.fromCharCode(bytes[i]);
- }
- varbase64 = btoa(out );
- varcreateInfo =new SP.FileCreationInformation();
- createInfo.set_content(base64);
- createInfo.set_url(fileName);
- varuploadedDocument = oList.get_rootFolder().get_files().add together(createInfo)
- clientContext.load(uploadedDocument);
- clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
- }
- part QuerySuccess()
- {
- console.log('File Uploaded Successfully.' );
- }
- function QueryFailure(sender, args)
- {
- console.log('Request failed with fault message - ' + args.get_message() + ' . Stack Trace - ' + args.get_stackTrace());
- }
- </script>
- </caput>
- <body>
- <input id="getFile" blazon= "file" /><br />
- <input id="addFileButton" type= "button" value= "Upload" />
- </body>
- <html>
Nosotros tin test this in SharePoint, by adding it to the content editor Web part, equally shown beneath:
- Salvage the code, depicted above, to a text file and save it into ane of the SharePoint Libraries, say Site Assets.
- Go to the edit settings of the SharePoint page and click Spider web part from the Insert tab.
- Add together content editor Web part.
- Click on edit Spider web art from content edit Web role. Assign the URL of the script text file and click utilize.
Information technology gives usa 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 certificate library using JavaScript object model, there is a pitfall that we should exist 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 take to employ Rest to upload the file to the document library.
Source: https://www.c-sharpcorner.com/article/upload-file-to-document-library-in-sharepoint-2016-javascript-object-model/
0 Response to "Uploading an Html File to Sharepoint 2016"
Post a Comment